Skip to content

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:

  1. The rule: a restriction stored in your Mux environment, e.g. "allow example.com and *.example.com, block requests without a referrer". You manage these under Mux → Playback Restrictions.
  2. The token: Mux only checks a rule when a signed token names it in its playback_restriction_id claim. 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 request

Some consequences:

QuestionAnswer
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:

ChangeImages (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 secondsseveral 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:

ColumnMeaning
DomainsComma-separated: example.com, *.example.com. * allows every domain; empty allows none. Up to 10 entries.
Allow No ReferrerWhether requests with no Referer header are allowed, e.g. a URL pasted into the address bar, many native apps and smart-TV players.
DefaultTick to make this the default restriction for every signed token. Only one can be ticked.
IDThe 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, or mysite.test locally), 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:

bash
# .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:

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.

twig
{% 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.

twig
{% 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:

php
// 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:

twig
{{ video.getPlaybackToken('v', {playback_restriction_id: ''}) }}

Which methods accept an override ​

MethodDefault restrictionPer-call override
getPlaybackToken(type, modifiers)YesYes, in modifiers
getStreamUrl(modifiers)YesYes
getThumbnailUrl(modifiers)YesYes
getAnimatedUrl(modifiers)YesYes
getSecurePlaybackJWT(keyId, type, options)YesYes, in options
getStoryboardUrl(), getTextTrackUrl(), getStaticRenditionUrl(), thumb()YesNo: build the URL from a token instead (below)
twig
{# 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>

Render every video through one partial, so signing, restrictions, audio-only handling and the missing-key fallback live in one place:

twig
{# 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 %}
twig
{# 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.

graphql
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 ​

graphql
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 variableResult
Not passed, or nullThe 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 ​

SymptomLikely causeFix
Video plays everywhere despite a restrictionNo default is set, so tokens don't name the restrictionSet Default Playback Restriction ID
Video plays everywhere despite a restrictionThe playback ID is publicRestrictions only apply to signed playback IDs
Restriction worked before 2.8.0?It didn't: earlier versions never added it to tokensUpgrade and set the default
403 on your own siteYour domain, or the CP domain, isn't in the restriction's domain listAdd it under Mux → Playback Restrictions
403 when opening a URL directlyNo-referrer requests are blockedExpected. Test inside a page, or allow no referrer.
403 in a native app or TV playerThose players send no referrerAllow no referrer in the restriction
404 Not Found on every signed URLThe restriction named in the tokens was deletedChoose another default, or clear it
400 Invalid playback restriction IDThe ID is mistypedCopy 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 playsThe tokens use different restrictionsPass the same restriction to all three tokens, or use the partial
Added a domain: thumbnails work but the video is still refusedMux keeps refusing an already-refused stream URL for several minutes after a restriction is loosenedWait a few minutes. See How fast changes apply.