Skip to content

Instantly share code, notes, and snippets.

@slint
Created February 11, 2020 12:15
Show Gist options
  • Save slint/46c71cf0bab9d66f6f33d4e61a282583 to your computer and use it in GitHub Desktop.
Save slint/46c71cf0bab9d66f6f33d4e61a282583 to your computer and use it in GitHub Desktop.
Zenodo REST API example in cURL
#!/bin/bash
BASE_URL="https://sandbox.zenodo.org"
API_TOKEN="<your-token>"
# 1. Create the first version
curl -i -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
"$BASE_URL/api/deposit/depositions" \
--data @- <<'EOF'
{
"metadata": {
"title": "DMP First version",
"upload_type": "publication",
"publication_type": "datamanagementplan",
"description": "This is the first version of the DMP",
"creators": [{"name": "Doe, John", "affiliation": "CERN"}]
}
}
EOF
# 201 CREATED
# {
# "id": 123,
# "links": {
# "bucket": "https://sandbox.zenodo.org/api/files/b799cb5a-c607-42a0-942c-0548f2a13d02",
# ...
# },
# ...
# }
# Store the deposition ID and bucket URL (for file uploads)
DEPOSIT_ID_V1="123"
BUCKET_URL="https://sandbox.zenodo.org/api/files/a4887fcd-65a4-46c0-b8f3-4ee312fc5243"
# 2. Upload the file
# Note: I named the file "dmp-v1.pdf" in this case, but this could be anything
curl -i -X PUT \
-H "Authorization: Bearer $API_TOKEN" \
"$BUCKET_URL/dmp-v1.pdf" \
--upload-file "/path/to/your/dmp.pdf"
# 201 CREATED
# { ... }
# 3. Publish the record
curl -i -X POST \
-H "Authorization: Bearer $API_TOKEN" \
"$BASE_URL/api/deposit/depositions/$DEPOSIT_ID_V1/actions/publish"
# 200 OK
# {
# ...
# "doi": "10.5072/zenodo.123",
# "links": {
# "newversion": "https://sandbox.zenodo.org/api/deposit/depositions/123/actions/newversion",
# "record_html": "https://sandbox.zenodo.org/record/123",
# ...
# }
# }
# 4. Create a new version
curl -i -X POST \
-H "Authorization: Bearer $API_TOKEN" \
"$BASE_URL/api/deposit/depositions/$DEPOSIT_ID_V1/actions/newversion"
# 201 CREATED
# {
# ...
# "links": {
# "latest_draft": "https://sandbox.zenodo.org/api/deposit/depositions/456"
# ...
# }
# }
# We care about the "links.latest_draft" URL, which has the new deposit version's ID
DEPOSIT_ID_V2="456"
# 5. Update the new version's metadata (optional)
curl -i -X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
"$BASE_URL/api/deposit/depositions/$DEPOSIT_ID_V2" \
--data @- <<'EOF'
{
"metadata": {
"title": "DMP Second version",
"upload_type": "publication",
"publication_type": "datamanagementplan",
"description": "This is the second version of the DMP",
"creators": [{"name": "Doe, John", "affiliation": "CERN"}]
}
}
EOF
# 201 CREATED
# {
# "id": 456,
# "links": {
# "bucket": "https://sandbox.zenodo.org/api/files/97be6594-4ffb-4fb0-8feb-03cfdc0ab3d4",
# ...
# },
# ...
# }
# Keep the new bucket URL
BUCKET_URL="https://sandbox.zenodo.org/api/files/97be6594-4ffb-4fb0-8feb-03cfdc0ab3d4"
# 6. Update the file(s) of the new version.
# Note: This is a mandatory step, each version should have a unique set of files
# Update the existing file
curl -i -X PUT \
-H "Authorization: Bearer $API_TOKEN" \
"$BUCKET_URL/dmp-v2.pdf" \
--upload-file "/path/to/your/dmp-changed.pdf"
# 200 OK
# { ... }
# If you want to delete a file:
curl -i -X DELETE \
-H "Authorization: Bearer $API_TOKEN" \
"$BUCKET_URL/some-other.txt"
# 7. Publish the new version
curl -i -X POST \
-H "Authorization: Bearer $API_TOKEN" \
"$BASE_URL/api/deposit/depositions/$DEPOSIT_ID_V2/actions/publish"
# 200 OK
# {
# ...
# "doi": "10.5072/zenodo.456",
# "links": {
# "newversion": "https://sandbox.zenodo.org/api/deposit/depositions/456/actions/newversion",
# "record_html": "https://sandbox.zenodo.org/record/456",
# ...
# }
# }
# Note: to create a new version you can repeat from step 4. It doesn't actually
# matter which deposit version ID you use for the
# "POST /api/deposit/depositions/<deposit-id>/actions/newversion" call, in any
# case the "links.latest_draft", will always point to the newest draft version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment