Skip to main content
Put a segmnt video in a React app: a custom course platform, a dashboard or a single-page app. React changes nothing about playback: your server still makes the playback token, and the video still plays only on your verified domain. What changes is where the embed code lives: inside a component. There are two ways to add the video:

Before you start

Check four things first:
  1. Your video is ready (vid_...). See Manage videos.
  2. The host that serves your React app is a verified domain of the project. localhost cannot be verified. Use a staging host you control, such as staging.example.com, while you develop. See Integration guide, section 3.
  3. Your backend has a route that returns a playback token for a video. See Create playback tokens on your server.
  4. The API key stays on your server. Never put it in React code, a bundle or an environment variable that reaches the browser. The React app asks your backend for a token; it never talks to the segmnt API itself.
The token route answers with token, expiresAt, embedUrl and playlistUrl. The embed paths below use token; the white-label direct-mount path uses playlistUrl.

Path 1: iframe in JSX

Your backend mints a fresh token per page load. Your component fetches it and renders the iframe:
Rules for this component:
  • Fetch a token per page load. Do not store the token in localStorage, a context or a global store across sessions: it expires (4 hours by default), and a stored token stops working.
  • A long-lived single-page app can outlive a token. If the player shows “expired”, ask your backend for a new token and re-render. The error codes tell you which case you are in.
  • Keep the wrapper div and the aspect-ratio style, so the player resizes with its container.

Path 2: the player script

With many videos on one page, load the player script once and let a component mount the embed. The script goes in your page <head> — in Create React App or Vite, add it to the index.html:
Then mount the video in a useEffect, and remove it on cleanup:
SegmntPlayer.embed(target, options) builds the same responsive iframe as Path 1 inside target. It returns the <iframe> element, so the cleanup can remove it. The script defines window.SegmntPlayer after it loads; because the token arrives from your backend after render, the effect runs after the script is ready in practice. If your app renders before the script loads, gate the effect on the script (for example, set state in the script’s load event). Options for embed(): videoId, token, baseUrl, locale, title and aspectRatio. The locale is "ar" (default, RTL) or "en".

Next.js and other server-rendered setups

The embed is browser-only, so:
  • Put the component in a client component ("use client" in the App Router) or render it only after mount. The iframe and SegmntPlayer exist only in the browser.
  • Put the <script src=...> tag for Path 2 in your layout’s <head> (App Router: the head/metadata of the root layout; Pages Router: next/head). Keep defer.
  • Fetch the token from a route handler or API route on your server, which holds the API key in a server-side environment variable. Never put the key in NEXT_PUBLIC_* or any other variable that reaches the browser.

Common problems

Where to go next

Tested against the current release

Checked on 24 Oct 2026:
  • The JSX iframe path renders the same markup as the dashboard’s iframe embed: same /e/{videoId} URL shape, allow list, referrerpolicy and responsive wrapper.
  • Both components from this page were run in a DOM test harness (React 18, StrictMode on) against the real player bundle: SegmntPlayer.embed(target, { videoId, token, locale }) puts one iframe in the target, the cleanup removes it on unmount, and a StrictMode double-mount still leaves one player.
  • GET https://cdn.segmnt.cloud/segmnt-player.js → 200.
  • GET /e/{videoId} with no token → 403 token_missing (checked on the TEST deploy), so an app with a broken token fetch shows the player’s error message, not a blank box.
  • The token response fields (token, expiresAt, embedUrl, playlistUrl) match Playback tokens and the OpenAPI spec.