Statement Resource
Manages learner statements.

sendStatement

Sends a statement to the LRS.

Examples

Example 1: Send Basic Statement

1
import XAPI, { Statement } from "@xapi/xapi";
2
3
// new XAPI() etc
4
5
const myStatement: Statement = {
6
actor: {
7
objectType: "Agent",
8
name: "Test Agent",
9
mbox: "mailto:[email protected]"
10
},
11
verb: {
12
id: "http://example.com/verbs/tested",
13
display: {
14
"en-GB": "tested"
15
}
16
},
17
object: {
18
objectType: "Activity",
19
id: "https://github.com/xapijs/xapi"
20
}
21
};
22
23
xapi.sendStatement({
24
statement: myStatement
25
});
Copied!

Example 2: Send Embedded Attachment Statement

1
import CryptoJS from "crypto-js";
2
import { arrayBufferToWordArray } from "...";
3
4
const attachmentContent: string = "hello world";
5
const arrayBuffer: ArrayBuffer = new TextEncoder().encode(attachmentContent);
6
7
const myStatement: Statement = {
8
// actor, verb, object etc,
9
attachments: [{
10
usageType: XAPI.AttachmentUsages.SUPPORTING_MEDIA,
11
display: {
12
"en-US": "Text Attachment"
13
},
14
description: {
15
"en-US": `The text attachment contains "${attachmentContent}"`
16
},
17
contentType: "text/plain",
18
length: arrayBuffer.byteLength,
19
sha2: CryptoJS.SHA256(arrayBufferToWordArray(arrayBuffer)).toString()
20
}]
21
}
22
23
xapi.sendStatement({
24
statement: myStatement,
25
attachments: [arrayBuffer]
26
});
Copied!
This example requires CryptoJS to generate a sha2 of the attachment data and convert the array buffer to a word array.

Parameters

Parameter
Type
Required
Description
statement
Statement
true
The statement you wish to send to the LRS.
attachments
ArrayBuffer[]
false
An array of attachment data converted to ArrayBuffer.

Returns

This method returns an AxiosPromise with the success data containing a string array of statement IDs if successful, or if unsuccessful the rejection contains an error message.

sendStatements

Sends multiple statements to the LRS.

Example 1: Send Basic Statements

1
import XAPI, { Statement } from "@xapi/xapi";
2
3
// new XAPI() etc
4
5
const myStatement: Statement = {
6
actor: {
7
objectType: "Agent",
8
name: "Test Agent",
9
mbox: "mailto:[email protected]"
10
},
11
verb: {
12
id: "http://example.com/verbs/tested",
13
display: {
14
"en-GB": "tested"
15
}
16
},
17
object: {
18
objectType: "Activity",
19
id: "https://github.com/xapijs/xapi"
20
}
21
};
22
23
// Define another statement
24
const myStatementTwo: Statement = {
25
...myStatement
26
};
27
28
xapi.sendStatements({
29
statements: [myStatement, myStatementTwo]
30
});
Copied!

Example 2: Send Embedded Attachment Statements

1
import CryptoJS from "crypto-js";
2
import { arrayBufferToWordArray } from "...";
3
4
const attachmentContent: string = "hello world";
5
const arrayBuffer: ArrayBuffer = new TextEncoder().encode(attachmentContent);
6
7
const myStatement: Statement = {
8
// actor, verb, object etc,
9
attachments: [{
10
usageType: XAPI.AttachmentUsages.SUPPORTING_MEDIA,
11
display: {
12
"en-US": "Text Attachment"
13
},
14
description: {
15
"en-US": `The text attachment contains "${attachmentContent}"`
16
},
17
contentType: "text/plain",
18
length: arrayBuffer.byteLength,
19
sha2: CryptoJS.SHA256(arrayBufferToWordArray(arrayBuffer)).toString()
20
}]
21
}
22
23
// Define another statement
24
const myStatementTwo: Statement = {
25
...myStatement
26
};
27
28
// Send through the attachments in the same sequence as they
29
// appear in the statements, for brevity we are using the same
30
// attachment here twice
31
32
xapi.sendStatement({
33
statements: [myStatement, myStatementTwo],
34
attachments: [arrayBuffer, arrayBuffer]
35
});
Copied!
This example requires CryptoJS to generate a sha2 of the attachment data and convert the array buffer to a word array.

Parameters

Parameter
Type
Required
Description
statements
true
The statements you wish to send to the LRS.
attachments
ArrayBuffer[]
false
An array of attachment data converted to ArrayBuffer.

Returns

This method returns an AxiosPromise with the success data containing a string array of statement IDs if successful, or if unsuccessful the rejection contains an error message.

getStatement

To receive a single statement, you must use the getStatement method and pass the statement ID in the query. Optionally, you can provide additional parameters to the query to change the data format returned from the LRS.

Examples

Example 1: Get Basic Statement

1
xapi.getStatement({
2
statementId: "abcdefgh-1234"
3
}).then((result: AxiosResponse<Statement>) => {
4
const statement: Statement = result.data;
5
// do stuff with `statement`
6
});
Copied!

Example 2: Get Embedded Attachment Statement

1
xapi.getStatement({
2
statementId: "abcdefgh-1234",
3
attachments: true
4
}).then((result: AxiosResponse<StatementResponseWithAttachments>) => {
5
const parts: [Statement, ...Part[]] = result.data;
6
const statement: Statement = parts[0];
7
const attachment: unknown = parts[1];
8
})
Copied!

Parameters

Parameter
Type
Required
Description
statementId
string
true
The identifier of the statement to request.
attachments
boolean
false
Whether to return the attachment(s) associated with the statement, if any.
format
string
false
What human readable names and descriptions are included in the statement.
  • exact format returns the statements exactly as they were received by the LRS.
  • ids only ids are returned with none of the human readable descriptions.
  • canonical format requests the LRS to return its own internal definitions of objects, rather than those provided in the statement.

Returns

This method returns an AxiosPromise with the success data containing the Statement of the supplied statementId.
When getting a statement with attachments: true, the success data is returned as an array. The first array item is of type Statement, and the following are of type Part and are the attachments supplied with the statement.

getStatements

To receive an array of statements based upon a query, you must use the getStatements method. See the GetStatementsQuery interface for a full list of ways to create your query.

Example

1
const myAgent: Agent = {...};
2
3
xapi.getStatements({
4
agent: myAgent
5
}).then((result: AxiosResponse<StatementsResponse>) => {
6
const statementsResponse: StatementsResponse = result.data;
7
// do stuff with `statementsResponse.statements`
8
});
Copied!

Parameters

Parameter
Type
Required
Description
agent
Agent
false
The agent to filter the query by.
verb
string
false
The verb identifier to filter the query by.
activity
string
false
The activity identifier to filter the query by.
registration
string
false
The registration to filter the query by.
related_activities
boolean
false
Whether to include any activity in the statement as part of the filter to query by.
related_agents
boolean
false
Whether to include any agent in the statement as part of the filter to query by.
since
Timestamp (ISOString)
false
Filters only statements after the given Timestamp.
until
Timestamp (ISOString)
false
Filters only statements before the given Timestamp.
limit
number
false
Number of statements to return.
ascending
boolean
false
Determines whether to return the statements in ascending order.
attachments
boolean
false
Determines whether to return the attachments for the resulting statements.

Returns

This method returns an AxiosPromise with the success data containing a StatementsResponse object.
When getting statements with attachments: true, the success data is returned as an array. The first array item is of type StatementsResponse, and the following are of type Part and are the attachments supplied with the statement.

getMoreStatements

To be used in conjunction with getStatements. If the more property is populated on your initial request, more data is available. Send the value of the more property to this method to get the next page of statements.

Example

1
const myAgent: Agent = {...};
2
3
xapi.getStatements({
4
agent: myAgent
5
}).then((result: AxiosReponse<StatementsResponse>) => {
6
// data
7
xapi.getMoreStatements({
8
more: result.data.more
9
}).then((result: AxiosResponse<StatementsResponse>) => {
10
// more data
11
});
12
});
Copied!

Parameters

Parameter
Type
Required
Description
more
string
true
The more property passed from the getStatements/getMoreStatements query response.

Returns

This method returns an AxiosPromise with the success data containing a StatementsResponse object or an Array containing StatementsResponse and Part if the original query has attachments: true. When working in TypeScript you may need to cast your result.data return type explicitly as StatementsResponse or StatementsResponseWithAttachments.

voidStatement

Voids a statement in the LRS by the supplied Actor.

Example

1
const actor: Actor = { ... };
2
xapi.voidStatement({
3
actor: actor,
4
statementId: "abcdefgh-1234"
5
});
Copied!
Parameters
Parameter
Type
Required
Description
actor
Actor
true
The Actor who is voiding the statement e.g. an administrator.
statementId
string
true
The statement to be voided.

Returns

This method returns an AxiosPromise with the success data containing an array of statement ID strings of the void statement.

voidStatements

Voids multiple statements in the LRS by the supplied Actor.

Example

1
const actor: Actor = { ... };
2
xapi.voidStatements({
3
actor: actor,
4
statementIds: ["abcdefgh-1234", "ijklmnop-5678"]
5
});
Copied!
Parameters
Parameter
Type
Required
Description
actor
Actor
true
The Actor who is voiding the statement e.g. an administrator.
statementIds
string[]
true
The statements to be voided.

Returns

This method returns an AxiosPromise with the success data containing an array of statement ID strings of the void statements.

getVoidedStatement

To receive a single voided statement, you must use the getVoidedStatement method and pass the original statement ID in the query (not the original statement's void statement id). Optionally, you can provide additional parameters to the query to change the data format returned from the LRS.

Example

1
xapi.getVoidedStatement({
2
statementId: "abcdefgh-1234"
3
}).then((result: AxiosResponse<Statement>) => {
4
const voidStatement: Statement = result.data;
5
// do stuff with `voidStatement`
6
});
Copied!

Parameters

Parameter
Type
Required
Description
voidedStatementId
string
true
The identifier of the voided statement to request.
attachments
boolean
false
Whether to return the attachment(s) associated with the voided statement, if any.
format
string
false
What human readable names and descriptions are included in the voided statement.
  • exact format returns the statements exactly as they were received by the LRS.
  • ids only ids are returned with none of the human readable descriptions.
  • canonical format requests the LRS to return its own internal definitions of objects, rather than those provided in the statement.

Returns

This method returns an AxiosPromise with the success data containing the Statement of the supplied voidedStatementId.
When getting a statement with attachments: true, the success data is returned as an array. The first array item is of type Statement, and the following are of type Part and are the attachments supplied with the statement.
Last modified 1mo ago