Where to place data import file and why documents aren't appearing in Studio

1 replies
Last updated: Oct 2, 2020
Hello, I'm following the guide for importing data from an external source (https://www.sanity.io/guides/guide-importing-data-from-external-sources ) but I'm a little confused about where that actual file should be saved in the project structure. I've just put a file called 'dataCall.js' in the schema's folder and made an import call for this within the schema.js file. For the moment, I'm getting an API call to work fine and I'm creating the document but nothing is happening in the studio - there are no documents created (visually at least). On the query side, the data is there, it's just not creating the documents. Perhaps I'm missing a step here? How does Sanity create a component from this procedure? Thanks for any tips and help.
AI Update

Looking at the guide you're following, I can help clarify the confusion about file placement and why your documents aren't appearing in Studio.

Where to put your import script:

Your import script (dataCall.js) should NOT go in the schema folder. The schema folder is specifically for defining your content types. Instead, you should:

  1. Create your import script in the root of your Sanity project or in a separate /scripts folder
  2. Run it as a standalone Node.js script from your terminal, not as part of the schema

For example:

my-sanity-project/
├── schemas/
│   └── schema.js (your schema definitions only)
├── scripts/
│   └── dataCall.js (your import script)
└── sanity.config.js

Why documents aren't showing in Studio:

The most likely issue is that your document _type doesn't match your schema. When you create documents, the _type field must exactly match a document type name defined in your schema. For example:

// In your schema
{
  type: 'document',
  name: 'catBreed',  // This name matters!
  fields: [...]
}

// In your import script
{
  _id: 'imported-cat-1',
  _type: 'catBreed',  // Must match the schema name exactly
  // ... other fields
}

How to properly run the import:

  1. Install the Sanity client in your project:
npm install @sanity/client node-fetch
  1. Create a separate import script (e.g., scripts/import.js):
const sanityClient = require('@sanity/client')
const fetch = require('node-fetch')

const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  token: 'your-write-token',
  useCdn: false
})

// Your fetch and transform logic here
// Then use client.createOrReplace() to create documents
  1. Run it from your terminal:
node scripts/import.js

Common gotchas:

  • Make sure your token has write permissions (generate one at manage.sanity.io)
  • Verify the _type in your documents matches your schema exactly (case-sensitive)
  • Check that your schema is properly registered in sanity.config.js
  • If documents exist in queries but not in Studio, restart your Studio (sanity dev)

The guide you're following shows the complete process - the key is that importing is a separate operation from defining schemas. You define the schema first, then run a standalone script to populate data that conforms to that schema.

Show original thread
1 reply
OK, I've worked it out. I hadn't realised that you have to define the content model first. Makes sense now.

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?