Tutorial: Connect Teachable webhooks and API to automate course enrollment sequences

Introduction

The following tutorial guides you through how to connect Teachable webhooks and the API in order to automate course enrollment sequences. For example, you might have multiple courses in your school that you want your students to take in a particular order—and you only want them enrolled in Course B after they complete Course A.

This is an advanced tutorial—in order to accomplish the end result, we need to use webhooks to create an “on-demand” monitor that triggers the Teachable API request to run each time a webhook payload is received. Additionally, we need to pull data from the received webhook and automatically include it in the request to the API. (Don’t worry if you’re already overwhelmed…we’ll walk through it step-by-step!)

Typically, one might achieve the above using Zapier—a third-party service that automates these kinds of triggers and actions. While Zapier is great, it can get expensive pretty quickly (it’ll cost a minimum of $39/month to complete 1,500 tasks a month, and only goes up from there). By using the guide below, you can achieve the same result at a much lower cost. We use an API platform called Postman for the purposes of this tutorial, which includes up to 1,000 calls on their free plan, and up to 10,000 calls at $12/month.

You can also get creative and use the general structure provided in this tutorial to connect any webhook to any API endpoint. Ultimately, the goal is to help you automate triggers and actions in whatever way works best for your business.

What you need

We use a few specific programs in this tutorial, including:

  • A Teachable account on the Pro plan or higher, and a Teachable API key
  • Postman – what we’ll be using for this tutorial as an API platform. (If you don’t already have an account, you can start with a free account, and download the application to your computer.)
  • A Slack account (the free plan works just fine!)

Tutorial

First, we need to set up a Slack webhook. Slack is going to play an important role in receiving data from our Teachable webhook. You could theoretically use another application here, but we find that Slack is a) easy to use, b) a great way to monitor incoming webhooks, and c) free.

To set up the Slack webhook:

  1. If you don’t already have a Slack workspace, create one from their website and download the Slack app for your computer.
  2. Next, we need to build an app in Slack. Navigate to https://api.slack.com/apps and click Create an App. In the popup window, select to make an app From scratch.
1302
  1. In the popup window fields, enter in an App Name (we’ll use “Course Enrollment Completed” in this example). You also need to pick a workspace to create your app in. Once you’ve selected, click Create App.
  2. On the following page, navigate to the Add features and functionality section and select Incoming Webhooks.
1294
  1. Turn on the Activate Incoming Webhooks toggle, then click the Add New Webhook to Workspace button.
506
  1. On the following page, select which channel of your Slack workspace you want the incoming webhooks to be posted to. A message will be posted in the channel each time a webhook is received (i.e., each time a student completes a course in your school), so choose wisely! Once you’ve selected a channel, click Allow.
  2. You should see a webhook URL available to copy. Save this URL—we’ll need it in the next section.
511

Now, let’s test that our Slack app works by sending a POST request to the Slack webhook. We’ll also get our basic setup in Postman sorted.

  1. Open up the Postman application, and create a new collection. You can give your collection a name—for this example, we’ll call the collection “Enrollment sequencing”. (Learn more about collections in Postman.)
1193
  1. Add a new request to the collection you created in the step above. We’ll title this request “Slack webhook.” (Learn more about creating and building requests in Postman).
1187
  1. Build the Slack webhook request as a POST request. For the request URL, copy and paste the Slack webhook URL you receive in step 7 of the section above. In the Body tab of the request, select the raw data type option from the dropdown menu, and then select JSON as the input type. Finally, paste the below JSON data in the body window:
{
    "text": "hello, world!"
}
1543
  1. Be sure to Save your changes, then click Send to test the webhook. If everything worked correctly, you should receive a message that looks like the following in the Slack channel you created the app in:
734

Next, we’re actually going to create a new webhook using Postman’s API. This webhook is going to function as our “on-demand” monitor, which will trigger our enrollment sequencing collection each time a webhook is sent.

  1. Go back to your Postman application, and create a new GET request to https://api.getpostman.com/collections. (We chose to put this request in a new collection titled “Postman webhook.”
  2. Before we can make the request, we need to create a Postman API key via your Postman Integrations Dashboard. (Learn more about Postman API keys.) Once you have your Postman API key, add it to the Header of the request as the value for x-api-key:
2292
  1. Be sure to Save your changes, then click Send to make the request. In the return body, search for the original collection you put your Slack webhook request in (ours is titled “Enrollment Sequencing,”) and copy the uid number of that collection.
1540
  1. Now, create another request—this time, we’ll be making a POST request to https://api.getpostman.com/webhooks. We’ll title this request as “Create Postman Webhook,” and we’ll place it in the “Postman webhook” collection.
  2. In the Body tab of the request, select the raw data type option from the dropdown menu, and then select JSON as the input type. Finally, paste the below JSON data in the body window (You can name the webhook whatever you want, and need to replace the “uid-number-here” value with the UID number copied in step 13 above.)
{
    "webhook": {
        "name": "enrollment completed webhook",
        "collection": "uid-number-here"
    }
}
2174
  1. Make sure you’ve added your Postman API key as an x-api-key value in your request header. Then, Save your changes and Send the request.
  2. In the body of the return data, copy the webhookUrl value—this will be needed shortly.
1540

We’re getting there! Next up, we’re going to set up our Enrollment Completed webhook in Teachable.

  1. Log in to your Teachable account, and create a new webhook from your Settings > Webhooks page. (Learn more about creating webhooks in Teachable).
    a. The Webhook URL will be the webhookUrl value copied in step 17 of the section above.
    b. Keep the webhook payload setting toggle turned off.
    c. For the event type, we’ll use Enrollment completed (as we want to get notified when a user is completing a course).
698
  1. To test that everything is working as expected, try triggering the webhook in Teachable (you can create a test student account in your course—and once you’ve reached 100% completion, you should see the test “hello, world!” notification come through your Slack workspace.)

At this point, we know that our Teachable webhook and Slack notification are connected—however, we want to be able to parse through the webhook data that is sent to Slack, and pull out specific pieces of data that we need.

  1. Navigate back to Postman and the POST request to the slack webhook URL.
  2. In the Pre-request Script tab, add the following piece of Javascript:
var previousRequest = JSON.parse(globals.previousRequest),
    webhookRequestData = previousRequest.data;

let user_id = webhookRequestData.object.user_id
let user_name = webhookRequestData.object.user.name
let course_name = webhookRequestData.object.course.name

pm.variables.set("user_id", user_id)
pm.variables.set("user_name", user_name)
pm.variables.set("course_name", course_name)

console.log(JSON.stringify(webhookRequestData));
1688
  1. Next, switch to the Tests tab of the same POST request and add the following Javascript. (You’ll need to replace the 12345 value with the Course ID number of Course A, which is the first course you want to be a part of the sequence. This code instructs the sequence to only continue if the course that was completed was Course A. If any other course was completed, the rest of the sequence will intentionally fail.).
var previousRequest = JSON.parse(globals.previousRequest),
    webhookRequestData = previousRequest.data;

let course_id = webhookRequestData.object.course_id

pm.variables.set("course_id", course_id)

stop = true;
pm.test("Check for Course A", () => {
  pm.expect(course_id).to.eql(12345);
  stop = false;
});
if (stop) {
    postman.setNextRequest(null) 
}
1718
  1. In the Body tab of the request, add some JSON using the user_id, user_name, and course_name variables you just set in the pre-request script. You can customize this as you’d like—this will just be the text that displays in your slack channel when a student completes any course in your school.
{
    "text": "{{user_name}} (User ID {{user_id}}) has completed {{course_name}}."
}
1700
  1. Save your changes. To test if the script works, you can complete a course in your Teachable school with a test student account. Once you complete the course, you should receive a notification in Slack:
960

As a final step, we need to add a request to the Teachable API that enrolls the student in Course B.

  1. Back in Postman, add a new request to the Enrollment Sequencing collection. We’ll call this one “Enroll in Course B.” The request is a POST request to https://developers.teachable.com/v1/enroll.
  2. In the Body tab of the request, select the raw data type option from the dropdown menu, and then select JSON as the input type. Finally, paste the below JSON data in the body window. You'll need to replace 12345 with the unique ID number of the course you want to enroll the student in after they complete course A. (i.e., the course ID number for Course B).
{
     "user_id": {{user_id}},
     "course_id": 12345
}
816
  1. Make sure to add your Teachable API key to the Header of the request as the value for apiKey. (Learn more about getting your Teachable API key).
  2. Save your request.

Once you complete the above, your automation is complete. To make sure that everything works as expected, create a test student account in your school—when you complete your enrollment in Course A, you should automatically be enrolled in Course B.

1218