How to access file asset URL in Sanity query for PDF download link?
The issue is that you need to dereference the asset properly in your GROQ query. The syntax you're using is close, but it needs a slight adjustment.
Your current query:
PDF{
asset->url
}This should actually be:
"pdf": PDF.asset->urlOr if you want to keep the entire PDF object structure and add the URL:
PDF{
...,
"url": asset->url
}The key is using dot notation (PDF.asset->) rather than just querying inside the object. The -> operator dereferences the asset reference to get the actual asset document, and then you access its url property.
So your full query might look something like:
*[_type == "yourDocumentType"]{
"pdfUrl": PDF.asset->url
}Then in your component, you can use it directly:
<a href={pdfUrl}>Download PDF</a>Bonus tip: If you want the file to trigger a download instead of opening in the browser, append ?dl to the URL:
<a href={`${pdfUrl}?dl`}>Download PDF</a>This works because Sanity's asset URLs support the ?dl query parameter to force downloads, which is especially useful since the standard HTML download attribute doesn't work across different origins.
Show original thread10 replies
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.