Python#
This is a minimal server application built for Python with Flask that allows:
- Generating LiveKit tokens on demand for any application client.
- Receiving LiveKit webhook events.
It internally uses LiveKit Python SDK.
Running this application#
Download the tutorial code:
To run this server application, you need Python 3 installed on your device.
-
Navigate into the server directory
-
Create a python virtual environment
-
Activate the virtual environment
-
Install dependencies
-
Run the application
Info
You can run any Application Client to test against this server right away.
Understanding the code#
The application is a simple Flask app with a single file app.py
that exports two endpoints:
/token
: generate a token for a given Room name and Participant name./webhook
: receive LiveKit webhook events.
Let's see the code of the app.py
file:
app.py | |
---|---|
|
- Import all necessary dependencies from
livekit
library - Load environment variables from
.env
file - The port where the application will be listening
- The API key of LiveKit Server
- The API secret of LiveKit Server
- Initialize the Flask application
- Enable CORS support
The app.py
file imports the required dependencies and loads the necessary environment variables from .env
file using dotenv
library:
SERVER_PORT
: the port where the application will be listening.LIVEKIT_API_KEY
: the API key of LiveKit Server.LIVEKIT_API_SECRET
: the API secret of LiveKit Server.
Finally the Flask
application is initialized and CORS support is enabled.
Create token#
The endpoint /token
accepts POST
requests with a payload of type application/json
, containing the following fields:
roomName
: the name of the Room where the user wants to connect.participantName
: the name of the participant that wants to connect to the Room.
app.py | |
---|---|
|
- A new
AccessToken
is created providing theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
. - We set participant's identity in the AccessToken.
- We set the video grants in the AccessToken.
room_join
allows the user to join a room androom
determines the specific room. Check out all Video Grants. - Finally, we convert the AccessToken to a JWT token and send it back to the client.
The endpoint first obtains the roomName
and participantName
parameters from the request body. If they are not available, it returns a 400
error.
If required fields are available, a new JWT token is created. For that we use the LiveKit Python SDK:
- A new
AccessToken
is created providing theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
. - We set participant's identity in the AccessToken.
- We set the video grants in the AccessToken.
room_join
allows the user to join a room androom
determines the specific room. Check out all Video Grants. - Finally, we convert the AccessToken to a JWT token and send it back to the client.
Receive webhook#
The endpoint /webhook
accepts POST
requests with a payload of type application/webhook+json
. This is the endpoint where LiveKit Server will send webhook events.
app.py | |
---|---|
|
- Initialize a
TokenVerifier
using theLIVEKIT_API_KEY
andLIVEKIT_API_SECRET
. - Initialize a
WebhookReceiver
using theTokenVerifier
. It will help validating and decoding incoming webhook events. - Get the 'Authorization' header from the HTTP request.
- Obtain the webhook event using the
WebhookReceiver#receive
method. It expects the raw body of the request and the 'Authorization' header. - Consume the event as you whish.
First of all, we need a WebhookReceiver
for validating and decoding incoming webhook events. We initialize it with a TokenVerifier
built with the LIVEKIT_API_KEY
and LIVEKIT_API_SECRET
.
Inside the receive_webhook
handler we:
- Get the
Authorization
header from the HTTP request. - Obtain the webhook event using the
WebhookReceiver#receive
method. It expects the raw body of the request and theAuthorization
header. In this way, we can validate the event to confirm it is actually coming from our LiveKit Server. - If everything is ok, you can consume the event as you whish (in this case, we just log it).