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.

  • View xAPI statements in the LearnCard app's Activity Feed.

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.


Part 3: Viewing xAPI Statements in the LearnCard App

Once xAPI statements have been sent, users can view their activity data directly in the LearnCard app. This provides a user-friendly way to see all learning activities associated with a specific app or contract.

1

From the LearnCard app:

  1. Tap on your profile picture in the top corner

  2. Select "Manage Data Sharing" to see all your consented apps and contracts

2

Select a Contract

In the Data Sharing view, you'll see a list of apps you've consented to share data with. Tap on any contract to view its details.

3

Open the Activity Feed

In the contract detail view, look for the "xAPI Data Feed" button. Tap it to open the Activity Feed modal.

4

View Your Activity Data

The Activity Feed displays all xAPI statements associated with that specific contract, including:

  • Verb badges (e.g., "completed", "attempted", "mastered") with color-coded styling

  • Activity name and description

  • Timestamps showing when each activity occurred

  • Result data (scores, completion status, success/failure)

  • Raw JSON view for developers who want to inspect the full statement

The feed supports infinite scrolling, so you can browse through all your historical activity data.

circle-info

For Developers: The Activity Feed filters statements by the contractUri extension in context.extensions. When you send xAPI statements with a contract URI (as shown in Contract-Scoped xAPI Statements), they will automatically appear in this feed for the associated contract.


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, read, and view 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).

  • Using Contract-Scoped xAPI Statements to associate activity data with specific apps.

  • Connecting these xAPI statements as evidence for Verifiable Credentials.

Happy tracking!

Last updated

Was this helpful?