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>');
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
signal
object An object containing the following subset of Signal 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 Identity in DFend.
Returns
A Promise resolving with a response
object containing the following fields:
started
number A Unix timestamp (ms).
finished
number A Unix timestamp (ms).
checkOptedIn async
Check whether a user has opted into DFend.
Example
const userId = 'qkHIslwhyFxHVvNg'; // user.id
const { optedIn } = await dfend.checkOptedIn({ userId });
Params
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 Identity in DFend.
Returns
A Promise resolving with a response
object containing the following fields:
optedIn
boolean A boolean indicating whether the user has opted in.
getOptInUrl
Get the URL for the page where a user can opt into DFend.
Example
const userId = 'qkHIslwhyFxHVvNg'; // user.id
const url = dfend.getOptInUrl(userId);
Params
userId
string The user's primary key or unique ID in your system.
Returns
url
string The opt in URL.
getOptOutUrl
Get the URL for the page where a user can opt out of DFend.
Example
const userId = 'qkHIslwhyFxHVvNg'; // user.id
const url = dfend.getOptOutUrl(userId);
Params
userId
string The user's primary key or unique ID in your system.
Returns
url
string The opt out URL.