Sanity Pioneers: Get early access to betas, extra AI credits, and a direct line to the engineering team. Apply now

How to get Auth token for export data API in Node.js?

8 repliesLast updated: Nov 29, 2025

Anybody know how to get Auth token to then call the export data api using nodejs?
AI Update

To call the Sanity Export Data API using Node.js, you'll need to authenticate your request with an API token. Here's how to get set up:

Creating an API Token

You need to create an API token in your Sanity project management interface:

Using the Token with the Export API

Here's how to call the export API in Node.js:

import https from 'https';

const projectId = 'your-project-id';
const dataset = 'production'; // or your dataset name
const token = process.env.SANITY_API_TOKEN; // Store your token in env vars
const apiVersion = 'v2021-03-25';

const options = {
  hostname: `${projectId}.api.sanity.io`,
  path: `/${apiVersion}/data/export/${dataset}`,
  method: 'GET',
  headers: {
    'Authorization': `Bearer ${token}`
  }
};

const req = https.request(options, (res) => {
  res.on('data', (chunk) => {
    // Process NDJSON stream
    console.log(chunk.toString());
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.end();

Best Practices

Alternative: Use the Sanity CLI

For most export use cases, you might find it easier to use the Sanity CLI's built-in export command, which handles authentication automatically if you're logged in:

sanity dataset export production export.ndjson

The full Export API documentation is available at the Sanity Export API reference.

Show original thread
8 replies

Was this answer helpful?

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.

Related contributions