Troubleshooting image upload issues with Sanity's Assets API

6 replies
Last updated: Nov 30, 2024
Hey guys, I've been having trouble uploading images to sanity so I attempted to try it on postman with different pictures as well and I'm still getting this error.
AI Update

I understand you're having trouble uploading images to Sanity through the REST API, both in your application and when testing with Postman. While you didn't mention the specific error you're encountering, let me walk you through the proper way to upload images to Sanity and address common issues.

How to Upload Images via the HTTP API

Based on the Sanity Assets API documentation, here's how to properly upload an image:

Endpoint:

POST https://{projectId}.api.sanity.io/{apiVersion}/assets/images/{dataset}

Required components:

  • Replace {projectId} with your Sanity project ID
  • Replace {apiVersion} with the API version (e.g., v2024-06-24)
  • Replace {dataset} with your dataset name (usually production)
  • Set Content-Type header to match your image type (e.g., image/jpeg, image/png)
  • Include an authorization token in the Authorization: Bearer {token} header
  • Send the image as binary data in the request body

Optional query parameters:

  • filename - Original filename
  • title - Asset title
  • description - Asset description
  • meta - Array of metadata to extract (e.g., palette, lqip, blurhash)

Common Issues and Solutions

1. 413 Payload Too Large Error

If you're getting a 413 error, your image file is likely exceeding Sanity's upload size limits. While Sanity's documentation doesn't explicitly state a hard limit in the Assets API page, this typically means:

  • Try reducing your image file size before uploading
  • Compress your images using tools like ImageOptim, TinyPNG, or similar
  • Consider resizing large images to reasonable dimensions

2. Authentication Issues

Make sure you're using a valid token with write permissions:

  • Go to your Sanity project dashboard
  • Navigate to API settings
  • Create a token with "Editor" or "Administrator" permissions
  • Include it in the Authorization: Bearer YOUR_TOKEN header

3. Postman Configuration

Here's how to set up your Postman request correctly:

Method: POST

URL: https://YOUR_PROJECT_ID.api.sanity.io/v2024-06-24/assets/images/YOUR_DATASET

Headers:

Authorization: Bearer YOUR_TOKEN
Content-Type: image/jpeg

Body:

  • Select "binary"
  • Choose your image file

Query Parameters (optional):

  • filename: my-image.jpg
  • title: My Image Title

4. Content-Type Header

Make sure your Content-Type header matches your actual image format:

  • image/jpeg for JPG/JPEG files
  • image/png for PNG files
  • image/webp for WebP files
  • image/gif for GIF files

5. Using the JavaScript Client (Alternative)

If the HTTP API continues giving you trouble, consider using the official JavaScript client which handles uploads more reliably:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  token: 'your-token',
  apiVersion: '2024-06-24',
})

// Upload from a file input or blob
client.assets
  .upload('image', fileData, {
    filename: 'my-image.jpg',
    title: 'My Image'
  })
  .then(document => {
    console.log('Upload successful:', document)
  })
  .catch(error => {
    console.error('Upload failed:', error)
  })

Debugging Steps

  1. Check your image file size - try with a very small image (under 1MB) first
  2. Verify your project ID, dataset name, and API version are correct
  3. Confirm your token has the necessary permissions
  4. Test with different image formats
  5. Check the response body for specific error messages

If you share the specific error message you're seeing, I can provide more targeted guidance!

I see the one file is a jpeg, but your assigning to a png file. If you leave the format the same, does it work as expected?
the second one isnt a real image and i can remove it and it still won't work
Using the
form-data
type will (I assume) send the header
Content-Type: multipart/form-data
, which isn’t what the Assets API expects . I would try changing the body type to
binary
.
Thank you for the help!
It works now
Great!

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?