Send xAPI Statements

This tutorial will walk you through the essential steps to send an xAPI statement to LearnCloud Storage and then read it back. We'll keep it simple so you can get up and running quickly!

What you'll accomplish:

  • Construct a basic xAPI statement.

  • Send the statement to the LearnCloud xAPI endpoint.

  • Retrieve and verify the statement you sent.

Prerequisites:

  1. Understanding Key Concepts:

    • What is xAPI? xAPI (Experience API) is a way to track learning experiences using a simple "Actor - Verb - Object" structure (e.g., "Sarah completed 'Safety Course'"). For a deeper dive, see our Understanding xAPI Data in LearnCard core concept page.

    • What is a DID? A DID (Decentralized Identifier) is a unique identifier for your user. Think of it as a secure, private digital ID. More details can be found on our Understanding DIDs core concept page.

  2. Your Environment:

    • You have the LearnCard SDK initialized in your project.

    • You have obtained a JSON Web Token (JWT) for authentication. This JWT represents the authenticated user (the "actor"). As an example of how to create this JWT, check out the "Create a Connected Website Tutorial."

    • You have the DID of the authenticated user.

    • The default LearnCloud xAPI endpoint is https://cloud.learncard.com/xapi/statements.


Part 1: Sending an xAPI Statement

Let's send a statement indicating a user has attempted a challenge in a game.

1

Define Your xAPI Statement

An xAPI statement has three main parts: an actor (who did it), a verb (what they did), and an object (what they did it to).

✨ Good to know:

  • DID Usage: For LearnCloud, ensure the userDid is used in both actor.name and actor.account.name.

  • Verb Selection: Use standard xAPI verb URIs when possible. You can find lists of common verbs online (e.g., on the ADLNet website).

  • Activity IDs: Make your object.id URIs unique for each distinct activity. They don't need to be real, live URLs.

2

Prepare and Send the Statement

We'll use a Workspace request to send this statement. The key things are the POST method, correct headers, and the statement in the body.

3

Check the Response

If successful, the LearnCloud Storage API will typically return an HTTP status like 200 OK or 204 No Content. Often, a 200 OK response to a POST will include an array containing the unique ID(s) of the statement(s) just stored. Our sendStatement function logs this.


Part 2: Reading xAPI Statements

Now that we've sent a statement, let's try to read statements for that user.

1

Prepare Your Request Parameters

To read statements, you'll usually query for statements related to a specific agent (the actor). The agent parameter must be a JSON string.

2

Make the API Call to Read Statements

This will be a GET request.

3

Process the Response

The response from a GET request to the /statements endpoint will be a JSON object. This object typically contains an array called statements and optionally a more property. The more property provides a URL to fetch the next page of results if pagination is active.


Important Considerations (Recap)

  • Authentication (X-VP Header): All requests to the LearnCloud xAPI endpoint must include a valid JWT in the X-VP header.

  • Permissions:

    • Users can only send statements where they are the actor (or have delegated authority).

    • Users can typically only read statements where they are the actor. The DID in your JWT (X-VP header) must match the actor's DID you are querying for. A 401 Unauthorized error often means a DID mismatch or an invalid/expired JWT.

  • Error Handling: Always check response statuses and handle potential errors from the API or network issues.

  • Delegated Access: For scenarios where another party needs to read or write statements on behalf of a user, LearnCloud supports a delegated access mechanism using Verifiable Credentials. (See Delegated Access for more info).

  • Contract-Scoped Statements: When using ConsentFlow, xAPI statements can be automatically tagged with the contract URI, enabling queries by contract. (See Contract-Scoped xAPI Statements for details).

  • Voiding Statements: You can invalidate previously sent statements. (See Advanced Topics: Voiding Statements for how).


Next Steps

Congratulations! You've now seen how to send and read basic xAPI statements with LearnCloud.

From here, you can explore:

  • Sending different types of xAPI statements (e.g., completed, mastered, with result objects).

  • Using the other examples provided in our xAPI Concepts Guide.

  • Implementing more advanced queries to filter and retrieve statements. (See Advanced xAPI Statement Queries).

  • Connecting these xAPI statements as evidence for Verifiable Credentials.

Happy tracking!

Last updated

Was this helpful?