Quickstart
Add uploads, webhooks and private playback to a Next.js app in four steps.
By the end of this page, a signed-in user of your app uploads a video from the browser, your database hears when it's ready, and a viewer your app allows watches it, encrypted. The browser never sees a ClipMux key.
It takes one command, one route file, two elements and three callbacks. The same handler runs in Express, Hono and other Fetch runtimes.
Before you start
- A ClipMux server, for example
https://video.example.com, and an owner account on it to approve your app. To run one, see Install with one line. - A Next.js app with the App Router, on Node 20.19 or later.
Add ClipMux
Connect the app
npm i @clipmux/node @clipmux/uploader @clipmux/player
npx @clipmux/node init --server https://video.example.cominit opens an approval page on your ClipMux server. Approve the app there,
as the owner, and init writes its credentials to .env.local:
CLIPMUX_URL, CLIPMUX_API_KEY, CLIPMUX_SIGNING_KID,
CLIPMUX_SIGNING_KEY and CLIPMUX_WEBHOOK_SECRET. It never prints them.
Keep .env.local out of git.
Add one route file
import { clipmuxHandler } from '@clipmux/node/handlers'
export const POST = clipmuxHandler({
// Someone wants to upload: say what the upload is, or return null to refuse.
async upload(req, { file }) {
await requireInstructor(req)
const lesson = new URL(req.url).searchParams.get('lesson')
return { passthrough: `lesson_${lesson}`, title: file.name, generate: { subtitles: true } }
},
// Someone wants to watch: say who they are, or return null to refuse.
async playback(req, { playbackId }) {
const user = await getUser(req)
return (await canWatch(user, playbackId)) ? { viewerId: user.id } : null
},
// A verified webhook, with the video re-fetched so it's current.
async asset(asset) {
await db.lesson.update({
where: { ref: asset.passthrough },
data: { status: asset.status, playbackId: asset.playback_ids[0]?.id },
})
},
})The file answers POST requests on three of your app's routes: /api/clipmux/upload, /playback and
/webhook. requireInstructor, getUser, canWatch and db stand for
your own code. Every route refuses by default: a callback you leave out is a
404, and one that returns null is a 403.
passthrough is your own ID for the video. It comes back on every webhook,
so you can find the record it belongs to. See
Link videos to your records.
Add two elements
'use client'
import type {} from '@clipmux/player/react' // JSX types only
import type {} from '@clipmux/uploader/react'
import { useEffect } from 'react'
export function LessonVideo({ lessonId, playbackId }: { lessonId: string; playbackId?: string }) {
// The elements need the DOM, so load them in the browser only.
useEffect(() => {
import('@clipmux/uploader/element')
import('@clipmux/player/element')
}, [])
return (
<>
<clipmux-uploader create-url={`/api/clipmux/upload?lesson=${lessonId}`} />
{playbackId && <clipmux-player playback-id={playbackId} session-url="/api/clipmux/playback" />}
</>
)
}The uploader sends the file in parts straight to storage, and resumes if the
connection drops. The player asks your playback callback for a token before
it plays, and again when the token runs out. Outside React, the same two
elements work in plain HTML:
<script type="module">
import '@clipmux/uploader/element'
import '@clipmux/player/element'
</script>
<clipmux-uploader create-url="/api/clipmux/upload?lesson=42"></clipmux-uploader>
<clipmux-player playback-id="pid_…" session-url="/api/clipmux/playback"></clipmux-player>Check it
npx @clipmux/node doctordoctor checks every credential and ends with ✓ Ready. It also prints the
origins your Content-Security-Policy must allow.
While you develop, your laptop can't receive webhooks from the server, so forward them:
npx @clipmux/node listen --forward-to localhost:3000/api/clipmux/webhookCheck it works
- Open a lesson page, choose a short video in the uploader and wait for it to finish.
- Watch the
listenterminal:video.asset.readyarrives when encoding is done, and yourassetcallback stores the status and the playback ID. - Reload the page. The video plays.
To test without the browser, upload a file from the terminal:
npx @clipmux/node upload sample.mp4 --passthrough lesson_42 --waitIf a step fails, Troubleshooting lists each error and what to do.
Next
examples/nextjs-lms: these four steps in a whole course site- Receive webhooks: every event, and keeping your records in step
- Add the player: events, React, subtitles and chapters
- Production checklist