Skip to content

Idempotency

Idempotency is an endpoint-specific contract. BatchJob start, schedule trigger and BI submissions preserve one accepted handle when a response is lost. Sending an Idempotency-Key to an ordinary write does not make it safe to retry.

Operation Key Exact recovery
POST /api/v1/batch-jobs/start Required; 1–256 characters, not UUID-only GET /api/v1/batch-job-runs?submissionKey=…&entryMode=api
POST /api/v1/schedules/{id}/trigger Required; 1–256 characters, not UUID-only GET /api/v1/batch-job-runs?submissionKey=…&entryMode=schedule_run_now
POST /api/v1/bi/queries (SQL or structured definition) UUID Idempotency-Key GET /api/v1/bi/queries?submissionKey=…
POST /api/v1/bi/saved-queries/{id}/run UUID Idempotency-Key GET /api/v1/bi/queries?submissionKey=…

The SDK generates one UUID before a submission when you omit a key, preserves caller-supplied keys and reuses the same key for every transport attempt. Independent deliberate invocations get different keys. Reusing an accepted key with changed intent returns a conflict rather than silently launching new work. A BatchJob replay returns the original run’s current summary, including a terminal outcome; it does not recreate the run.

Keep the key and normalized request intent before sending when recovery must survive a process restart. Resolve an uncertain submission before changing its intent or choosing a new key. An abort after dispatch stops the client wait; it does not cancel work already accepted by the server.

The SDK exports BatchJobSubmissionIndeterminateError. It carries submissionKey, entryMode: 'api', operation: 'batchJobs.start', source and dispatched: true when acceptance may have committed but no usable handle was received. An abort before send instead remains a JustCrawlAbortError with dispatched: false. Definitive validation, conflict and quota errors retain their normal SDK error types when no earlier attempt was ambiguous. A lost response, timeout or possibly accepted server failure remains uncertain even when a later retry returns 401/403, 402 or conflict. The indeterminate error preserves the original key/source and final HTTP cause. Restore authentication or permissions when needed, then recover the original key before starting new work. The later refusal stops automatic retries; it does not establish the earlier attempt’s outcome.

import { BatchJobSubmissionIndeterminateError } from '@justcrawl/sdk';
try {
await jc.batchJobs.start(intent, { idempotencyKey: submissionKey });
} catch (error) {
if (!(error instanceof BatchJobSubmissionIndeterminateError)) throw error;
const recovered = await jc.batchJobRuns.list({
submissionKey: error.submissionKey,
entryMode: error.entryMode,
});
console.log(recovered.items[0] ?? 'No acceptance committed; repeat the same intent with the same key.');
}

Exact-key recovery returns zero or one full run summary independently of recent history. It checks the authenticated principal and cannot be combined with a cursor, limit or history filter. Zero matches means acceptance did not commit: repeat the same intent with the same key. Do not generate a new key to recover an uncertain start. Count and confirm large work before the initial start; see Batch jobs.

The MCP start tool owns a key before dispatch and returns it on an indeterminate outcome. Follow its jc_batch_runs_get next step with that key and entryMode: api; the same zero-match rule applies.

Keep the schedule ID, maxJobs intent and key before sending:

const intent = { maxJobs: 500 };
try {
const run = await jc.schedules.trigger(scheduleId, intent, { idempotencyKey: submissionKey });
console.log(run.id, run.replayed, run.outcome, run.skipReason);
} catch (error) {
if (!(error instanceof BatchJobSubmissionIndeterminateError)) throw error;
const recovered = await jc.batchJobRuns.list({
submissionKey: error.submissionKey,
entryMode: error.entryMode,
});
console.log(recovered.items[0] ?? 'Repeat the same intent with the same key.');
}

The same error class carries operation: 'schedules.trigger', entryMode: 'schedule_run_now' and source: { scheduleId }. The SDK generates a key before dispatch when omitted and keeps the normalized key and body unchanged through retries. Changed intent, including a different maxJobs, conflicts under a used key. Replay finds the original current summary before mutable schedule, email or quota checks, including after edits or deletion.

An overlap or unresolved workflow returns a durable 202 skipped run. A skip consumes its key: retrying it returns the same skip. After the blocking run settles or the workflow is fixed, choose a new key for a deliberate new trigger. This differs from uncertain acceptance with zero exact matches, where repeating the original intent with the original key is safe.

jc_schedules_trigger owns its key before SDK dispatch, returns it on uncertainty and names jc_batch_runs_get with entryMode: schedule_run_now. Exact recovery also accepts schedule_occurrence for server-owned cron receipts. Keep the namespace with the key: the same key in api, schedule_run_now and schedule_occurrence names separate operations. Recovery is a read; it never lets a caller choose a trusted start mode. See Schedules.

Preserve a UUID in the Idempotency-Key header or SDK idempotencyKey option, then call jc.bi.listQueries({ submissionKey }) to find the retained execution. Exact lookup is organization-scoped and independent of the recent-history limit. Once you have a query ID, continue result reads rather than resubmitting.

BI does not use the BatchJob-specific SDK error class. Its MCP submission tools own their key and return submissionStatus: "indeterminate" with a jc_bi_list_queries next step if all responses are lost. Follow that exact-key lookup. See Agents & MCP for BI result continuation.

The SDK does not automatically retry ordinary POST, PATCH, PUT or DELETE calls. In particular:

  • Submitting POST /api/v1/jobs twice creates two jobs and can charge twice.
  • Definition create/update, durable-input create/append/seal, URL creation and schedule create/update/toggle/delete do not join the automatic retry allowlist. Replaying an unchanged input chunk under the same input ID and ordinal is explicitly safe under the chunk-receipt contract, but the SDK does not retry that write for you.

If an ordinary write loses its response, inspect the resulting resource before retrying. A list-then-submit check cannot provide atomic deduplication. Serialize uncertain writes on your side when needed; do not rely on a header that the endpoint does not support.

Safe reads and the keyed submissions above retry network errors, HTTP 5xx and 429 with a bounded retry budget. The SDK honors a numeric Retry-After when present; otherwise it uses capped exponential backoff with jitter. Keep the same key and intent throughout the retry ladder.

HTTP 402 is never retried. Add credits or change the request after a definitive quota refusal; replay recovery for an already accepted BatchJob remains a read. Other definitive validation, permission and conflict errors need their stated remedy. A provider-failed crawl may be refunded under the billing policy, but a second successful submission is still separate paid work.