openvidu-basic-python 🔗

Check it on GitHub

This is a minimal OpenVidu server application sample built for Python with Flask. It internally uses the OpenVidu REST API.

Running this application 🔗

Prerequisites 🔗

To run this application you will need Python 3 installed on your system:

Download repository 🔗

git clone
cd openvidu-tutorials/openvidu-basic-python

Create a python3 environment and activate it 🔗

python3 -m venv venv
. venv/bin/activate

Install dependencies 🔗

pip install -r requirements.txt

Run application 🔗


Understanding the code 🔗

The application is a simple Flask application with a single controller file that exports two endpoints:

  • /api/sessions : Initialize a session.
  • /api/sessions/{{SESSION_ID}}/connections : Create a connection.

You can get more information about theses endpoints in the Application Server Endpoints section.

Let's see the code of the controller:

app = Flask(__name__)
# Enable CORS support
cors = CORS(app, resources={r"/*": {"origins": "*"}})

# Load env variables
SERVER_PORT = os.environ.get("SERVER_PORT")
OPENVIDU_URL = os.environ.get("OPENVIDU_URL")

if __name__ == "__main__":, host="", port=SERVER_PORT)


Starting by the top, the file has the following fields:

  • app : Flask application.
  • cors : CORS support for Flask application.
  • SERVER_PORT : Port of the application server.
  • OPENVIDU_URL : URL of the OpenVidu Server.
  • OPENVIDU_SECRET : Secret of the OpenVidu Server.

Initialize session endpoint 🔗

The first endpoint allows us initialize a new OpenVidu Session. The code of this endpoint is the following:


@app.route("/api/sessions", methods=['POST'])
def initializeSession():
        body = request.json if else {}
        response =
            OPENVIDU_URL + "openvidu/api/sessions",
            auth=("OPENVIDUAPP", OPENVIDU_SECRET),
            headers={'Content-type': 'application/json'},
        return response.json()["sessionId"]
    except requests.exceptions.HTTPError as err:
        if (err.response.status_code == 409):
            # Session already exists in OpenVidu
            return request.json["customSessionId"]
            return err

The endpoint receives a POST request with a JSON body containing the customSessionId of the session to be created. If the session already exists in OpenVidu, the endpoint returns the customSessionId received in the request body. Otherwise, it creates a new session, using the OpenVidu REST API and returns the sessionId of the new session.

Create conneciton endpoint 🔗

The second and last endpoint has the goal of creating a new OpenVidu Connection in a session:


@app.route("/api/sessions/<sessionId>/connections", methods=['POST'])
def createConnection(sessionId):
    body = request.json if else {}
        OPENVIDU_URL + "openvidu/api/sessions/" + sessionId + "/connection",
        headers={'Content-type': 'application/json'},

The endpoint creates a new Connection using the OpenVidu REST API and returns the token of the new connection.