Trouble with file assets in Sanity Studio and how to upload them

10 replies
Last updated: Apr 14, 2020
Hi, I'm trying to get a File schema to work and am having no luck. I followed the docs: Files
When you need to upload a non-image file (say a PDF or a zip file) use the 
/assets/files/<dataset>
 endpoint. The response is similar to the one you get from an image, but the type will be 
fileAsset
.
But this is all I get. I see it in the graphql query, just nowhere in studio or anywhere else...
AI Update

I can see the confusion! The docs you quoted are about the HTTP API endpoint for uploading files, but to actually use files in your Studio, you need to define a file field type in your schema. The fileAsset you're seeing in GraphQL is the underlying asset type, but without a proper schema field, you won't see it in Studio.

Here's what you need to do:

Add a File Field to Your Schema

In your document schema, add a field with type: 'file':

{
  name: 'myDocument',
  type: 'document',
  fields: [
    {
      name: 'title',
      type: 'string',
    },
    {
      name: 'attachment',  // or whatever you want to call it
      title: 'Attachment',
      type: 'file',  // This is the key!
    }
  ]
}

Once you add this to your schema and restart your Studio, you'll see an upload interface where you can drag-and-drop or select files (PDFs, ZIPs, etc.).

Querying Files

When you query your documents, you can access the file like this:

*[_type == "myDocument"] {
  title,
  "fileUrl": attachment.asset->url
}

The file field type documentation has more details on available properties and options.

Why You're Seeing It in GraphQL But Not Studio

The sanity.fileAsset type exists in GraphQL because it's a system type that stores uploaded files, but it's not meant to be edited directly in Studio. You access file assets through file type fields in your documents, similar to how you use image type fields to work with sanity.imageAsset.

Once you add the file field to your schema, you'll be able to upload files through Studio, and they'll automatically create the underlying fileAsset documents that you're currently seeing in GraphQL.

Hi User, I’m sorry the file assets are giving you some trouble. If I understand you correctly, you’re able to upload them successfully via the endpoint and you can also query them? It’s just that you can’t see them in the Studio itself? If you have the Vision plugin installed, are you able to query them using
*[_type == 'file']
? What’s an example output of a file when queried with GraphQL?
I haven't tried uploading via the endpoint. I can however see the sanityFileAsset in GraphiQL
There’s no need to upload via the endpoint if you’re happy enough doing so within the Studio itself 🙂 The
/assets/files/<dataset>
endpoint is used to upload images via the HTTP API. In your case, it looks like you were trying to add the endpoint to your
rd-sanity.sanity.studio
URL, which is independent from the HTTP API and therefore the endpoint above.
The
rd-sanity.sanity.studio
URL is simply where you can access the Sanity Studio to manage your content. The HTTP API with its different endpoints would have a different URL, such as
<http://your-project-id.api.sanity.io/v1/assets/images/production|your-project-id.api.sanity.io/v1/assets/images/production>
. But as said above, you won’t necessarily need this to upload files. I hope the distinction is a bit clear 🙂

sanityFileAsset
and
allSanityFileAsset
are included by default in GraphiQL but do not necessarily mean that you have available files. They would also show if you have no files at all uploaded yet.
Let’s see if we can figure out why it’s not working. Could you share your file schema?
Thanks User, here's the file schema:
export default {
    
title: 'Rescource',
    
name: 'rescource',
    
type: 'file',
    
fields: [
        {
            
name: 'categories',
            
type: 'array',
            
title: 'Categories',
            
of: [
                {
                
type: 'reference',
                
to: {
                    
type: 'category'
                
}
            
}
        
]
        
},
      
{
        
name: 'description',
        
type: 'string',
        
title: 'Description'
      
},
      
{
        
name: 'author',
        
type: 'reference',
        
title: 'Author',
        
to: {type: 'author'}
      }
    ]
  }
Thanks User — have you added
rescource
to your content schema? And have you added it as a field to any of your document types?
Yes, I imported it to the main schema and I'm beginning to see where I went wrong. I had assumed that the File(Rescource) would be a standalone inpun in studio. I'm going to create a new download schema to include the file in and see if that works.
*input
That sounds like a good direction, as you’ll probably need to include it in a document type to start seeing it in your Studio 🙂
Thanks User. I appreciate your help.
Hey User, I got the fil into a document. Now, I'm trying to figure out how to get the "upload" button like in the image upload. My goal is to make Documents uploadable to a front end that's easy for anyone.Thanks again

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?