OpenTok Token Encoder

1.1.1 · abandoned · verified Wed Apr 22

The `opentok-token` package is a low-level utility designed to generate `X-TB-TOKEN-AUTH` headers for authentication with the OpenTok REST API. As of its current stable version, 1.1.1, it focuses solely on the encoding process, transforming provided token data, an API key, and an API secret into a JWT string. A critical characteristic of this package is its explicit lack of input validation; it does not check if the provided data aligns with OpenTok REST API semantics, allowing for the generation of syntactically correct but functionally invalid tokens. It offers default values for `create_time`, `expire_time`, `role`, and `nonce` if these properties are omitted from the token data. Its primary distinction is its role as a simple encoder, contrasting with the more comprehensive `opentok-node` server SDK, which provides a full suite of API interactions including data validation. Given its last update several years ago, the package is effectively no longer maintained, indicating an inactive release cadence.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to generate an OpenTok API token using `opentok-token`, including setting a role and connection data, with a note on required API credentials.

const encodeToken = require('opentok-token');

// IMPORTANT: Replace with your actual OpenTok API Key and Secret
const apiKey = process.env.OPENTOK_API_KEY ?? 'YOUR_API_KEY';
const apiSecret = process.env.OPENTOK_API_SECRET ?? 'YOUR_API_SECRET';
const sessionId = 'SESSIONID_FROM_OPENTOK'; // Replace with a valid OpenTok Session ID

// Token data for a moderator role, expiring in 24 hours
const tokenData = {
  session_id: sessionId,
  // create_time and nonce will default to now and a random number if not specified
  role: 'moderator',
  // expire_time will default to now + 1 day if not specified
  connection_data: JSON.stringify({ 'user_id': 'user123', 'level': 'gold' })
};

try {
  const token = encodeToken(tokenData, apiKey, apiSecret);
  console.log('Generated OpenTok Token:', token);
  // You would typically use this token in the X-TB-TOKEN-AUTH header
  // when making requests to the OpenTok REST API.
} catch (error) {
  console.error('Error generating token:', error.message);
}

view raw JSON →