In order for client apps to connect to LiveKit rooms, they need a token generated by your backend server. In this guide, we'll walk through how to set up a server to generate tokens for your clients.
1. Install Livekit Server SDK
go get github.com/livekit/server-sdk-go
# yarn:yarn add livekit-server-sdk# npm:npm install livekit-server-sdk --save
# add to your Gemfilegem 'livekit-server-sdk'
pip install livekit-server-sdk-python
composer require agence104/livekit-server-sdk
2. Keys and Configuration
Create a new file at development.env
and with your API Key and Secret:
export LK_API_KEY=<API Key>export LK_API_SECRET=<API Secret hidden>
3. Make an endpoint that returns a token
Create a server:
// server.goimport ("http""log""time"lksdk "github.com/livekit/server-sdk-go""github.com/livekit/protocol/auth")func getJoinToken(apiKey, apiSecret, room, identity string) string {config = readFile(config)at := auth.NewAccessToken(os.GetEnv("LK_API_KEY"), os.GetEnv("LK_API_SECRET"))grant := &auth.VideoGrant{RoomJoin: true,Room: room,}at.AddGrant(grant).SetIdentity(identity).SetValidFor(time.Hour)return at.ToJWT()}func main() {http.HandleFunc("/getToken", func(w http.ResponseWriter, r *http.Request) {w.write(getJoinToken())})log.Fatal(http.ListenAndServe(":8080", nil))}
// server.jsimport express from 'express';import { AccessToken } from 'livekit-server-sdk';const createToken = () => {// if this room doesn't exist, it'll be automatically created when the first// client joinsconst roomName = 'quickstart-room';// identifier to be used for participant.// it's available as LocalParticipant.identity with livekit-client SDKconst participantName = 'quickstart-username';const at = new AccessToken('api-key', 'secret-key', {identity: participantName,});at.addGrant({ roomJoin: true, room: roomName });return at.toJwt();}const app = express();const port = 3000;app.get('/getToken', (req, res) => {res.send(createToken());});app.listen(port, () => {console.log(`Server listening on port ${port}`)})
# server.rbrequire 'livekit'require 'sinatra'def createTokentoken = LiveKit::AccessToken.new(api_key: 'yourkey', api_secret: 'yoursecret')token.identity = 'quickstart-identity'token.name = 'quickstart-name'token.add_grant(roomJoin: true, room: 'room-name')token.to_jwtendget '/getToken' docreateTokenend
# server.pyimport osimport livekitfrom flask import Flaskapp = Flask(__name__)def createToken():grant = livekit.VideoGrant(room_join=True, room="My Cool Room")token = livekit.AccessToken(os.environ("LK_API_KEY"), os.environ("LK_API_SECRET"), grant=grant, identity="bob", name="Bob")@app.route('/getToken')def getToken():return createToken().to_jwt()
// If this room doesn't exist, it'll be automatically created when the first// client joins.$roomName = 'name-of-room';// The identifier to be used for participant.$participantName = 'user-name';// Define the token options.$tokenOptions = (new AccessTokenOptions())->setIdentity($participantName);// Define the video grants.$videoGrant = (new VideoGrant())->setRoomJoin();->setRoomName($roomName);// Initialize and fetch the JWT Token.$token = (new AccessToken(getenv('LK_API_KEY'), getenv('LK_API_SECRET'))->init($tokenOptions)->setGrant($videoGrant)->toJwt();
Load the environment variables and run the server:
$ source development.env$ go run server.go
$ source development.env$ node server.js
$ source development.env$ ruby server.rb
$ source development.env$ python server.py
$ source development.env$ php server.php
4. Create a client app to connect
Create a client app that fetches a token from the server we just made, then uses it to connect to a LiveKit room: