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:

Download repository 🔗

git clone -b v2.29.0
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/:sessionId/connections : Create a connection.

You can get more information about these 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: the URL of your OpenVidu deployment.
  • OPENVIDU_SECRET: the secret of your OpenVidu deployment.

Initialize session endpoint 🔗

The first endpoint allows us to 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 creates a new Session using the OpenVidu REST API and returns the sessionId of the new session. If the request brought a customSessionId parameter and that session already existed in the OpenVidu deployment (that's the 409 CONFLICT error), then the endpoint simply returns the same customSessionId. At this point the Session is ready to create Connections, whether it is a newly created Session or an already existing one.

Create connection endpoint 🔗

The second endpoint allows us to create a new OpenVidu Connection in the 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. We can use this token in openviu-browser SDK to connect the user to the Session.