Vonage Video (API Client)
The `vonage-video` package provides programmatic access to the Vonage Video API, enabling server-side applications to manage video sessions, tokens, archives, and broadcasts. While it exists as a standalone PyPI package, it is primarily designed to be installed as a dependency of the main `vonage` Python SDK (currently v4.x.x). The `vonage` SDK is the recommended entry point for interacting with all Vonage APIs, including Video. Vonage maintains an active release cadence for its SDKs, with frequent updates and improvements.
Common errors
-
AttributeError: 'Vonage' object has no attribute 'video'
cause The installed `vonage` SDK version might be too old or `vonage-video` (as a dependency) might not be correctly installed/available, preventing the `video` module from being attached to the client.fixEnsure you have the latest `vonage` SDK installed: `pip install --upgrade vonage`. If the issue persists, check Python environment and dependencies. -
ModuleNotFoundError: No module named 'opentok'
cause This error occurs when trying to run code written for the legacy `opentok` SDK after migrating to the `vonage` SDK or when `opentok` was never installed.fixIf migrating, update your code to use the `vonage` SDK and its `client.video` methods and `vonage_video` data models. If it's a new project, ensure you are not importing `opentok` and are using `vonage` instead. -
from vonage.video import SessionOptions # ModuleNotFoundError: No module named 'vonage.video'
cause While `client.video` provides access to methods, data models like `SessionOptions` are imported directly from the top-level `vonage_video` package, not a nested submodule of `vonage`.fixChange the import statement to `from vonage_video import SessionOptions` (and similar for other models).
Warnings
- breaking Migration from OpenTok Python SDK to Vonage Python SDK requires significant changes. Positional parameters are replaced with data models, methods return Pydantic data models, and some method names have changed. Authentication now primarily uses Application ID and Private Key (for JWTs) instead of API Key and Secret for Video API operations.
- gotcha While `vonage-video` exists on PyPI, it's typically installed as a dependency of the main `vonage` SDK. Directly installing `vonage-video` and trying to use its classes might lead to missing broader SDK context or unexpected behavior, especially for API client instantiation.
- gotcha Authentication for the Vonage Video API uses an Application ID and a Private Key (for JWT generation). The traditional API Key and Secret are generally used for other Vonage APIs (like SMS, Voice) but not directly for Video API calls through the `vonage` client.
Install
-
pip install vonage -
pip install vonage-video
Imports
- Vonage
from vonage import Vonage, Auth
- SessionOptions
from vonage.video import SessionOptions
from vonage_video import SessionOptions
- MediaMode
from vonage_video.constants import MediaMode
Quickstart
import os
from vonage import Vonage, Auth
from vonage_video.constants import MediaMode
from vonage_video import SessionOptions
# Replace with your actual Vonage Application ID and Private Key path
# Obtain these from your Vonage Dashboard after creating a Video-enabled application.
VONAGE_APPLICATION_ID = os.environ.get('VONAGE_APPLICATION_ID', 'YOUR_APPLICATION_ID')
VONAGE_PRIVATE_KEY_PATH = os.environ.get('VONAGE_PRIVATE_KEY_PATH', 'path/to/your/private.key')
if VONAGE_APPLICATION_ID == 'YOUR_APPLICATION_ID' or not os.path.exists(VONAGE_PRIVATE_KEY_PATH):
print("Please set VONAGE_APPLICATION_ID environment variable and VONAGE_PRIVATE_KEY_PATH, and ensure the private key file exists.")
print("Refer to https://developer.vonage.com/en/messages/application/create for creating an application.")
else:
try:
auth = Auth(
application_id=VONAGE_APPLICATION_ID,
private_key=VONAGE_PRIVATE_KEY_PATH,
)
client = Vonage(auth=auth)
# Create a session
session_options = SessionOptions(media_mode=MediaMode.ROUTED)
session = client.video.create_session(session_options)
session_id = session.session_id
print(f"Created Session ID: {session_id}")
# Generate a token for a client to connect to the session
token = client.video.generate_client_token(session_id)
print(f"Generated Client Token: {token}")
except Exception as e:
print(f"An error occurred: {e}")