SDK Usage
Session Token

Create Session Token

To create a session token for your Videoedge SDK integration, you need to set up an API endpoint in your application server. This endpoint will handle requests to generate a session token by passing the access key, secret access key, and room ID to the Videoedge server.

API Endpoint to Create Session Token

In your application server, implement the following API endpoint to create a session token:

Sample Code

const express = require("express");
const axios = require("axios");
const app = express();
const serverUrl = "https://test-api.videoedge.io/";
 
app.use(express.json());
 
app.post("/api/create-session-token", async (req, res) => {
  const { roomId } = req.body;
 
  try {
    const response = await axios.post(
      `${serverUrl}/api/org/create-session-token`,
      {
        accessKey: process.env.ACCESS_KEY,
        secretAccessKey: process.env.SECRET_ACCESS_KEY,
        roomId,
      }
    );
 
    // Check if the session token was generated successfully
    if (response.data.message === "Session token generated successfully") {
      return res.status(200).send({
        message: "Session token fetched successfully",
        sessionToken: response.data.sessionToken,
      });
    }
 
    // Check if an active session already exists for the roomId
    if (response.data.message === "Active session exists for this roomId") {
      return res.status(200).send({
        message: "Active session token fetched successfully",
        sessionToken: response.data.activeSessionToken,
      });
    }
 
    return res.status(400).send({
      message: "Failed to fetch session token",
    });
  } catch (error) {
    console.error("Error creating session token:", error);
    console.log("Error message", error.message);
    return res.status(500).json({ error: "Internal Server Error" });
  }
});

Frontend request to Fetch Session Token

To use the session token in your frontend application, make a request to your backend API to fetch the session token. Here’s an example of how you might implement this in your frontend code:

async function fetchSessionToken(roomId) {
  try {
    const response = await fetch("/api/create-session-token", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ roomId }),
    });
 
    const data = await response.json();
 
    if (response.status === 200) {
      console.log("Session Token:", data.sessionToken);
      return data.sessionToken; // Use this token to join the call
    } else {
      console.error("Error fetching session token:", data.message);
    }
  } catch (error) {
    console.error("Error:", error);
  }
}

Summary

To create a join event in the SDK instance, the session token must be passed. Therefore, first, implement the API endpoint in your application server to create the session token. Then, create a request in your frontend to call your backend API, which will fetch the session token for you. This setup allows you to manage session tokens efficiently and integrate them into your Videoedge SDK calls.