JS SDK

Learn about the DFend JS SDK.

circle-info

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>');
circle-exclamation

Methods

chevron-rightpush asynchashtag

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.

chevron-rightcheckOptedIn asynchashtag

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.

chevron-rightgetOptInUrlhashtag

Get the URL for the page where a user can opt into DFend.

Example

Params

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

Returns

  • url string The opt in URL.

chevron-rightgetOptOutUrlhashtag

Get the URL for the page where a user can opt out of DFend.

Example

Params

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

Returns

  • url string The opt out URL.

Last updated