Sign playback tokens
Let your backend decide who watches a private video, with a token it signs itself.
A private video plays only with a token: a short JWT that your backend signs with your app's signing key after it has decided the viewer may watch. Signing is local. Your backend makes no call to ClipMux per play, and ClipMux checks the signature when the player asks for the video.
If you use clipmuxHandler and <clipmux-player session-url> from the
quickstart, this already happens: your playback
callback decides, and the handler signs. Read on to sign tokens yourself, for
your own route, another player or another language.
Before you start
- A video with a signed playback ID. Uploads are signed unless you ask
for
playback_policy: "public". - Your app's signing key, as
CLIPMUX_SIGNING_KID(the key's ID) andCLIPMUX_SIGNING_KEY(a P-256 private key in PEM).npx @clipmux/node initwrites both. You can also create one under Apps & API in the dashboard, or withclipmux admin create-signing-keyon the server. - The media origin,
base_url, fromGET /v1/app, for examplehttps://media.example.com.
The signing key is a secret. Keep it on your server, never in browser code, and never in a log.
What a token holds
A token is an ES256 JWT. Its header names the key, and its claims name the video, what the token may fetch, and for how long.
| Part | Field | Value |
|---|---|---|
| Header | alg | ES256 |
| Header | kid | Your CLIPMUX_SIGNING_KID |
| Claims | sub | The playback ID, pid_… |
| Claims | aud | v to play the video, its tracks and its poster; t for the poster only |
| Claims | iat, exp | Issued and expiry times, in whole Unix seconds |
| Claims | vid | Optional. A stable ID for the viewer, which counts unique viewers. ClipMux stores only a keyed hash of it |
exp - iat may be at most max_token_lifetime_seconds from GET /v1/app,
which is 7 days by default. Keep tokens short, 15 minutes for a player that
renews them, and let the player ask again.
Sign a token
Check access
Decide in your own code, from your own session, whether this person may watch this video. ClipMux has no idea who your users are. If the answer is no, don't sign anything.
Sign
import { ClipMux } from '@clipmux/node'
const clipmux = new ClipMux() // reads the CLIPMUX_* settings
const token = await clipmux.playback.token('pid_…', {
viewerId: user.id, // optional
expiresIn: '15m', // the default
}){ audience: 'poster' } signs a poster-only token.
Any ES256 JWT library works in other languages: put kid in the header and
the claims above in the body.
Play
Give the player the token, or build the URL yourself:
https://media.example.com/v/{token}/{playback_id}/master.m3u8
https://media.example.com/v/{token}/{playback_id}/poster.jpgWith <clipmux-player>, set getToken so the player asks your backend each
time it needs a fresh token. See Add the player.
Check it works
curl -s -o /dev/null -w '%{http_code}\n' \
"https://media.example.com/v/$TOKEN/pid_…/master.m3u8"200: the token is good.401withtoken_expired: the token'sexphas passed. Sign a new one; the player does this by itself.403withdenied: the signature, the key, the playback ID or the audience is wrong, or the key was revoked. Stop, and check thekidmatches the key you signed with.404: the playback ID was revoked, or doesn't exist.
Safari and AirPlay
iOS before 17.1 and AirPlay play HLS themselves and can't renew a token
mid-video. <clipmux-player> tells your backend when it plays natively:
native: true in the session request, or in getToken's argument. Sign
those tokens for longer. The Node SDK does it for you with
playback.token(id, { native: true, durationSeconds }): the larger of twice
the video's duration and 4 hours, or 12 hours without a duration, within the
maximum.
Next
- Add the player
- Posters and thumbnails
- Playback IDs and tokens, the concepts behind them