Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Issue with calling the export data API using nodejs and the solution.

8 replies
Last updated: Apr 18, 2024
Anybody know how to get Auth token to then call the export data api using nodejs?
Apr 18, 2024, 1:03 PM
import exportDataset from '@sanity/export';
import {createClient} from '@sanity/client';

const configTemplate = {
    // Instance of @sanity/client configured to correct project ID and dataset
    client: createClient({
        projectId: process.env.projectId,
        dataset: process.env.datasetName,
        useCdn: false, // set to `true` to fetch from edge cache
        apiVersion: '2022-01-12', // use current date (YYYY-MM-DD) to target the latest API version
        token: process.env.API_TOKEN
    }),

    // Name of dataset to export
    dataset: process.env.datasetName,

    // Path to write tar.gz-archive file to, or `-` for stdout
    outputPath: process.env.outputPath,

    // Whether or not to export assets. Note that this operation is currently slightly lossy;
    // metadata stored on the asset document itself (original filename, for instance) might be lost
    // Default: `true`
    assets: true,

    // Exports documents only, without downloading or rewriting asset references
    // Default: `false`
    raw: false,

    // Whether or not to export drafts
    // Default: `true`
    drafts: true,

    // Export only given document types (`_type`)
    // Optional, default: all types
    // types: ['products', 'shops'],

    // Run 12 concurrent asset downloads
    assetConcurrency: 12
};

await exportDataset(configTemplate);
Apr 18, 2024, 1:48 PM
user J
Im getting
exportDataset
is not a function
Apr 18, 2024, 2:13 PM
You need to install.
"@sanity/export"

npm install @sanity/export
Apr 18, 2024, 2:46 PM
I already have. I got version
3.37.3
Apr 18, 2024, 2:46 PM
I'm running 3.34 doubtful. thats the issue
Apr 18, 2024, 2:48 PM
https://github.com/sanity-io/export Maybe try
const exportDataset = require('@sanity/export');
Apr 18, 2024, 2:50 PM
Don't know without seeing your code, even then JS isn't my best language
Apr 18, 2024, 2:50 PM
Yea it needed to be require. That works. Thanks!
Apr 18, 2024, 2:51 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?