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.
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"
}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"
}
}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.
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>For Open Endpoint API calls, the JWT must be included as a header with the key X-SaaSquatch-User-Token. cURL uses the -H flag to pass an extra header. You may specify any number of extra headers.
curl -X POST 'https://app.referralsaasquatch.com/api/v1/{tenant_alias}/open/account/{accountId}/user/{userId}' \
-H "X-SaaSquatch-User-Token: {X-SaaSquatch-User-Token}" \
-H "Content-Type: application/json" \
-d '{
"id": "john@example.com",
"accountId": "john@example.com",
"email": "john@example.com",
"firstName": "John",
"lastName": "Testerson",
"locale": "en_US",
"referralCode": "JOHNTESTERSON"
}'Last updated

