Token generator examples

|

View as Markdown

Use these examples to generate a personalization token for web authentication. Each example builds the payload described in JWT payload structure and signs it with your widget secret key.

Examples by language [#examples-by-language]

Each example uses the sample values from the token examples, with the following exceptions:

  • jti: Each example generates a new value for every token. Live Chat rejects a token whose jti was already used for the widget.
  • sid: Each example takes the ID of the current user session from your system. Each user session needs its own sid.
IMPORTANT

Generate and sign the token on your server. Never put the secret key in your website or app code.

The following tabs contain a JWT generator example in Node.js, Java, Kotlin, Swift, and PHP:

json
1const crypto = require('crypto');
2const jwt = require('jsonwebtoken');
3 
4const securityKey = JSON.parse(
5'{"id":"43f7a6d6-f064-4a6b-8890-fff70b1363ca","key":"0wQGQwukZLkJNShlHS0Nm3SCiIwrWNfm7NEPOhukJhM="}'
6);
7 
8const now = Math.floor(Date.now() / 1000);
9 
10const payload = {
11 iat: now,
12 iss: 'da73dfca-7e33-43de-9e97-ece90aaada22',
13 jti: crypto.randomUUID(),
14 ski: securityKey.id,
15 stp: 'email',
17 exp: now + 15,
18 sid: sessionId // ID of the current user session in your system
19};
20 
21const token = jwt.sign(payload, Buffer.from(securityKey.key, 'base64'));
22 
23console.log(token);
NOTE

Decode your secret key from Base64 before you use it for the signature.



Related pages

Authenticate web users
Review token requirements and the JWT payload structure.

Authenticate mobile users
Use the personalization token for additional security in mobile apps.

JavaScript SDK in Live Chat
Submit the generated token to the web widget with the Auth command.


Was this page helpful?