Sanity Export Documents: OAuth vs Internal API Token Authentication

1 replies
Last updated: Jun 9, 2023
Hi all! We have a Sanity Studio and we want to utilize the export documents endpoint to extract our data. Do we need to set up OAuth authentication or is there a simpler way to do it with an internal API token?
Thank you!!


user B
AI Update

Good news! You don't need to set up OAuth for using the export documents endpoint. A simple internal API token will work perfectly fine.

Here's what you need to do:

Using an API Token with the Export Endpoint

The export endpoint accepts standard API token authentication. You can create a token through your project's API settings and include it in your request using the Authorization header with a Bearer token format.

Creating Your Token

  1. Go to your project at sanity.io/manage or run npx sanity@latest manage from your CLI
  2. Navigate to the "API" tab
  3. Create a new token with appropriate permissions:
    • Viewer role for read-only access (sufficient for exports)
    • Or a custom role if you need specific permissions

Using the Token

Include your token in the Authorization header when making requests:

Authorization: Bearer YOUR_TOKEN_HERE

The endpoint URL format is:

https://{projectId}.api.sanity.io/{apiVersion}/data/export/{dataset}

Important Security Note

Unauthenticated requests will only return publicly available documents. When you include an authentication token, you'll get access to all documents that token has permissions for, including drafts and system documents.

Alternative: Use the CLI or npm Package

For most use cases, you'll probably want to use the Sanity CLI's export functionality (sanity dataset export) or the @sanity/export npm package. These have built-in features like asset downloads, draft inclusion options, and automatic handling of cursor mode for large datasets.

The token approach is much simpler than OAuth and is the standard way to authenticate with Sanity's HTTP API for programmatic access!

Show original thread
1 reply
If your dataset it public, you can export any documents on a root path (meaning there’s no
.
in their
_id
) without making an authenticated request. If your dataset is private or you want your export to include things like drafts you’ll need to make an authenticated request . This is linked at the bottom of the page in that first link you shared if you want to see it in context.

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?