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

  1. 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).

  • immediateResults ImmediateResult[] 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

  1. 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

  1. 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.getOptInUrl(userId);

Params

  1. userId string The user's primary key or unique ID in your system.

Returns

  • url string The opt out URL.

Last updated