
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeWhen creating or updating 100+ documents with the Sanity JS client, a naive loop with client.create() will quickly hit rate limits (max 25 requests per second), which is exactly what you're experiencing. Here are the best practices:
Sanity's API has a rate limit of 25 requests per second. When you fire off 100+ simultaneous client.create() calls, you'll blow past this limit and start seeing failures.
The most efficient approach combines throttling with transactions to batch documents into groups. This example uses p-throttle:
const pThrottle = require('p-throttle')
const persistBatch = pThrottle(
// Batch documents into transactions of 10
batch => batch.reduce(
(trx, doc) => trx.createOrReplace(doc),
client.transaction()
).commit(),
// Max 20 requests per second
20,
1000 // 1 second window
)
// Split your documents into batches of 10
let batch = []
for (let i = 0; i < documents.length; i++) {
batch.push(documents[i])
if (batch.length === 10 || (i === documents.length - 1 && batch.length > 0)) {
persistBatch(batch)
batch = []
}
}This approach is optimal because:
If your documents are large or you prefer simpler code:
const pThrottle = require('p-throttle')
const persistDocument = pThrottle(
doc => client.createOrReplace(doc, {visibility: 'async'}),
20, // Max 20 requests
1000 // Within 1 second
)
await Promise.all(documents.map(persistDocument))Note the {visibility: 'async'} option - this returns immediately when changes are committed rather than waiting for them to be queryable, giving you better performance.
For one-time imports or very large datasets (1000+ documents), use the Sanity CLI with NDJSON files:
# Export your data as NDJSON (newline-delimited JSON)
# Each line should be a complete document
sanity dataset import data.ndjson productionThis bypasses rate limits and is much faster for bulk operations.
{visibility: 'async'} or 'deferred' for better performance when you don't need immediate query visibilityp-throttle is recommended, but throttled-queue also worksThe throttling + batched transactions approach gives you the best balance of speed, reliability, and staying within API limits for most use cases involving 100+ documents.
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.
Content backend


The only platform powering content operations


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store