What You’ll Build
A Next.js application that:- Issues short-lived client tokens from a server API route
- Uses client tokens in the browser to connect to Decart’s real-time API
- Captures and transforms webcam video in real-time
- Never exposes your API key to the client
Prerequisites
- Node.js 18 or higher
- A Decart API key
- A webcam for testing
Setup
Configure your API key
Create a
.env.local file and add your API key:Use
.env.local in Next.js to ensure environment variables are not exposed to the client.Start the development server
Architecture
Server: Token Generation API Route
The/api/realtime-token/route.ts endpoint creates client tokens:
Client: Main Page Component
Theapp/page.tsx component manages the prompt state:
Client: VideoStream Component
Thecomponents/video-stream.tsx component fetches a token and connects:
Key Concepts
Client Tokens
Client tokens provide secure, temporary access to the real-time API:Benefits of Client Tokens
- Security: Your main API key never leaves the server
- Scoped Access: Tokens only work with the real-time API
- Time-Limited: Tokens expire automatically
- Simple: Drop-in replacement for API keys in client code
Next.js App Router
This example uses Next.js App Router features:- Server Actions: API routes in
app/api/directory - Client Components: Interactive UI with
"use client"directive - Environment Variables:
.env.localfor server-side secrets
Error Handling
Handle token fetch and connection errors:Available Models
You can use different real-time models:mirage_v2- MirageLSD video restyling (recommended)mirage- Original MirageLSD modellucy_v2v_720p_rt- Lucy for video editing (add/change objects)lucy_2_rt- Lucy 2 with reference image support