> ## Documentation Index > Fetch the complete documentation index at: https://bunny.net/docs/llms.txt > Use this file to discover all available pages before exploring further. # Sign Bunny Player embed URLs on the server > Play videos from a library with embed view token authentication in Next.js, Nuxt, or SvelteKit, by signing the embed URL on the server so the key never reaches the browser. With [embed view token authentication](https://bunny.net/docs/stream/token-authentication.md) on, Bunny Player turns away any embed URL without a valid `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. > [card] [Next.js](https://github.com/BunnyWay/examples/tree/main/stream/player-token-auth-nextjs) > > A Server Component. > [card] [Nuxt](https://github.com/BunnyWay/examples/tree/main/stream/player-token-auth-nuxt) > > A server route and `useFetch`. > [card] [SvelteKit](https://github.com/BunnyWay/examples/tree/main/stream/player-token-auth-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. > [!WARNING] > > Check that the viewer may watch the video before you sign its URL. The page or route that signs is a public endpoint, so anyone can request it with any video ID from your library. Without that check, token authentication protects nothing. The comment in each example below marks where the check goes. > [tab] Next.js > > Put `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. > > ```ts lib/bunny-stream.ts theme={null} > import "server-only"; > import { createHash } from "node:crypto"; > > const TOKEN_TTL_SECONDS = 60 * 60; > > export function signEmbedUrl(videoId: string): { src: string; expires: number } { > const libraryId = process.env.BUNNY_STREAM_LIBRARY_ID!; > const key = process.env.BUNNY_STREAM_TOKEN_AUTH_KEY!; > const expires = Math.floor(Date.now() / 1000) + TOKEN_TTL_SECONDS; > const token = createHash("sha256").update(`${key}${videoId}${expires}`).digest("hex"); > const query = new URLSearchParams({ token, expires: String(expires) }); > > return { src: `https://player.mediadelivery.net/embed/${libraryId}/${videoId}?${query}`, expires }; > } > ``` > > `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 ``. With Cache Components on, as in a new `create-next-app` project, request data outside `` fails the build. > > ```tsx app/videos/[id]/page.tsx theme={null} > import { Suspense } from "react"; > import { connection } from "next/server"; > import { signEmbedUrl } from "@/lib/bunny-stream"; > > export default function VideoPage({ params }: { params: Promise<{ id: string }> }) { > return ( > > > > ); > } > > async function SignedPlayer({ params }: { params: Promise<{ id: string }> }) { > await connection(); > const { id } = await params; > // Check that the signed-in viewer may watch this video before signing. > // Anyone can request this with any video ID, so never skip the check. > const { src } = signEmbedUrl(id); > > return ( > > ``` > > For playback control, pass `token` and `expires` to the [Svelte and SvelteKit guide](https://bunny.net/docs/stream/player/svelte.md)'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 > [accordion] 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](https://bunny.net/docs/stream/embedding.md#embedding-restrictions). > [accordion] 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 ``. Check the route isn't prerendered in Nuxt or SvelteKit. This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.