Troubleshooting image upload issues with Sanity's Assets API
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 (usuallyproduction) - Set
Content-Typeheader 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 filenametitle- Asset titledescription- Asset descriptionmeta- 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_TOKENheader
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.jpgtitle:My Image Title
4. Content-Type Header
Make sure your Content-Type header matches your actual image format:
image/jpegfor JPG/JPEG filesimage/pngfor PNG filesimage/webpfor WebP filesimage/giffor 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
- Check your image file size - try with a very small image (under 1MB) first
- Verify your project ID, dataset name, and API version are correct
- Confirm your token has the necessary permissions
- Test with different image formats
- Check the response body for specific error messages
If you share the specific error message you're seeing, I can provide more targeted guidance!
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.