xAPI.js
  • xAPI.js
  • xAPI Wrapper Library
    • Introduction
    • Getting Started
    • The XAPI Class
    • Statement Resource
    • State Resource
    • Activity Profile Resource
    • Agent Profile Resource
    • About Resource
    • Agents Resource
    • Activities Resource
    • Helpers
    • Verbs
    • Attachment Usages
  • cmi5 Profile Library
    • Introduction
    • Getting Started
    • The Cmi5 Class
    • "cmi5 defined" Statement methods
    • "cmi5 allowed" Statement methods
    • Helpers
Powered by GitBook
On this page
  • UMD (Browser, client-side applications)
  • ES Module (Browser, client-side applications)
  • CJS (Server-side applications)

Was this helpful?

  1. xAPI Wrapper Library

Getting Started

How to get the library up and running in your project.

The library has been configured to work in multiple environments:

  • UMD (Browser, client-side applications)

  • ESM (Browser, client-side applications)

  • CJS (Server-side applications)

UMD (Browser, client-side applications)

For browser-based applications you can link the UMD package directly from unpkg.com in a script tag.

<script src="https://unpkg.com/@xapi/xapi"></script>

<script type="text/javascript">
  // Create LRS connection
  const endpoint = "https://my-lms.com/endpoint";
  const username = "username";
  const password = "password";
  const auth = XAPI.toBasicAuth(username, password);
  const xapi = new XAPI({
    endpoint: endpoint,
    auth: auth
  });

  // Create your statement
  const myStatement = { ... };

  // Send your statement
  xapi.sendStatement({
    statement: myStatement
  });
</script>

ES Module (Browser, client-side applications)

For client-side applications built using imports, an ES Module is available for you to install and import into your project.

npm i @xapi/xapi --save
import XAPI, { Statement } from "@xapi/xapi";

// Create LRS connection
const endpoint = "https://my-lms.com/endpoint";
const username = "username";
const password = "password";
const auth = XAPI.toBasicAuth(username, password);
const xapi: XAPI = new XAPI({
  endpoint: endpoint,
  auth: auth
});

// Create your statement
const myStatement: Statement = { ... };

// Send your statement
xapi.sendStatement({
  statement: myStatement
});

CJS (Server-side applications)

For server-side applications built using require, a CommonJS module is available for you to install and require into your project.

npm i @xapi/xapi --save
const XAPI = require("@xapi/xapi");

const endpoint = "https://my-lms.com/endpoint";
const username = process.env.LRS_USERNAME || "";
const password = process.env.LRS_PASSWORD || "";
const auth = XAPI.toBasicAuth(username, password);
const xapi = new XAPI({
  endpoint: endpoint,
  auth: auth
});

// Create your statement
const myStatement = { ... };

// Send your statement
xapi.sendStatement({
  statement: myStatement
});

Alternatively, the module can also be imported using .mjs.

import XAPI from "@xapi/xapi";

const endpoint = "https://my-lms.com/endpoint";
const username = process.env.LRS_USERNAME || "";
const password = process.env.LRS_PASSWORD || "";
const auth = XAPI.toBasicAuth(username, password);
const xapi = new XAPI({
  endpoint: endpoint,
  auth: auth
});

// Create your statement
const myStatement = { ... };

// Send your statement
xapi.sendStatement({
  statement: myStatement
});
PreviousIntroductionNextThe XAPI Class

Last updated 3 years ago

Was this helpful?