JS SDK
Learn about the DFend JS SDK.
Last updated
Learn about the DFend JS SDK.
Last updated
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.
<script defer="defer" src="https://js.dfend.app/sdk/v0/client.js"></script>
const dfend = DFend('<Publishable API Key>');
Do not use the new
keyword.
Send us a Signal.
const userId = 'qkHIslwhyFxHVvNg'; // user.id
const { immediateResults } = await dfend.push({
idempotencyKey: `${userId}-${new Date().getTime()}`,
type: 'PageLoad',
identifiers: {
userId
}
});
signal
object An object containing the following subset of fields that are suitable for sending from a client environment:
idempotencyKey
string Prevent duplicate events by providing a unique key for this signal.
type
string A custom string value representing the type of event.
identifiers
object An object containing at least one of the following fields:
userId
string The user's primary key or unique ID in your system.
identity
string The id
value of the user's in DFend.
A Promise resolving with a response
object containing the following fields:
started
number A Unix timestamp (ms).
finished
number A Unix timestamp (ms).
immediateResults
ImmediateResult[] An array of objects.
Check whether a user has opted into DFend.
const userId = 'qkHIslwhyFxHVvNg'; // user.id
const { optedIn } = await dfend.checkOptedIn({ userId });
identifiers
object An object containing at least one of the following fields:
userId
string The user's primary key or unique ID in your system.
identity
string The id
value of the user's in DFend.
A Promise resolving with a response
object containing the following fields:
optedIn
boolean A boolean indicating whether the user has opted in.