SvelteKit quickstart
Play a hosted video in a SvelteKit app (Svelte 5) with the transcodely-player custom element, and serve private videos with a token endpoint. This uses @transcodely/sdk 0.3.28 or later.
The quick path: install the SDK, import the element in a component, and use the tag. Svelte renders custom elements as they are; no configuration is needed.
1. Install
npm install @transcodely/sdk2. Use the element
<!-- src/lib/LessonPlayer.svelte -->
<script lang="ts">
import "@transcodely/sdk/player/element";
let { videoId }: { videoId: string } = $props();
</script>
<transcodely-player
video-id={videoId}
title="Lesson"
onprogress={(e: CustomEvent<{ percent: number }>) =>
console.log(`played ${Math.round(e.detail.percent * 100)}%`)}
oncompleted={() => console.log("lesson complete")}
></transcodely-player>Render <LessonPlayer videoId="vid_a1b2c3d4e5f6g7" /> with the id of a ready, public or unlisted video.
Server rendering is safe. The import runs on the server too, where it does nothing: the tag is defined only in the browser, so the page renders the bare element on the server and the player appears when the page hydrates. No onMount or browser check is needed.
Events arrive as DOM events with the payload in event.detail: ready, play, pause, ended, seeked, timeupdate, progress, completed and error. In progress, percent is the share of the video actually played, from 0 to 1; seeking ahead does not add to it. completed fires once per page load when that share reaches the threshold, 0.9 by default.
Methods. Bind the element with bind:this and call play(), pause() or seek(seconds) on it.
3. Private videos
A private video needs a playback token signed on your server for one viewer. Add an endpoint:
// src/routes/api/playback-token/+server.ts
import { signPlaybackToken } from "@transcodely/sdk";
/** Replace with your auth library's session lookup. Until then only local development is let in. */
async function currentUserId(request: Request): Promise<string | null> {
void request;
return process.env.NODE_ENV === "production" ? null : "dev-user";
}
export async function GET({ request, url }: { request: Request; url: URL }): Promise<Response> {
const userId = await currentUserId(request);
if (!userId) return new Response(null, { status: 401 });
const videoId = url.searchParams.get("video") ?? "";
// Check here that this user may watch this video.
const token = signPlaybackToken({
keyId: process.env.TRANSCODELY_PLAYBACK_KEY_ID!,
secret: process.env.TRANSCODELY_PLAYBACK_SECRET!,
videoId,
viewerId: userId,
});
return Response.json({ token }, { headers: { "cache-control": "no-store" } });
}Set TRANSCODELY_PLAYBACK_KEY_ID (psk_…) and TRANSCODELY_PLAYBACK_SECRET (pss_…) in the server environment, from a playback signing key created on your app’s page in the dashboard (the secret is shown once). Never give them a PUBLIC_ prefix: SvelteKit sends those to the browser. In your app you would normally read them from $env/dynamic/private.
Then point the element at the endpoint. It fetches a token before loading, and a fresh one when the token expires:
<transcodely-player
video-id={videoId}
token-url={`/api/playback-token?video=${videoId}`}
></transcodely-player>For uploads from the browser, the Browser Uploads guide has the SvelteKit upload route and the framework-free uploader.
What can go wrong
- The element renders but nothing plays: the video is not
ready, or it isprivateand has notoken-url. - The token endpoint answers 401:
currentUserIdreturned nothing. Wire in your real session lookup. - The player does not load on your domain: your app limits Allowed embed domains and this host is not on the list.
Next steps
- Embed a video anywhere: every attribute of the element.
- Protected Playback: tokens, overlays and resolution caps.
- Course Progress: completion, resume and progress per viewer.