You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c84bf7f
Browse filesBrowse the repository at this point in the historyBrowse files
docs: use the run ID as the idempotency key for external services (#4953)
## Summary
Follow-up to
[#4952](#4952). The new
"Side effects inside `run()`" section told readers to derive the
provider's idempotency key from a payload ID, but the more general
answer is the run ID: `ctx.run.id` is stable across every attempt of a
run, is always available, and is the same value the default `run` scope
already mixes into a Trigger.dev idempotency key.
The Stripe refund example now keys the provider call on `ctx.run.id`,
and a short paragraph explains when a business ID from the payload is
the better choice (when the same task can be triggered more than once
for the same order and you want deduplication across separate runs).
Docs only.
Copy file name to clipboardExpand all lines: docs/idempotency.mdx
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ An idempotency key stops a task from being *triggered* twice. It does not stop t
44
44
45
45
This matters most for payments. A refund issued directly inside a retryable task is issued again on every retry, even when the retry is caused by an unrelated failure later in the same function. Moving the refund into a child task and triggering it with an idempotency key does not close the gap either: the child is triggered once, but its own `run()` can retry after the provider has accepted the request. Only the provider can deduplicate its own API call.
46
46
47
-
Pass the provider's idempotency key on every call that must happen at most once. Derive it from a value that stays the same across retries, such as an ID from your payload:
47
+
Pass the provider's idempotency key on every call that must happen at most once. Derive it from a value that stays the same across every attempt of the run. The run ID, `ctx.run.id`, always qualifies: retries are attempts of the same run, so the ID does not change between them. This is the same value the default `run` scope mixes into a Trigger.dev idempotency key.
The run ID protects against retries of one run. If the same task can be triggered more than once for the same order, either trigger it with a Trigger.dev idempotency key so repeated triggers resolve to the same run and the same run ID, or key the provider call on a business ID from your payload instead, such as `refund-${payload.orderId}`, which deduplicates across separate runs too.
74
+
73
75
Use a Trigger.dev idempotency key to stop a retry triggering a task twice, and the provider's idempotency key to stop a retry calling the provider twice. For a side effect with no idempotency support, record that you performed it in your own database and check that record before performing it again.
0 commit comments