
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeTo render a downloadable file in Sanity, you'll need to query the file URL and handle the download properly on your frontend. Here's how:
First, define a file field in your schema:
{
name: "document",
title: "Document",
type: "file",
}In your GROQ query, get the file URL like this:
*[_type == "yourDocType"] {
"fileUrl": document.asset->url
}The key thing to know is that the standard HTML download attribute won't work for files hosted on Sanity's CDN (different origin). Instead, use the ?dl query parameter that Sanity provides:
// React/Next.js example
<a href={`${fileUrl}?dl`}>
Download File
</a>Or if you want to specify a custom filename:
<a href={`${fileUrl}?dl=${filename}`}>
Download {filename}
</a>export default function FileDownload({ document }) {
const fileUrl = document.fileUrl;
const fileName = document.fileName || 'download';
return (
<a
href={`${fileUrl}?dl=${fileName}`}
className="download-button"
>
Download {fileName}
</a>
);
}The ?dl parameter tells Sanity's CDN to set the proper Content-Disposition header, which triggers the browser's download behavior instead of trying to display the file in the browser.
You can find more details in the Sanity file type documentation and this community answer about rendering files.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store