👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Export

Export all the documents in a dataset

The Export API allows you to export all the non-deleted documents in a dataset, including drafts and asset documents.

GET /v2021-06-07/data/export/:dataset

By default, this endpoint returns all the documents a user can access as an NDJSON stream. The documents will be in the state they were when the request was made and will not be affected by any changes in the dataset that happened after the request was made.

Protip

In most cases, you probably want to use the CLI tool’s export functionality or the JavaScript npm package. Those have built-in features such as asset downloads and an option for including drafts.

Cursor mode for large datasets

For datasets with many documents (typically tens of thousands), you can experience that network or other instabilities cause the export stream to time out or be interrupted. In these cases, you can send a request a nextCursor query parameter set to an empty string. This will cause the export to run for 30 seconds and end with an object with a nextCursor key and value. The cursor is an encoded representation of a position in an ordered document list of the dataset.

You can initiate a new export request with this value as the nextQuery parameter value to continue the export manually.

Gotcha

Note that any changes made between requests in "cursor mode" will be reflected in the export, which can lead to inconsistencies, for example, if references have been changed in the dataset.

Protip

An unauthenticated request will only return publicly available documents. If you include an authentication token, it will consist of documents that this token can access (for example, drafts and system documents).

Note that if export completes successfully before the 30s timeout occurs, no cursor is written to stream as the export is done.

Example

A document stream in cursor mode can look like this:

{"_id":"doc-5","_type":"counter","count":10}
{"_id":"doc-6","_type":"counter","count":11}
{"nextCursor":"taZ9fp"}

In this case, you would initiate another export API request with the cursor ?nextCursor=taZ9fp to resume this export.

Query Parameters

  • typesstring

    Ccomma-separated document types to export

  • nextCursorstring

    Cursor to continue the export from. Set to an empty string to initiate an export in "cursor mode".

Example

Return all the documents <token> has access to:

curl -H "Authorization: Bearer <token>" "https://exx11uqh.api.sanity.io/v2021-06-07/data/export/test

Return all the documents with specific document types:

curl -H "Authorization: Bearer <token>" "https://exx11uqh.api.sanity.io/v2021-06-07/data/export/test

Write all the documents in a dataset to a file:

curl -H "Authorization: Bearer <token>" "https://exx11uqh.api.sanity.io/v2021-06-07/data/export/test

Was this article helpful?