Skip to Content
Mux Docs: Home

Add high-performance video to your Astro application

Use our API and components to handle embedding, storing, and streaming video in your Astro application

When should you use Mux with Astro?

When adding video to your Astro app, you'll encounter some common hurdles. First, videos are large. Storing them in your public directory can lead to excessive bandwidth consumption and poor Git repository performance. Next, it's important to compress and optimize your videos for the web. Then, as network conditions change, you might want to adapt the quality of your video to ensure a smooth playback experience for your users. Finally, you may want to integrate additional features like captions, thumbnails, and analytics.

You might consider using Mux's APIs and components to handle these challenges, and more.

Use the API to build your video workflow

If you're looking to build your own video workflow that enables uploading, playback, and more in your application, you can use the Mux API and components like Mux Player and Mux Uploader.

Example: allowing users to upload video to your app

One reason you might want to build your own video workflow is when you want to allow users to upload video to your app.

Much of the work described here is done on the server and is unique for every user. Make sure your Astro app is in SSR mode before you begin.

Let's start by adding a new page where users can upload videos. This will involve using the Mux Uploader component, which will upload videos to a Mux Direct Uploads URLAPI.

In the code sample below, we'll create an upload URL using the Mux Node SDK and the Direct Uploads URL API. We'll pass that URL to the Mux Uploader component, which will handle uploading for us.

import Layout from '../layouts/Layout.astro';
import Mux from "@mux/mux-node";
const mux = new Mux({
tokenId: import.meta.env.MUX_TOKEN_ID,
tokenSecret: import.meta.env.MUX_TOKEN_SECRET
const upload = await{
new_asset_settings: {
playback_policy: ['public'],
encoding_tier: 'baseline',
cors_origin: '*',
<Layout title="Upload a video to Mux">
<mux-uploader endpoint={upload.url}></mux-uploader>
import '@mux/mux-uploader';

In production, you'll want to apply additional security measures to your upload URL. Consider protecting the route with authentication to prevent unauthorized users from uploading videos. Also, use cors_origin and consider playback_policy to further restrict where uploads can be performed and who can view uploaded videos.

Next, we'll create an API endpoint that will listen for Mux webhooks. When we receive the notification that the video has finished uploading and is ready for playback, we'll add the video's metadata to our database.

import type { APIRoute } from 'astro';
import mux from '../lib/mux';
export const POST: APIRoute = async ({ request }) => {
const body = await request.text();
// mux.webhooks.unwrap will validate that the given payload was sent by Mux and parse the payload.
// It will also provide type-safe access to the payload.
// Generate MUX_WEBHOOK_SIGNING_SECRET in the Mux dashboard
const event = mux.webhooks.unwrap(
// you can also unwrap the payload yourself:
// const event = await request.json();
switch (event.type) {
case 'video.upload.asset_created':
// we might use this to know that an upload has been completed
// and we can save its assetId to our database
case 'video.asset.ready':
// we might use this to know that a video has been encoded
// and we can save its playbackId to our database
// there are many more Mux webhook events
// check them out at
return new Response(JSON.stringify({ message: 'ok' }), {
headers: {
'Content-Type': 'application/json',

Finally, let's make a playback page. We retrieve the video metadata from our database, and play it by passing its playbackId to Mux Player:

import Layout from '../../layouts/Layout.astro';
const { playbackId } = Astro.params;
<mux-player playback-id={playbackId}></mux-player>
import '@mux/mux-player';

And we've got upload and playback. Nice!

What's next? You can integrate with your CMS. You can optimize your loading experience. Or get started with the example project below:

Example projects


This is a bare-bones starter application with Astro that uses:

Was this page helpful?