PYTHON · SERVER
Python server SDK
Create the short-lived user tokens your apps need for voice calls, video calls and chat, from a Python backend (Django, Flask, FastAPI or any other). Published on PyPI as rtcexpress-server: Python 3.8 or newer, no dependencies (standard library only), type hints included.
Where it runs
Your App Secret must stay on your server, and so does this package. Your backend signs the user in, decides which room they may join, and returns a token. The browser or mobile app passes that token to a client SDK (Web, React Native, Flutter, Android).
Install
pip install rtcexpress-serverCreate a token endpoint
import os
from rtcexpress_server import RTCExpressServer
rtc = RTCExpressServer(
app_id=os.environ["RTC_APP_ID"],
app_secret=os.environ["RTC_APP_SECRET"],
)
# Example with FastAPI: only signed-in users get a token.
@app.post("/api/rtc-token")
def rtc_token(user=Depends(current_user)): # your own login check
# Calling app: no room_id (one room per contact). One fixed room: room_id="support-42".
result = rtc.create_token(user.id)
return {"token": result.token, "expiresIn": result.expires_in}It works the same in Django or Flask. create_token is a normal (blocking) call; in async code you can run it with await asyncio.to_thread(rtc.create_token, user_id).
Options
| Argument | Required | Description |
|---|---|---|
app_id | Yes | Your project's App ID, from the customer console. |
app_secret | Yes | Your App Secret. Load it from an environment variable or secret store. |
server_url | No | Defaults to https://api.devsquarelabs.com. |
timeout | No | Request timeout in seconds. Defaults to 10. |
rtc.create_token(user_id, room_id=None, role=None) returns a result with token and expires_in (seconds, currently one hour). user_id is your own ID for the user; each device in a call needs its own. room_id limits the token to one room: use it for a single fixed room (a support chat, one meeting). Leave it out for calling apps where users call their contacts. role is "host", "publisher", "subscriber" or "audience", for broadcast-style rooms.
Errors
Failures raise RTCExpressError with a status: 401 wrong App ID or App Secret, 400 missing or invalid values, 429 too many token requests from your server (30 a minute per IP; reuse a token until it is close to expiry), 0 RTCExpress could not be reached (network error or timeout).
from rtcexpress_server import RTCExpressError
try:
rtc.create_token("alice")
except RTCExpressError as err:
if err.status == 401:
... # check RTC_APP_ID / RTC_APP_SECRET
raiseProduction notes
Another language? Call POST /v1/token directly from any backend. Node.js: Node.js server SDK.