JS SDK
Learn about the DFend JS SDK.
Currently only supports use in web browser environments. Please contact Support if you are interested in using the JS SDK in another environment.
The DFend JS SDK is a lightweight client library that makes it easy to interact with the DFend API from a JavaScript environment.
Load the SDK
<script defer="defer" src="https://js.dfend.app/sdk/v0/client.js"></script>Create an instance
const dfend = DFend('<Publishable API Key>');Do not use the new keyword.
Methods
push async
Send us a Signal.
Example
const userId = 'qkHIslwhyFxHVvNg'; // user.id
const { immediateResults } = await dfend.push({
idempotencyKey: `${userId}-${new Date().getTime()}`,
type: 'PageLoad',
identifiers: {
userId
}
});Params
signalobject An object containing the following subset of Signal fields that are suitable for sending from a client environment:idempotencyKeystring Prevent duplicate events by providing a unique key for this signal.typestring A custom string value representing the type of event.identifiersobject An object containing at least one of the following fields:userIdstring The user's primary key or unique ID in your system.identitystring Theidvalue of the user's Identity in DFend.
Returns
A Promise resolving with a response object containing the following fields:
startednumber A Unix timestamp (ms).finishednumber A Unix timestamp (ms).immediateResultsImmediateResult[] An array of ImmediateResult objects.
checkOptedIn async
Check whether a user has opted into DFend.
Example
const userId = 'qkHIslwhyFxHVvNg'; // user.id
const { optedIn } = await dfend.checkOptedIn({ userId });Params
identifiersobject An object containing at least one of the following fields:userIdstring The user's primary key or unique ID in your system.identitystring Theidvalue of the user's Identity in DFend.
Returns
A Promise resolving with a response object containing the following fields:
optedInboolean A boolean indicating whether the user has opted in.
Last updated