Anyone who could help me out with sanity/mux?

5 replies
Last updated: Nov 3, 2020
Anyone who could help me out with sanity/mux? I’m new to sanity and mux, and I want to be able to upload videos in sanity using mux, this integration for studio was easy. However, when I’m trying to fetch the video for web, I get this:
video: { asset: { _ref: '6c100819-3c4b-4c80-9077-3219a7ccb35e' } }
The ref is the same as the videos passthrough property. I dont get any other data on the asset (preferably I want the id).
In my schema I’ve added it like this (to a post consisting of some additional fields such as text, images etc.):

{
  name: 'video',
  type: 'mux.video',
}
And my query is currently super simple:

groq`*[_type == "post"]`

AI Update

You're hitting a common issue when working with Mux videos in Sanity! The problem is that your basic GROQ query only returns the reference to the video asset, not the actual asset data containing the playbackId and other Mux information you need.

The Solution: Dereference the Asset

You need to dereference the video asset in your GROQ query using the -> operator. Change your query from:

*[_type == "post"]

to:

*[_type == "post"] {
  ...,
  video {
    asset->
  }
}

The -> operator tells GROQ to follow the reference and fetch the actual mux.videoAsset document instead of just returning { _ref: '...' }.

Getting the Playback ID and Asset ID

Once you dereference the asset, you can access fields like playbackId, assetId, and more. Here's a more complete query example from the official Mux plugin documentation:

*[_type == "post"] {
  ...,
  video {
    asset-> {
      _id,
      playbackId,
      assetId,
      status,
      filename,
      thumbTime
    }
  }
}

This will return data like:

{
  video: {
    asset: {
      _id: '4e37284e-cec2-406d-973c-fdf9ab1e5598',
      playbackId: 'YA02HBpY02fKWHDRMNilo301pdH02LY3k9HTcK43ItGJLWA',
      assetId: '7ovyI76F92n02H00mWP7lOCZMIU00N4iysDiQDNppX026HY',
      status: 'ready',
      filename: 'my-video.mp4'
    }
  }
}

Understanding the Data Structure

When you add a mux.video field to your schema, Sanity stores it as a reference to a separate mux.videoAsset document. That's why you're seeing { _ref: '...' } - it's pointing to another document that contains all the Mux metadata.

The _ref value you mentioned matches the passthrough property because the Mux plugin uses passthrough to link Mux assets back to Sanity documents. This is completely normal behavior.

Using the Playback ID in Your Frontend

Once you have the playbackId, you can use it with Mux Player. For example, with @mux/mux-player-react:

import MuxPlayer from '@mux/mux-player-react'

export default function VideoPlayer({ playbackId }) {
  if (!playbackId) return null
  return <MuxPlayer playbackId={playbackId} />
}

The dereferencing operator (->) is essential for working with any reference types in Sanity, whether it's Mux videos, regular file assets, or references between documents. For more details on querying references, check out the Sanity documentation on writing GROQ queries for references.

The
asset
field is presumably a reference to an asset, and not an asset by itself, so it has to be dereferenced/expanded in the query https://www.sanity.io/docs/how-queries-work#expanding-references-8ca3cefc3a31
Thanks, yes I looked through that. However, I don’t understand how I can use that reference to get the videos Id. I have tried following the tutorial of sanity + mux with the request posted there, however the response is still the same:
video: { asset: { _ref: '6c100819-3c4b-4c80-9077-3219a7ccb35e' } }
Its solved, added to query
"video": video.asset->
Glad you got help with that! Let me know if we can help with anything else!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?