token and expires. The key behind those tokens can sign every video in the library. Keep it on the server and send the browser a finished URL.
Next.js
A Server Component.
Nuxt
A server route and
useFetch.SvelteKit
A server
load function.Turn on token authentication
On your library’s Security page, enable Embed view token authentication. Copy the key from the Token authentication key card below it. The field there is labelled API key, but it’s a different value from the library’s API key on the API page. The same key signs CDN token authentication URLs for the library, so resetting it breaks both. Unsigned embeds start returning 403 straight away. Deploy the signing code first.Sign the URL
The token is a hex SHA-256 of the key, video ID, and expiry, joined with nothing between them. The expiry is Unix time in seconds.- Next.js
- Nuxt
- SvelteKit
Put For playback control, pass
BUNNY_STREAM_LIBRARY_ID and BUNNY_STREAM_TOKEN_AUTH_KEY in .env.local. The server-only import breaks the build if a Client Component ever pulls this file in.lib/bunny-stream.ts
await connection() signs a fresh URL on every request. Without it, Next.js can prerender a token at build time. Call it, and read params, in a component inside <Suspense>. With Cache Components on, as in a new create-next-app project, request data outside <Suspense> fails the build.app/videos/[id]/page.tsx
token and expires to the Next.js guide’s component through params.Choose an expiry
Anyone holding a signed URL can play it until it expires. We use an hour in the examples to make them easy to try. Production pages sign a fresh URL on every load, which makes a window of a few minutes painless.Troubleshooting
The iframe shows a 403
The iframe shows a 403
Check the inputs. The key comes from the Token authentication key card on the Security page, not the library’s API key on the API page. The video ID must match the URL, and
expires must be in seconds.With a good token, look at the library’s embedding restrictions.Every visitor gets the same expired link
Every visitor gets the same expired link
The page was rendered once and cached. In Next.js, call
await connection() in the component that signs, inside <Suspense>. Check the route isn’t prerendered in Nuxt or SvelteKit.