TinCanJS (Experience API Library)

0.50.0 · active · verified Tue Apr 21

TinCanJS is a JavaScript library designed for implementing the Experience API (xAPI, formerly Tin Can API) in both web browser and Node.js environments. The current stable version is 0.50.0. While there isn't a fixed release cadence, the project sees regular updates addressing bug fixes, enhancing LRS interaction (e.g., retrieving activity or profile data), and adding new features like end-to-end statement attachment support. Key differentiators include its broad browser compatibility, supporting older IE versions (IE8+ for CORS, IE6+ for non-CORS) and modern browsers, as well as providing a unified API for cross-origin requests. It abstracts away environmental differences, offering a consistent interface for constructing, sending, and retrieving xAPI statements and interacting with Learning Record Stores (LRSs).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize an LRS connection and send a basic xAPI statement, followed by querying statements. Uses environment variables for sensitive LRS credentials.

const TinCan = require('tincanjs');

const lrs = new TinCan.LRS({
    endpoint: process.env.LRS_ENDPOINT ?? 'https://cloud.scorm.com/tc/public/', // Example endpoint
    username: process.env.LRS_USERNAME ?? 'YOUR_USERNAME',
    password: process.env.LRS_PASSWORD ?? 'YOUR_PASSWORD',
    version: '1.0.3' // Specify the xAPI version, e.g., '1.0.3'
});

const actor = new TinCan.Agent({
    name: 'Test User',
    mbox: 'mailto:test.user@example.com'
});

const verb = new TinCan.Verb({
    id: 'http://adlnet.gov/expapi/verbs/experienced',
    display: { 'en-US': 'experienced' }
});

const activity = new TinCan.Activity({
    id: 'http://example.com/activities/tincanjs-test',
    definition: {
        name: { 'en-US': 'TinCanJS Test Activity' },
        description: { 'en-US': 'A test activity for TinCanJS quickstart.' }
    }
});

const statement = new TinCan.Statement({
    actor: actor,
    verb: verb,
    object: activity
});

lrs.saveStatement(statement, function (err, xhr) {
    if (err) {
        console.error('Failed to save statement:', err);
        return;
    }
    console.log('Statement saved successfully with ID:', JSON.parse(xhr.responseText)[0]);
});

// Example of retrieving statements
lrs.queryStatements({}, function (err, result) {
    if (err) {
        console.error('Failed to query statements:', err);
        return;
    }
    console.log('Queried statements count:', result.statements.length);
});

view raw JSON →