For the complete documentation index, see llms.txt. This page is also available as Markdown.

JSON Web Tokens (JWTs)

A JSON Web Token, or JWT, is an open standard for securely sharing information as a JSON object. JWTs are small enough to be used in a GET or POST parameter or an HTTP header, and because they are digitally signed, the information inside can be trusted.

JWTs can be generated using a library. Options can be found on JWT.io or GitHub.

1

Collect the data object

Whether you are using a JWT with UTT or the Open Endpoints, you will need to start with the data object that you are trying to sign.

For Advocate implementations, id and accountId will always be set to the same thing.

{
  "id": "john@example.com",
  "accountId": "john@example.com",
  "firstName": "John",
  "lastName": "Doe",
  "email": "john@example.com",
  "locale": "en_US"
}
2

Assemble the JWT payload

The JWT payload structures the data trying to be signed in this format:

{  
  "user":{  
    "id": "john@example.com", //This field will be dependent on your implementation
    "accountId": "john@example.com", //This field will be dependent on your implementation
    "firstName": "John",  
    "lastName": "Doe",  
    "email": "john@example.com",  
    "locale": "en_US"  
  }  
}
3

Sign the payload

Use your chosen library to build the JWT with the payload, and sign it with your API key and Auth Token.

// Example uses jose-jwt: https://github.com/dvsekhvalnov/jose-jwt

using System.Collections.Generic;
using System.Text;
using Jose;

namespace JwtExample
{
  class Jwt
  {
    // An example for building a user JWT.
    //   accountSid/authToken - your impact.com API credentials
    //   userId - your unique identifier for this user
    public static string BuildJwt(string accountSid, string authToken, string userId, string email, string firstName, string lastName)
    {
      // Build the user payload. Most fields are optional, but id and accountId are required 
      // and are set to the same value. See the API documentation for more fields that you 
      // can add to your users.
      var userPayload = new Dictionary<string, object>() {
        { "id", userId },           // required
        { "accountId", userId },    // required
        { "firstName", firstName }, // optional
        { "lastName", lastName },   // optional
        { "email", email }          // optional
      };

      // Expiry date is optional, but recommended
      var expiryDate = ((DateTimeOffset)DateTime.UtcNow.Date.AddDays(7)).ToUnixTimeSeconds();

      var payload = new Dictionary<string, object>() {
        { "user", userPayload },
        { "exp", expiryDate }
      };

      var headers = new Dictionary<string, object>() {
        { "typ", "JWT" },
        { "kid", accountSid }
      };

      var byteSecret = Encoding.UTF8.GetBytes(authToken);

      return Jose.JWT.Encode(payload, byteSecret, JwsAlgorithm.HS256, extraHeaders: headers);
    }
  }
}

For other fields that can be included in user objects, visit the User Upsert API documentation.

4

Include the JWT with your calls

After creating a JWT, it must be included with all of your calls.

Here’s an example of how a UTT call that includes the JWT would appear:

<script>
  window.impactToken = "JWT_GOES_HERE";
</script>

<impact-embed widget="p/program-id/w/referrerWidget"></impact-embed>

Last updated