Sanity image upload error: Request failed to assets/images API endpoint

2 replies
Last updated: Mar 12, 2024
Hey Everyone! When I try to upload a file through this code:

await _sanityClient_.assets.upload('image', fs.createReadStream(localImagePath)).then((_data_) _=>_ {

console.log(_data_)

})

.catch((_error_) _=>_ {

console.log("Upload failed: ", _error_)

});

It gives me an error : Request error while attempting to reach
https://projectID.api.sanity.io/v2023-05-17/assets/images/production
Any help on this?

I'm using a token for developer so I have to be able to connect to the api and only happens when trying to upload an image not creating documents.
AI Update

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:

  1. 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
})
  1. 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.

  2. 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)
  }
}
  1. 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.

  2. Client Configuration: Make sure you're using useCdn: false when uploading assets. The CDN is read-only, so writes need to go directly to the API.

Quick Debugging Steps:

  1. Update your Sanity client to the latest version: npm install @sanity/client@latest
  2. Verify your token permissions in Sanity Manage
  3. Try with a very small test image (< 1MB) to rule out size issues
  4. 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 thread
2 replies
The problem was with the localImagePath, use :
const imageData = fs.readFileSync(localImagePath);
And then:
await _sanityClient_.assets.upload('image', fs.createReadStream(imageData)).then((_data_) _=>_ {

console.log(_data_)

})

.catch((_error_) _=>_ {

console.log("Upload failed: ", _error_)

});

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?