Sanity image upload error: Request failed to assets/images API endpoint
Based on your error and the fact that you can create documents but not upload assets, this is likely one of a few common issues with the assets.upload() method:
Most Common Causes:
- API Version Issue: The error shows you're using API version
v2023-05-17. Some older API versions have had issues with asset uploads. Try updating your Sanity client to use a more recent API version or omit it to use the default:
const client = createClient({
projectId: 'your-project-id',
dataset: 'production',
token: 'your-token',
useCdn: false,
apiVersion: '2024-01-01' // or newer
})Token Permissions: Even though you have a token, make sure it has the correct permissions for asset uploads. Your token needs Editor or Administrator permissions. Developer tokens sometimes have restricted scopes. Check in your Sanity Manage under API settings → Tokens.
File Stream Issue: When using
fs.createReadStream(), make sure:- The file path is correct and the file exists
- The file isn't corrupted or too large (Sanity has a 5GB limit per asset)
- The stream is being properly passed to the upload method
Try this more robust version:
const fs = require('fs')
async function uploadImage(localImagePath) {
try {
const imageStream = fs.createReadStream(localImagePath)
const imageAsset = await client.assets.upload('image', imageStream, {
filename: 'your-filename.jpg' // optional but recommended
})
console.log('Upload successful:', imageAsset)
return imageAsset
} catch (error) {
console.error('Upload failed:', error.message)
console.error('Full error:', error)
}
}Network/Firewall Issue: Since document creation works but asset uploads don't, check if there's a firewall or proxy blocking requests to the
/assets/endpoint specifically. Asset uploads go to a different endpoint than document mutations.Client Configuration: Make sure you're using
useCdn: falsewhen uploading assets. The CDN is read-only, so writes need to go directly to the API.
Quick Debugging Steps:
- Update your Sanity client to the latest version:
npm install @sanity/client@latest - Verify your token permissions in Sanity Manage
- Try with a very small test image (< 1MB) to rule out size issues
- Add more detailed error logging to see the full error object
If you're still stuck after trying these, share the full error message including any statusCode or additional details from the error object, and that will help narrow it down further!
Show original thread2 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.