Search code examples
sanity

Access file asset url


I have a schema with a file type, and I'm successfully pulling the right data with groq. My question is how do I access the absolute path for the file that's uploaded to sanity?

Example data:

{ _createdAt: '2019-08-23T01:53:38Z',
  _id: '1948e168-32e2-4493-9513-6edb623fb568',
  _rev: 'As3uvzzIWDP1omVvc5btIF',
  _type: 'download',
  _updatedAt: '2019-08-23T02:00:17Z',
  file:
   { _type: 'file',
     asset:
      { _ref: 'file-fd0256015f201705310fc1c84704f02ac83bc147-zip',
        _type: 'reference' } },
  title: 'File Download' }

Solution

  • The URL is on the asset document, so you will have to follow that reference in your GROQ. In your case it could be *[_type=='download']{"fileURL":file.asset->url} (the arrow follows the reference to the asset document)