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:
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.
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.
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
userDidis used in bothactor.nameandactor.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.idURIs unique for each distinct activity. They don't need to be real, live URLs.
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.
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.
Make the API Call to Read Statements
This will be a GET request.
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-VPHeader): All requests to the LearnCloud xAPI endpoint must include a valid JWT in theX-VPheader.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-VPheader) must match the actor's DID you are querying for. A401 Unauthorizederror 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, withresultobjects).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?