Discussion on importing a link from a database to a project using Next.js and client.fetch
The easiest and quickest way to import data into your Sanity project depends on what you're importing, but here are your main options:
Quickest: Using the Sanity CLI (Recommended)
If you have data in NDJSON format (newline-delimited JSON), the CLI import command is your fastest route:
sanity dataset import <file> <targetDataset>For example:
sanity dataset import my-data.ndjson productionKey things to know:
- Each line in your NDJSON file should be a complete JSON document
- Documents need a
_typefield (and optionally an_id) - For images/files, use the special
_sanityAssetproperty:"image@file:///path/to/image.jpg"or"image@https://example.com/image.jpg" - You can import
.ndjsonfiles or.tar.gzarchives (which include assets)
Useful flags:
--replace- Overwrites existing documents with the same_id--missing- Only creates documents that don't exist yet--allow-failing-assets- Continues import even if some assets fail
Run sanity dataset import --help to see all options.
Alternative: Using a Client Library
If you need more control or are importing from a custom source, you can use the Sanity JavaScript client with methods like client.create() or client.createOrReplace().
Important considerations when using the client:
- Implement rate limiting (max 25 requests/second)
- Use transactions for multiple mutations
- Set visibility mode to
deferredfor large imports - Upload assets first, then reference them in documents
For a single link/document, you could also just create it directly in Sanity Studio if it's a one-off!
What type of data are you looking to import? That'll help determine the best approach.
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.