Skip to content

Instantly share code, notes, and snippets.

@joeyates
Last active August 14, 2017 12:57
Show Gist options
  • Save joeyates/0d1b25ebc2d620aab30799124dffdbca to your computer and use it in GitHub Desktop.
Save joeyates/0d1b25ebc2d620aab30799124dffdbca to your computer and use it in GitHub Desktop.
# Upload an item with an image field
export DATO_CMS_API=https://site-api.datocms.com
export DATO_CMS_API_TOKEN={{your read-write API token}}
# Get info about your Models (item-types):
curl \
-H "Authorization: Api-Key $DATO_CMS_API_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
$DATO_CMS_API/item-types
# Take note of the item type id for the item you wish to create from the response
# Get a signed S3 URL for the image upload:
curl \
-H "Authorization: Api-Key $DATO_CMS_API_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
$DATO_CMS_API/upload-requests \
-d '{"data": {"type": "upload_request", "attributes": {"filename": "image.jpg"}}}' \
| sed -r -e 's/\\u0026/\&/g; s/%3B/;/g; s/%2F/\//g'
# (the sed invocation does URL decoding)
# Take note of the id from the response
# Upload to S3:
curl \
-X PUT \
-H "x-amz-acl: public-read" \
-T {{path to file}} \
{{url from DatoCMS response}}
# The header '-H "x-amz-acl: public-read"' is essential as it is part of the signature
curl \
-H "Accept: application/json" \
-H "Authorization: Api-Key $DATO_CMS_API_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
https://site-api.datocms.com/items \
-d '{
"data": {
"type": "item",
"attributes": {
"image": {
"path": "{{the id from the upload-request}}",
"size": "{{file size in bytes}}",
"format": "jpg",
"width": 200,
"height": 200
}
},
"relationships": {
"item_type": {
"data": {
"type": "item_type",
"id": "{{the item type}}"
}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment