✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Error encountered when trying to upload an image through code in Sanity.io

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.
Mar 12, 2024, 1:01 PM
The problem was with the localImagePath, use :
const imageData = fs.readFileSync(localImagePath);
Mar 12, 2024, 3:13 PM
And then:
await _sanityClient_.assets.upload('image', fs.createReadStream(imageData)).then((_data_) _=>_ {

console.log(_data_)

})

.catch((_error_) _=>_ {

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

});
Mar 12, 2024, 3:14 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?