Uploading an image as an asset from a URL and using createIfNotExists and createOrReplace in the Sanity API.
13 replies
Last updated: Jan 27, 2021
A
I'm using this command to upload an image from the airtable code block as an asset:
but I'm getting this error:
Can anyone help how to upload an image as an asset from a url string?
curl \ -X POST \ -H 'Content-Type: image/jpeg' \ --data-binary "<https://dl.airtable.com/.attachments/image.jpg>" \
{statusCode: 400, error: "Bad Request", message: "Invalid image, could not read metadata", details: "Input buffer contains unsupported image format"}
Jan 22, 2021, 4:57 PM
P
Hi Aldrin - this seems like an issue with not having a readable stream for the image. Could it be an issue with access to the Airtable image, e.g. needing a token? Does the issue persist if you try with a publicly available image?
Jan 22, 2021, 6:01 PM
A
user M
hey dude thanks for replying. the image in the url is publicly available, i just tried in incognitoJan 25, 2021, 7:18 AM
P
Thanks Aldrin - looks like the issue is different.
Hope that helps!
curlhere relies on using a local file as shown by the
@/Users/mike/images/bicycle.jpgportion of the example. When inserting a remote URL instead,
curldoesn't fetch and upload, so we're essentially sending a URL as image content. To create a readable stream, an extra step is necessary before including it in the POST request. For example:
// Fetch the original image fetch('<https://dl.airtable.com/.attachments/image.jpg>') // Retrieve its body as ReadableStream .then(response => response.body)
Jan 25, 2021, 9:27 AM
A
thanks
user M
, i'll try this oneJan 25, 2021, 9:33 AM
A
user M
hi peter, thank you for your suggestion. its working now. cheers!Jan 25, 2021, 3:38 PM
P
Awesome, Aldrin! Glad to hear š
Jan 25, 2021, 3:41 PM
A
user M
i do have another question. in the mutations api (https://www.sanity.io/docs/http-mutations ), i'm trying to use createIfNotExists, what does the api check if the record exists? the
_id?
Jan 25, 2021, 3:50 PM
A
is it also the same case if i try to use
createOrReplace? i need to pass the sanity id?
Jan 25, 2021, 3:51 PM
P
That's correct, it checks if any document with the provided
Specifically,
Hope that clarifies things
š
_idexists. If you use
createIfNotExists, a new document will only be created if a document with a given
_iddoes not exist yet.
createOrReplace, on the other hand, creates (or sets) the document even if a document with the same
_idalready exists, thereby replacing it.
Specifically,
createOrReplacechecks if the
_idand the
_typeexist. If both are the same, it acts as a
setpatch to replace the document's entire contents. If the
_idis the same but the
_typeis different, it deletes the old doc and then creates the new one. The result of both is the same but the procedure behind the scenes is slightly different.
Hope that clarifies things
š
Jan 25, 2021, 3:58 PM
A
user M
if i pass nullas an id for
createIfNotExists, it should be an automatic create, right?
Jan 25, 2021, 4:04 PM
A
also, on succesful api call, is the
transactionIdthe sanity id of the document?
Jan 25, 2021, 4:48 PM
M
hey
user M
! any ideas for this? the transactionid mentioned seems to be the _revrevision id instead of the document id. How can we get the
_idto postback to Airtable?
Jan 27, 2021, 4:46 AM
P
If no ID is provided, it should generate a new, unique ID for you - but only if you use
As Micaela suggested,
If you need to return the document
createand not
createIfNotExistsin this case. The latter requires an explicit
_id.
As Micaela suggested,
transactionIdis the
_rev(revision) and not the document
_id.
If you need to return the document
_idto Airtable, you could consider fetching the document after creation, using a query like:
*[_rev == "<transactionId>"][0]_.id
Jan 27, 2021, 9:04 AM
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.