Quick Start Guide for OpenTok

Using OpenTok can be as simple as adding a few lines of code into a web page. This walk-through will show you how to create a simple OpenTok web application to chat with friends.

Step 1: Sign-up to get your API key, session ID, and token

Before you can begin using OpenTok, you must sign up using the developer dashboard. For testing purposes, you can use the developer dashboard to obtain the following:

  • API key — This identifies your OpenTok account.
  • Session ID — Your session ID identifies the "room" in which participants will meet and chat.
  • Token — The token is a key that defines who can enter the "room", and once inside what they can do.

In the Projects page of the developer dashboard, click on the "View Details" button for Project A.

In the "Create Session" section, click the "Create New" button create a session ID.

Then, in the "Generate Token" section, select "Publisher" as the role, and then click the "Generate new" button. Leave this web page open, because you will use the session ID and token soon.

Step 2. Insert the OpenTok.js library

To create an OpenTok-powered web application, you first need to add the OpenTok.js library in your HTML document.

<script src="//static.opentok.com/webrtc/v2.2/js/opentok.min.js" ></script>

Step 3: Code!

Copy and paste the following code into an HTML document.

<script type="text/javascript">
  var apiKey    = "YOUR API KEY HERE";
  var sessionId = "YOUR SESSION ID HERE";
  var token     = "YOUR TOKEN HERE";

  var session = OT.initSession(apiKey, sessionId);

  session.on("streamCreated", function(event) {
    session.subscribe(event.stream);
  });

  session.connect(token, function(error) {
    var publisher = OT.initPublisher();
    session.publish(publisher);
  });
</script>

Copy and paste your API key, the session ID, and the token into the obvious places at the top of the code you just put into your HTML document.

Step 4: View your app and share with friends!

Load the HTML document onto a web server, and view the application you just created. (You must load the page from a web server, not simply from the file system. Browsers will not allow you to connect to a WebRTC session on a page loaded from a file:// URL.)

Note: The browser will ask you to grant the page access to your webcam and microphone.

I want to cheat. Can you just show me a demo?

Here, check out a demo of the app.

What Next?

Congratulations, you made your first OpenTok app. So what's next from here?

IRC Live Chat

Have a quick question? Chat with TokBox Support on IRC. Join chat