{"id":4159,"library":"opentok","title":"OpenTok Python SDK","description":"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.","status":"active","version":"3.13.0","language":"en","source_language":"en","source_url":"https://github.com/opentok/Opentok-Python-SDK/","tags":["video","rtc","vonage","communication","streaming"],"install":[{"cmd":"pip install opentok","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for making HTTP requests to the OpenTok API.","package":"requests","optional":false},{"reason":"Used for generating JSON Web Tokens (JWTs) for authentication and token generation.","package":"PyJWT","optional":false}],"imports":[{"note":"The primary class was renamed from `OpenTok` to `Client` in version 3.x.x. Using `OpenTok` will raise an `AttributeError` or `ImportError` in recent versions.","wrong":"from opentok import OpenTok","symbol":"Client","correct":"from opentok import Client"},{"note":"Used to specify the media routing mode when creating a session (e.g., `MediaModes.routed`).","symbol":"MediaModes","correct":"from opentok import MediaModes"}],"quickstart":{"code":"import os\nfrom opentok import Client, MediaModes\n\n# Replace with your OpenTok API key and secret\nAPI_KEY = os.environ.get('OPENTOK_API_KEY', 'YOUR_API_KEY')\nAPI_SECRET = os.environ.get('OPENTOK_API_SECRET', 'YOUR_API_SECRET')\n\nif not API_KEY or not API_SECRET or API_KEY == 'YOUR_API_KEY':\n    print(\"WARNING: Please set OPENTOK_API_KEY and OPENTOK_API_SECRET environment variables or replace placeholders.\")\n    exit(1)\n\ntry:\n    # Initialize an OpenTok Client instance\n    opentok_client = Client(API_KEY, API_SECRET)\n\n    # Create a session that uses the OpenTok Media Router\n    # The routed mode is required for features like archiving.\n    # For a relayed (peer-to-peer) session, use MediaModes.relayed or omit parameter for default.\n    session = opentok_client.create_session(media_mode=MediaModes.routed)\n\n    session_id = session.session_id\n    print(f\"Session ID: {session_id}\")\n\n    # Generate a token for a publisher role in this session\n    # By default (since v3.10.0), JWTs are generated. For legacy T1 tokens, use `use_jwt=False`.\n    token = opentok_client.generate_token(session_id)\n    print(f\"Generated Token: {token}\")\n\n    # In a real application, you would send `session_id` and `token` to your client-side application.\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Update imports to `from opentok import Client` and instantiate with `Client(api_key, api_secret)`.","message":"The main class for interacting with the SDK was renamed from `OpenTok` to `Client` in version 3.x.x. Code using `from opentok import OpenTok` or `OpenTok.OpenTok()` will fail in versions 3.0 and above.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"If your client-side applications or existing logic explicitly requires T1 tokens, you must now call `opentok_client.generate_token(session_id, use_jwt=False)`. Most modern client SDKs support JWTs.","message":"As of v3.10.0, the `Client.generate_token()` method now generates JWT (JSON Web Token) formatted tokens by default. Previously, it generated T1 tokens by default.","severity":"breaking","affected_versions":">=3.10.0"},{"fix":"Determine whether you are building for the legacy OpenTok platform or the unified Vonage Video API. If the latter, consider using the `vonage` Python SDK and following its migration guide.","message":"The `opentok` Python SDK is specifically for the legacy Tokbox/OpenTok platform, even though OpenTok is now part of the Vonage Video API. For new projects or migrations to the unified Vonage platform, it is recommended to use the `vonage` Python SDK, which has a different import path (`from vonage import Vonage` then `vonage_client.video`) and uses different authentication (application ID and private key instead of API key/secret).","severity":"gotcha","affected_versions":"All versions"},{"fix":"If you need features like archiving or server-side media processing, always explicitly set `media_mode=MediaModes.routed` when calling `create_session()`.","message":"The default `media_mode` for sessions created via `create_session()` changed from `MediaModes.routed` to `MediaModes.relayed` in v2.2.1. Relayed (peer-to-peer) sessions have limitations, particularly regarding archiving or certain advanced features, which require `routed` sessions.","severity":"gotcha","affected_versions":">=2.2.1"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}