GetJWT()

Creates a JSON Web Token (JWT) for a JSON payload.

JWTs offer a method of securely transmitting information between parties as a JSON object. The information is digitally signed using cryptographic algorithms. Because the JWT is signed, parties that receive a JWT can be assured that the data wasn’t tampered with in transit.

We recommend that you use the companion function GetJWTByKeyName() instead of GetJWT(). The difference between GetJWTByKeyName() and GetJWT() is the first parameter. In the GetJWT() function, you pass your secret as the first parameter in plain text. With GetJWTByKeyName(), you pass a reference to a key that’s stored using the Key Management feature of Marketing Cloud Engagement. With Key Management, you can also control which users have access to your keys. These factors make the GetJWTByKeyName() function more secure for most use cases.

Important

Availability 

Marketing Cloud Engagement ✅ Yes
Marketing Cloud Next ❌ No

Syntax 

The GetJWT() function uses this syntax: GetJWT({secret}, {algorithm}, {jsonPayload})

The function has the parameters listed in this table.

NameTypeDescription
secretStringRequired. A secret used to sign the JWT.
algorithmStringRequired. The hash algorithm to use to encode the JWT. Possible values:
  • HS256—HMAC secret with SHA-256 hash
  • HS384—HMAC secret with SHA-384 hash
  • HS512—HMAC secret with SHA-512 hash
jsonPayloadStringRequired. The payload of the JWT. Typically, the payload is a JSON object with name-value pairs. The JWT payload isn’t encrypted.

Usage 

To use the function, provide a signing secret, and specify the encoding algorithm. Finally, provide the payload that you want to encode.

1%%[
2To use the function, provide a signing secret, and specify the encoding algorithm. Lastly, provide the payload that you want to encode.
3  Var @JSON
4  Var @HREF
5
6  Set @JSON = '{
7    "first_name": "Tomás",
8    "last_name": "Santos",
9    "email": "tomas.santos@example.com",
10    "campaign_id": "928384"
11  }'
12  Set @JWT = GetJWT("0123456789abcdef0123456789abcdef", "HS256", @JSON)
13  Set @HREF = CONCAT("https://www.example.com?sign=",@JWT)
14]%%
15
16<p>%%=v(@HREF)=%%</p>

The function outputs a JWT.

1eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmaXJzdF9uYW1lIjoiVG9tw6FzIiwibGFzdF9uYW1
2lIjoiU2FudG9zIiwiZW1haWwiOiJ0b21hcy5zYW50b3NAZXhhbXBsZS5jb20iLCJjYW1wYWlnbl9pZCI
36IjkyODM4NCJ9.EDh9eRIiFCndRAFcup4R2VktJJXA90Ms4M3rRL5FfZk

If the syntax for the function is invalid, the function returns an InvalidFunctionException error. If the function call is invalid—for example, if it has an invalid parameter value—it returns a FunctionExecutionException error.