OpenTok Python SDK

3.13.0 · active · verified Sat Apr 11

The OpenTok Python SDK provides server-side functionality for interacting with the OpenTok platform, which is part of the Vonage Video API. It enables developers to generate sessions and tokens for OpenTok applications, manage archiving of video sessions, and utilize other core video functionalities. The library is actively maintained, with the current version being 3.13.0, and receives regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the OpenTok Client, create a new session, and generate a client token. It's crucial to set your `OPENTOK_API_KEY` and `OPENTOK_API_SECRET` as environment variables for secure and functional use. The example creates a 'routed' session, which is necessary for features like archiving. A token is then generated for connecting a client to this session.

import os
from opentok import Client, MediaModes

# Replace with your OpenTok API key and secret
API_KEY = os.environ.get('OPENTOK_API_KEY', 'YOUR_API_KEY')
API_SECRET = os.environ.get('OPENTOK_API_SECRET', 'YOUR_API_SECRET')

if not API_KEY or not API_SECRET or API_KEY == 'YOUR_API_KEY':
    print("WARNING: Please set OPENTOK_API_KEY and OPENTOK_API_SECRET environment variables or replace placeholders.")
    exit(1)

try:
    # Initialize an OpenTok Client instance
    opentok_client = Client(API_KEY, API_SECRET)

    # Create a session that uses the OpenTok Media Router
    # The routed mode is required for features like archiving.
    # For a relayed (peer-to-peer) session, use MediaModes.relayed or omit parameter for default.
    session = opentok_client.create_session(media_mode=MediaModes.routed)

    session_id = session.session_id
    print(f"Session ID: {session_id}")

    # Generate a token for a publisher role in this session
    # By default (since v3.10.0), JWTs are generated. For legacy T1 tokens, use `use_jwt=False`.
    token = opentok_client.generate_token(session_id)
    print(f"Generated Token: {token}")

    # In a real application, you would send `session_id` and `token` to your client-side application.

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →