Skip to content
This repository
branch: master

OpenTok Server SDK for Python

The OpenTok server SDK for Python lets you generate sessions and tokens for OpenTok applications.


To install from PyPi using pip, a package manager for Python.

pip install opentok

If you get "Permission Denied" errors try running it with sudo in front:

sudo pip install opentok

You can download the OpenTok Python SDK from GitHub:


The OpenTok Python SDK requires Python 2.2 or greater.

You need an OpenTok API key and API secret, which you can obtain at


In order to use any of the functions of the SDK, you must first create an OpenTokSDK object with your developer credentials.
The OpenTokSDK constructor takes two parameters:

  • api_key (string) - Your OpenTok API key
  • api_secret (string) - Your OpenTok API secret
import OpenTokSDK

# Creating an OpenTok Object
API_KEY = ''     # Replace with your API key.
API_SECRET = ''  # Replace with your API secret.

Creating Sessions

Use the createSession() method of the OpenTokSDK object to create a session and a session ID:

# creating an OpenTok server-enabled session:
session_id = OTSDK.create_session().session_id

# Creating a peer-to-peer session
session_properties = {OpenTokSDK.SessionProperties.p2p_preference: "enabled"}
session_id = OTSDK.create_session(properties=session_properties).session_id

Generating Tokens

With the generated sessionId, you generate tokens for each user.

# Generate a publisher token that will expire in 24 hours:
token = OTSDK.generate_token(session_id)

# Generate a subscriber token that has connection data
role = OpenTokSDK.RoleConstants.SUBSCRIBER
connect_data = "username=Bob,level=4"
token = OTSDK.generate_token(session_id, role, None, connect_data)

Possible Errors:

  • "Null or empty session ID are not valid"
  • "An invalid session ID was passed"

More information

See the reference documentation.

For more information on OpenTok, go to

Something went wrong with that request. Please try again.