Playback Restrictions
A playback restriction limits where a signed video can be played, for example only on example.com. Signed tokens already limit how long a link works; a restriction stops a copied link from working on another website.
Available from 2.8.0
Before 2.8.0, restrictions could be created on the Playback Restrictions page, but the plugin never added them to tokens, so Mux never enforced them. From 2.8.0, the default restriction is applied to every signed token.
How it works
There are two separate pieces:
- The rule: a restriction stored in your Mux environment, e.g. "allow
example.comand*.example.com, block requests without a referrer". You manage these under Mux → Playback Restrictions. - The token: Mux only checks a rule when a signed token names it in its
playback_restriction_idclaim. A rule that no token names does nothing.
The plugin connects them: it adds the default restriction to every signed token it creates, in Twig, GraphQL and the control panel.
Mux → Playback Restrictions Default Playback Restriction ID
creates the rule in Mux ──► "use this one"
│
▼
every signed token includes its ID
│
▼
Mux checks the rule on every requestSome consequences:
| Question | Answer |
|---|---|
| Does a restriction need to be set at upload? | No. It's added when a token is created, so it applies to existing signed videos straight away, and you can change it at any time. |
| Does it work for public videos? | No. A public playback ID can't carry a token, so Mux can't restrict it. To restrict a video, it must have a signed playback ID. |
| How many restrictions can a token have? | One. A single restriction can allow several domains, so you usually only need one. |
| What happens to tokens already handed out when I change the default? | They keep the restriction they were created with until they expire (24–25 hours by default). |
| What if the restriction is deleted? | Every token that names it stops working within about 15 seconds: Mux returns 404. Choose a new default before deleting the old one. |
| What if I edit a restriction's rules? | Tokens already handed out follow the new rules, because a token only names the restriction. See How fast changes apply. |
How fast changes apply
Mux applies edits to a restriction on its own servers. Measured against a development environment:
| Change | Images (thumbnails, GIFs, storyboards) | Video stream |
|---|---|---|
| Tightening: removing a domain, blocking requests with no referrer | ~15 seconds | ~15 seconds |
| Deleting the restriction | ~15 seconds | ~15 seconds |
| Loosening: adding a domain, allowing no referrer | ~15 seconds | several minutes for a stream URL Mux has already refused |
Mux keeps refusing a stream URL it has already refused for a while after the rules are loosened. In the test it was still refused after 5 minutes. So when you add a domain to fix a blocked site, thumbnails start working straight away but the video can keep failing for several minutes. Reloading the page doesn't help, because the plugin returns the same token for up to an hour (rotation window). Wait, then try again.
Setup
1. Create the restriction
Go to Mux → Playback Restrictions and click Add a restriction. For each restriction:
| Column | Meaning |
|---|---|
| Domains | Comma-separated: example.com, *.example.com. * allows every domain; empty allows none. Up to 10 entries. |
| Allow No Referrer | Whether requests with no Referer header are allowed, e.g. a URL pasted into the address bar, many native apps and smart-TV players. |
| Default | Tick to make this the default restriction for every signed token. Only one can be ticked. |
| ID | The start of the restriction ID. Click it to copy the full ID. |
Recommended for a website:
- List your domains explicitly instead of
*. Include the domain of your Craft control panel (e.g.cms.example.com, ormysite.testlocally), otherwise video previews in the CP fail. - Leave Allow No Referrer off, unless your videos also play in native apps or people open caption and download links directly.
2. Make it the default
There are two ways:
Per environment (recommended). Each Mux environment (development, staging, production) has its own restriction IDs, so store the ID in each server's .env:
# .env
MUX_PLAYBACK_RESTRICTION_ID="YOUR_RESTRICTION_ID"Then, in Mux → Settings → Default Playback Restriction ID, choose $MUX_PLAYBACK_RESTRICTION_ID. Or set it in config/mux.php:
'playbackRestrictionId' => App::env('MUX_PLAYBACK_RESTRICTION_ID') ?? '',From the Playback Restrictions page. Tick the Default checkbox on a restriction. This saves its ID in the plugin settings, which are stored in project config and deployed to every environment. Only use it when all your environments share one Mux environment.
The page shows where the default comes from. Unticking the default asks for confirmation, then signed videos play without a restriction. When the default comes from an environment variable or config/mux.php, the Default checkboxes are disabled: change the variable or file instead.
The Default Playback Restriction ID setting also suggests the restrictions in your Mux environment as you type, and warns when the saved ID doesn't exist in the Mux environment for your API credentials.
3. Check it
- On a page: the video, poster and timeline previews all play and load.
- Without a referrer: a signed URL pasted into the address bar returns
403, if no-referrer requests are blocked. - From another site: the same player on a domain that isn't listed returns
403. - In the CP: the asset edit screen player and the thumbnails load.
- In the token: paste a token into jwt.io; it should contain
playback_restriction_id.
Twig
Using the default
Nothing to do: every signed token and URL the plugin creates includes the default restriction.
{% set video = entry.video.one() %}
<mux-player
playback-id="{{ video.getPlaybackId() }}"
playback-token="{{ video.getPlaybackToken('v') }}"
thumbnail-token="{{ video.getPlaybackToken('t') }}"
storyboard-token="{{ video.getPlaybackToken('s') }}"
></mux-player>
<img src="{{ video.getThumbnailUrl({width: 640}) }}" alt="">
<a href="{{ video.getStaticRenditionUrl('highest.mp4', 'video.mp4') }}">Download</a>For a public video, the tokens are null and the URLs are plain. No restriction applies.
A different restriction for one video
Pass playback_restriction_id with the other modifiers. Use the same restriction for all three player tokens; otherwise the video may play while the poster or timeline previews are blocked, or the other way round.
{% set restriction = craft.app.config.custom.muxRestrictions.partner %}
<mux-player
playback-id="{{ video.getPlaybackId() }}"
playback-token="{{ video.getPlaybackToken('v', {playback_restriction_id: restriction}) }}"
thumbnail-token="{{ video.getPlaybackToken('t', {playback_restriction_id: restriction}) }}"
storyboard-token="{{ video.getPlaybackToken('s', {playback_restriction_id: restriction}) }}"
></mux-player>
<img src="{{ video.getThumbnailUrl({width: 640, playback_restriction_id: restriction}) }}" alt="">Keep extra restriction IDs in configuration, not in templates. They differ per environment:
// config/custom.php
use craft\helpers\App;
return [
'muxRestrictions' => [
'partner' => App::env('MUX_RESTRICTION_PARTNER'),
],
];No restriction for one video
Pass an empty string to leave the default out:
{{ video.getPlaybackToken('v', {playback_restriction_id: ''}) }}Which methods accept an override
| Method | Default restriction | Per-call override |
|---|---|---|
getPlaybackToken(type, modifiers) | Yes | Yes, in modifiers |
getStreamUrl(modifiers) | Yes | Yes |
getThumbnailUrl(modifiers) | Yes | Yes |
getAnimatedUrl(modifiers) | Yes | Yes |
getSecurePlaybackJWT(keyId, type, options) | Yes | Yes, in options |
getStoryboardUrl(), getTextTrackUrl(), getStaticRenditionUrl(), thumb() | Yes | No: build the URL from a token instead (below) |
{# Download link with a non-default restriction #}
{% set restriction = craft.app.config.custom.muxRestrictions.partner %}
{% set token = video.getPlaybackToken('v', {playback_restriction_id: restriction}) %}
<a href="https://stream.mux.com/{{ video.getPlaybackId() }}/highest.mp4?token={{ token }}">Download</a>Recommended: one player partial
Render every video through one partial, so signing, restrictions, audio-only handling and the missing-key fallback live in one place:
{# templates/_components/mux-player.twig
video MuxAsset (required)
posterTime seconds for the poster frame (optional)
restriction restriction ID override, '' for none (optional; rare)
#}
{% set claims = (restriction ?? null) is not null ? {playback_restriction_id: restriction} : {} %}
{% set playbackId = video ? video.getPlaybackId() : null %}
{% if playbackId %}
{% set signed = video.securePlayback %}
{% set playbackToken = signed ? video.getPlaybackToken('v', claims) : null %}
{% set thumbnailToken = signed and not video.isAudioOnly ? video.getPlaybackToken('t', claims|merge({time: posterTime ?? null})) : null %}
{% set storyboardToken = signed and not video.isAudioOnly ? video.getPlaybackToken('s', claims) : null %}
{% if signed and not playbackToken %}
{# No signing key: the plugin logs a warning. Don't render a player that will fail. #}
<div class="video-unavailable">This video is not available right now.</div>
{% else %}
<mux-player
stream-type="on-demand"
playback-id="{{ playbackId }}"
metadata-video-id="{{ video.asset_id }}"
metadata-video-title="{{ video.title }}"
{% if video.isAudioOnly %}audio{% endif %}
{% if playbackToken %}playback-token="{{ playbackToken }}"{% endif %}
{% if thumbnailToken %}thumbnail-token="{{ thumbnailToken }}"{% endif %}
{% if storyboardToken %}storyboard-token="{{ storyboardToken }}"{% endif %}
></mux-player>
{% endif %}
{% endif %}{# Default restriction #}
{% include '_components/mux-player' with { video: entry.video.one(), posterTime: 2 } only %}
{# Partner page #}
{% include '_components/mux-player' with {
video: entry.video.one(),
restriction: craft.app.config.custom.muxRestrictions.partner,
} only %}GraphQL
Token and URL fields on playback_ids accept a playback_restriction_id argument. They need the signed playback permission on the schema.
Using the default
Nothing to do: tokens and signed URLs include the default restriction.
fragment MuxVideo on MuxAssetInterface {
playback_ids {
id
policy
playbackToken: token(type: "v")
thumbnailToken: token(type: "t", time: 2)
storyboardToken: token(type: "s")
poster: thumbnailUrl(width: 1280, format: "jpg")
}
}A different restriction, or none
query PartnerVideo($id: [QueryArgument], $restriction: String) {
muxAsset(id: $id) {
... on MuxAssetInterface {
playback_ids {
id
policy
playbackToken: token(type: "v", playback_restriction_id: $restriction)
thumbnailToken: token(type: "t", playback_restriction_id: $restriction)
storyboardToken: token(type: "s", playback_restriction_id: $restriction)
poster: thumbnailUrl(width: 1280, playback_restriction_id: $restriction)
download: staticRenditionUrl(name: "highest.mp4", playback_restriction_id: $restriction)
}
}
}
}$restriction variable | Result |
|---|---|
Not passed, or null | The default restriction |
"…" (an ID) | That restriction |
"" | No restriction |
Every URL field accepts the argument in GraphQL, including storyboardUrl, textTrackUrl and staticRenditionUrl.
Use every variable you declare
GraphQL rejects a query that declares a variable it doesn't use (Variable "$restriction" is never used). Declare $restriction only in queries that pass it to a field.
Keep restriction IDs on the server
Your frontend shouldn't decide which restriction to use. The server-side renderer that holds the schema token should choose it, or simply rely on the default. Anyone who can call a schema with the signed playback permission can request tokens without a restriction, by passing "". That's another reason to keep that schema private.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Video plays everywhere despite a restriction | No default is set, so tokens don't name the restriction | Set Default Playback Restriction ID |
| Video plays everywhere despite a restriction | The playback ID is public | Restrictions only apply to signed playback IDs |
| Restriction worked before 2.8.0? | It didn't: earlier versions never added it to tokens | Upgrade and set the default |
403 on your own site | Your domain, or the CP domain, isn't in the restriction's domain list | Add it under Mux → Playback Restrictions |
403 when opening a URL directly | No-referrer requests are blocked | Expected. Test inside a page, or allow no referrer. |
403 in a native app or TV player | Those players send no referrer | Allow no referrer in the restriction |
404 Not Found on every signed URL | The restriction named in the tokens was deleted | Choose another default, or clear it |
400 Invalid playback restriction ID | The ID is mistyped | Copy it from Mux → Playback Restrictions |
| Settings warns "doesn't exist in the Mux environment" | The ID belongs to a different Mux environment (e.g. production ID in development) | Use per-environment .env values |
| Poster or previews blocked but the video plays | The tokens use different restrictions | Pass the same restriction to all three tokens, or use the partial |
| Added a domain: thumbnails work but the video is still refused | Mux keeps refusing an already-refused stream URL for several minutes after a restriction is loosened | Wait a few minutes. See How fast changes apply. |