Get a peek at our latest innovations at Sanity Product Day on Dec 8th →

Can I upload images to a specific document via the API?

2 replies
Last updated: Sep 10, 2021

Hi everyone, from my research it appears that one cant upload an image file to a specific document via the API, one can only upload image assets to the CMS, and then link documents to them manually? am I correct?

Sep 10, 2021, 5:23 AM

If you’re defining documents through the API this is correct.Ie, you upload the image to the asset api, await the request and then put the

_id
the api returns to you into a field of the form:
"nameOfImageField":  {
    "_type": "image",
    "alt": "alt-text of image",
    "asset": {
      "_ref": "imageId",
      "_type": "reference"
    }
  }
on the document and then upload your document.
If you’re uploading through the Studio this happens behind the scenes automatically when you choose “upload image” on an “image” field of a document.

Sep 10, 2021, 6:53 AM

yeah I am trying to do as much as I can through the API, thank you!

Sep 10, 2021, 6:58 AM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Free to get started, and pay-as-you-go on all plans. Find out more.