Send xAPI Statements with Zapier to Any LRS

Send xAPI Statements with Zapier to Any LRS tutorial cover photo

You will not need to write any code, but you may need to modify some code slightly. We will tackle that a bit later.

Let’s get started!

Set up the Zapier Trigger

The first step to send any xAPI statement with Zapier is to set up the event that will trigger the action.

For the purposes of this tutorial, we’ll generate an xAPI statement whenever someone posts a message to the #xapi channel in my eLearning Slack community.

To do this, sign in to Zapier and select the black “MAKE A ZAP” button on the left toolbar.

Select the Make a Zap button in Zapier

In the text entry box where it asks you to choose an app and event, search for Slack. If you’re trying to send xAPI statements from a different app, then feel free to select it here.

Since we want the statements to fire whenever someone sends a message, select “New Message Posted to Channel” in the “Choose Trigger Event” dialogue box.

Choose the Zapier trigger app and event

Once the app and trigger event are selected, select the blue “CONTINUE” button. Next, follow the prompts to sign in to your Slack account (or the account for whatever app you’re using to follow along with this tutorial).

I only want this Zap to trigger when someone posts a message to the #xapi channel, so I must select that channel to finish configuring the trigger.

Choose the Slack channel that will trigger the Zap

Finally, select the blue “CONTINUE” button, select “TEST TRIGGER”, and then select the “CONTINUE” button again. Now our trigger event is complete.

Configure the “Code by Zapier” Action

Now that we’ve set up the trigger event that fires every time someone sends a message in the #xapi channel, we need to set up the action that we’d like to occur every time that happens.

To send an xAPI statement to any LRS, we must select the “Code by Zapier” action. Search for this in the “Choose App & Event” dialogue box.

Select the Code by Zapier action

Select “Code by Zapier,” then, in the “Choose Action Event” dialogue box, select “Run JavaScript.”

Select the Run JavaScript action event

Select the blue “CONTINUE” button once this is configured appropriately.

Now you’ll see the “Customize Run JavaScript” screen, which has two sections: Input Data and Code. Let’s cover each of them.

JavaScript Input Data

You use this “Input Data” section to create placeholders that you will use in the xAPI code. For example, since we want to know the user’s name who sent the message, we will create a placeholder called “name” and set it equal to the name that we get from Slack.

We can press the plus icon in this section and use the same approach to create an “email” placeholder.

Create the input data inputs

Finally, we want to include the message that the user sent. We can create a “message” placeholder” and set it equal to the “Text” value from Slack.

Add the Slack message text as input data

Now that we’ve created placeholder variables for the user’s name, email address, and message, we’re ready to work on the code itself.

JavaScript Code

First, copy and paste the code below into the “Code” box in Zapier. Big shout out to Rob Chadwick from Veracity Learning for helping me with this code. 

{% c-block language="js" %}
var statement = {
  actor: {
    mbox: "mailto:" + inputData.email,
    name: inputData.name
  },
  verb: {
    id: "https://w3id.org/xapi/acrossx/verbs/posted",
    display: {
      'en-US': "posted"
    }
  },
  object: {
    id: "https://www.devlinpeck.com/slack/channel/xapi",
    definition: {
      name: { 'en-US': "Devlin’s xAPI Slack Channel" }
    }
  },
  result: {
    response: inputData.message
  },
  timestamp: (new Date()).toISOString()
};

var endpoint = ;
var basicAuth = ;

fetch(endpoint, {
  method: 'POST',
  headers: {
    'X-Experience-API-Version': '1.0.0',
    'Content-Type': 'application/json',
    'Authorization': basicAuth
  },
  body: JSON.stringify(statement)
})
{% c-block-end %}

The code above will send an xAPI statement such as this:

“Devlin posted to Devlin’s xAPI Slack Channel. His message was: ‘The specific message will go here.’”

Notice that I used the placeholders that we created in the previous section via the inputData object. For example, to access the “name” input data that we defined, I typed inputData.name. To access the “email” input data, I typed inputData.email. And so on and so forth.

The exact code that you need for different use cases will vary depending on how you’re structuring your xAPI statement and the data that you want to include. Please read this “How to Write an xAPI Statement” tutorial that I wrote if you are unfamiliar with how to structure an xAPI statement.

When you’re modifying the code above, here are the things to look for:

  • Change the object ID and object name according to the object that people are interacting with in your trigger action. Remember, these can be placeholders that you fill in with data from the original app (using the inputData object...just as we did with name, email, and message in this example.)
  • If you only have the user’s email address (and not their name), then you can delete the “name” property in the “actor” object.
  • Delete the “result” object if you are not including a response (as we are in this example to hold the user’s message).
  • Change the verb ID and verb display depending on the action that people are performing. In this case, we are using “posted” because the user is posting the message to a channel. You can find a list of verb IDs on the xAPI Vocab Server.
  • You can add any other properties to this statement as long as they are allowed by the xAPI spec. You can create inputData properties (as we did in the last section) as needed to carry data from the "trigger" app into the xAPI statement.

If you have any questions about this or need additional guidance, then feel free to join my eLearning Slack channel and ask away.

Set the LRS Credentials

Once your xAPI statement is structured as desired, you must configure the LRS credentials to ensure that the xAPI statements make it to your LRS.

The way that you access your credentials will vary slightly by LRS, but you will need your LRS endpoint, key, and secret. Access your LRS documentation if needed.

Once you’ve located your LRS endpoint, enter it as the value for the “endpoint” variable in the code above. Add “statements” to the end of the URL, then enclose the whole URL in quotes, like so:

{% c-block language="js" %}
var endpoint = 'https://devlins-test-lrs.lrs.io/xapi/statements';
{% c-block-end %}

Next, we need to complete the “basicAuth” variable by adding the encoded LRS credentials next to where it says ‘Basic ‘. 

Locate your LRS key (or username) and secret (or password), then open your browser's console. You can do this by pressing the F12 key, or you can right click on an area of the browser, select "Inspect," then select the "Console" tab.

In the console, execute the following line of code, replacing “user” with your LRS key and “password” with your LRS secret:

{% c-block language="js" %}
btoa("user:password")
{% c-block-end %}

Press enter and you will receive the encoded string that you need to complete the “basicAuth” variable.

Encode the LRS credentials with the btoa command in the console

Now locate the “basicAuth” variable in your JavaScript. Copy the returned value from the console and paste it after the space next to “Basic,” but do not include the surrounding quotes. It should look like this (but your specific value will depend on your LRS key and secret):

{% c-block language="js" %}
var basicAuth = 'Basic dXNlcjpwYXNzd29yZA==';
{% c-block-end %}

Once you’ve updated the endpoint and basicAuth variables as shown above, your code is ready to go! Select the blue “CONTINUE” button.

Next, select the “TEST & CONTINUE” button. If you see your test data in your LRS, then you know that the code is working!

See the full xAPI statement in the Learning Record Store

You can turn the Zap on by selecting the “OFF” toggle at the bottom of the screen (which will turn it on). For me, this means that an xAPI statement will fire every time someone sends a message in my Slack community’s #xapi channel.

Conclusion 

Zapier enables you to send custom xAPI statements from thousands of different apps with ease. By adding the code provided in this tutorial and modifying it for your needs, you can use the power of Zapier with any LRS that you’d like.

If you have any questions about how to get this to work (or you’re wondering about what else is possible), then you should join my eLearning & instructional design Slack community. See you there!
You can send xAPI statements from thousands of apps with Zapier. Watershed LRS makes this easy with a native Zapier integration, but you can use Zapier with any LRS by following the steps in this tutorial.  

Featured Tutorials

View all tutorials