The Chat API token is crucial for authenticating API requests.
You can generate the token directly from your backend. Here is an example in various languages:
// Example code for generating a token in Node.js
const jwt = require('jsonwebtoken');
function generateChatApiToken(appId, signingKey, userIdentity) {
const payload = {
iss: appId,
sub: userIdentity,
exp: Math.floor(Date.now() / 1000) + (365 * 24 * 60 * 60) // 1 year
};
try {
const token = jwt.sign(payload, signingKey);
return token;
} catch (err) {
console.error('Error generating token:', err);
return null;
}
}
// Example usage
const appId = 'your_app_id';
const signingKey = 'your_signing_key';
const userIdentity = 'user_identity';
const token = generateChatApiToken(appId, signingKey, userIdentity);
console.log('Generated Token:', token);