@trigger.dev/sdk 4.5.0-rc.5 → 4.5.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/dist/commonjs/v3/ai.d.ts +178 -5
  2. package/dist/commonjs/v3/ai.js +603 -119
  3. package/dist/commonjs/v3/ai.js.map +1 -1
  4. package/dist/commonjs/v3/chat-client.js +3 -0
  5. package/dist/commonjs/v3/chat-client.js.map +1 -1
  6. package/dist/commonjs/v3/chat-react.js +10 -7
  7. package/dist/commonjs/v3/chat-react.js.map +1 -1
  8. package/dist/commonjs/v3/chat-server.d.ts +8 -0
  9. package/dist/commonjs/v3/chat-server.js +32 -10
  10. package/dist/commonjs/v3/chat-server.js.map +1 -1
  11. package/dist/commonjs/v3/chat-server.test.js +51 -0
  12. package/dist/commonjs/v3/chat-server.test.js.map +1 -1
  13. package/dist/commonjs/v3/chat.js +34 -6
  14. package/dist/commonjs/v3/chat.js.map +1 -1
  15. package/dist/commonjs/v3/chat.test.js +53 -0
  16. package/dist/commonjs/v3/chat.test.js.map +1 -1
  17. package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
  18. package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
  19. package/dist/commonjs/v3/sessions.d.ts +11 -6
  20. package/dist/commonjs/v3/sessions.js +10 -5
  21. package/dist/commonjs/v3/sessions.js.map +1 -1
  22. package/dist/commonjs/v3/test/mock-chat-agent.d.ts +6 -0
  23. package/dist/commonjs/v3/test/mock-chat-agent.js +1 -0
  24. package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
  25. package/dist/commonjs/version.js +1 -1
  26. package/dist/esm/v3/ai.d.ts +178 -5
  27. package/dist/esm/v3/ai.js +603 -120
  28. package/dist/esm/v3/ai.js.map +1 -1
  29. package/dist/esm/v3/chat-client.js +3 -0
  30. package/dist/esm/v3/chat-client.js.map +1 -1
  31. package/dist/esm/v3/chat-react.js +10 -7
  32. package/dist/esm/v3/chat-react.js.map +1 -1
  33. package/dist/esm/v3/chat-server.d.ts +8 -0
  34. package/dist/esm/v3/chat-server.js +32 -10
  35. package/dist/esm/v3/chat-server.js.map +1 -1
  36. package/dist/esm/v3/chat-server.test.js +51 -0
  37. package/dist/esm/v3/chat-server.test.js.map +1 -1
  38. package/dist/esm/v3/chat.js +34 -6
  39. package/dist/esm/v3/chat.js.map +1 -1
  40. package/dist/esm/v3/chat.test.js +53 -0
  41. package/dist/esm/v3/chat.test.js.map +1 -1
  42. package/dist/esm/v3/createStartSessionAction.test.js +30 -0
  43. package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
  44. package/dist/esm/v3/sessions.d.ts +11 -6
  45. package/dist/esm/v3/sessions.js +10 -5
  46. package/dist/esm/v3/sessions.js.map +1 -1
  47. package/dist/esm/v3/test/mock-chat-agent.d.ts +6 -0
  48. package/dist/esm/v3/test/mock-chat-agent.js +1 -0
  49. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
  50. package/dist/esm/version.js +1 -1
  51. package/docs/ai/prompts.mdx +430 -0
  52. package/docs/ai-chat/actions.mdx +115 -0
  53. package/docs/ai-chat/anatomy.mdx +71 -0
  54. package/docs/ai-chat/backend.mdx +817 -0
  55. package/docs/ai-chat/background-injection.mdx +221 -0
  56. package/docs/ai-chat/changelog.mdx +850 -0
  57. package/docs/ai-chat/chat-local.mdx +174 -0
  58. package/docs/ai-chat/client-protocol.mdx +1081 -0
  59. package/docs/ai-chat/compaction.mdx +411 -0
  60. package/docs/ai-chat/custom-agents.mdx +364 -0
  61. package/docs/ai-chat/error-handling.mdx +415 -0
  62. package/docs/ai-chat/fast-starts.mdx +672 -0
  63. package/docs/ai-chat/frontend.mdx +580 -0
  64. package/docs/ai-chat/how-it-works.mdx +230 -0
  65. package/docs/ai-chat/lifecycle-hooks.mdx +530 -0
  66. package/docs/ai-chat/mcp.mdx +101 -0
  67. package/docs/ai-chat/overview.mdx +90 -0
  68. package/docs/ai-chat/patterns/branching-conversations.mdx +284 -0
  69. package/docs/ai-chat/patterns/code-sandbox.mdx +126 -0
  70. package/docs/ai-chat/patterns/database-persistence.mdx +414 -0
  71. package/docs/ai-chat/patterns/human-in-the-loop.mdx +275 -0
  72. package/docs/ai-chat/patterns/large-payloads.mdx +169 -0
  73. package/docs/ai-chat/patterns/oom-resilience.mdx +120 -0
  74. package/docs/ai-chat/patterns/persistence-and-replay.mdx +211 -0
  75. package/docs/ai-chat/patterns/recovery-boot.mdx +230 -0
  76. package/docs/ai-chat/patterns/skills.mdx +221 -0
  77. package/docs/ai-chat/patterns/sub-agents.mdx +383 -0
  78. package/docs/ai-chat/patterns/tool-result-auditing.mdx +148 -0
  79. package/docs/ai-chat/patterns/trusted-edge-signals.mdx +337 -0
  80. package/docs/ai-chat/patterns/version-upgrades.mdx +172 -0
  81. package/docs/ai-chat/pending-messages.mdx +343 -0
  82. package/docs/ai-chat/prompt-caching.mdx +206 -0
  83. package/docs/ai-chat/quick-start.mdx +161 -0
  84. package/docs/ai-chat/reference.mdx +909 -0
  85. package/docs/ai-chat/server-chat.mdx +263 -0
  86. package/docs/ai-chat/sessions.mdx +333 -0
  87. package/docs/ai-chat/testing.mdx +682 -0
  88. package/docs/ai-chat/tools.mdx +191 -0
  89. package/docs/ai-chat/types.mdx +242 -0
  90. package/docs/ai-chat/upgrade-guide.mdx +515 -0
  91. package/docs/apikeys.mdx +54 -0
  92. package/docs/building-with-ai.mdx +261 -0
  93. package/docs/bulk-actions.mdx +49 -0
  94. package/docs/changelog.mdx +6 -0
  95. package/docs/cli-deploy-commands.mdx +9 -0
  96. package/docs/cli-dev-commands.mdx +9 -0
  97. package/docs/cli-dev.mdx +8 -0
  98. package/docs/cli-init-commands.mdx +58 -0
  99. package/docs/cli-introduction.mdx +25 -0
  100. package/docs/cli-list-profiles-commands.mdx +42 -0
  101. package/docs/cli-login-commands.mdx +33 -0
  102. package/docs/cli-logout-commands.mdx +33 -0
  103. package/docs/cli-preview-archive.mdx +59 -0
  104. package/docs/cli-promote-commands.mdx +9 -0
  105. package/docs/cli-switch.mdx +43 -0
  106. package/docs/cli-update-commands.mdx +42 -0
  107. package/docs/cli-whoami-commands.mdx +33 -0
  108. package/docs/community.mdx +6 -0
  109. package/docs/config/config-file.mdx +602 -0
  110. package/docs/config/extensions/additionalFiles.mdx +38 -0
  111. package/docs/config/extensions/additionalPackages.mdx +40 -0
  112. package/docs/config/extensions/aptGet.mdx +34 -0
  113. package/docs/config/extensions/audioWaveform.mdx +20 -0
  114. package/docs/config/extensions/custom.mdx +380 -0
  115. package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
  116. package/docs/config/extensions/esbuildPlugin.mdx +31 -0
  117. package/docs/config/extensions/ffmpeg.mdx +45 -0
  118. package/docs/config/extensions/lightpanda.mdx +56 -0
  119. package/docs/config/extensions/overview.mdx +67 -0
  120. package/docs/config/extensions/playwright.mdx +195 -0
  121. package/docs/config/extensions/prismaExtension.mdx +1014 -0
  122. package/docs/config/extensions/puppeteer.mdx +30 -0
  123. package/docs/config/extensions/pythonExtension.mdx +182 -0
  124. package/docs/config/extensions/syncEnvVars.mdx +291 -0
  125. package/docs/context.mdx +235 -0
  126. package/docs/database-connections.mdx +213 -0
  127. package/docs/deploy-environment-variables.mdx +435 -0
  128. package/docs/deployment/atomic-deployment.mdx +172 -0
  129. package/docs/deployment/overview.mdx +257 -0
  130. package/docs/deployment/preview-branches.mdx +224 -0
  131. package/docs/errors-retrying.mdx +379 -0
  132. package/docs/github-actions.mdx +222 -0
  133. package/docs/github-integration.mdx +136 -0
  134. package/docs/github-repo.mdx +8 -0
  135. package/docs/help-email.mdx +6 -0
  136. package/docs/help-slack.mdx +11 -0
  137. package/docs/hidden-tasks.mdx +56 -0
  138. package/docs/how-it-works.mdx +454 -0
  139. package/docs/how-to-reduce-your-spend.mdx +217 -0
  140. package/docs/idempotency.mdx +504 -0
  141. package/docs/introduction.mdx +223 -0
  142. package/docs/limits.mdx +241 -0
  143. package/docs/logging.mdx +195 -0
  144. package/docs/machines.mdx +952 -0
  145. package/docs/manual-setup.mdx +632 -0
  146. package/docs/mcp-agent-rules.mdx +41 -0
  147. package/docs/mcp-introduction.mdx +385 -0
  148. package/docs/mcp-tools.mdx +273 -0
  149. package/docs/migrating-from-v3.mdx +334 -0
  150. package/docs/observability/dashboards.mdx +102 -0
  151. package/docs/observability/query.mdx +585 -0
  152. package/docs/open-source-contributing.mdx +16 -0
  153. package/docs/open-source-self-hosting.mdx +541 -0
  154. package/docs/private-networking/aws-console-setup.mdx +304 -0
  155. package/docs/private-networking/overview.mdx +144 -0
  156. package/docs/private-networking/troubleshooting.mdx +78 -0
  157. package/docs/queue-concurrency.mdx +354 -0
  158. package/docs/quick-start.mdx +97 -0
  159. package/docs/realtime/auth.mdx +208 -0
  160. package/docs/realtime/backend/overview.mdx +45 -0
  161. package/docs/realtime/backend/streams.mdx +418 -0
  162. package/docs/realtime/backend/subscribe.mdx +225 -0
  163. package/docs/realtime/how-it-works.mdx +94 -0
  164. package/docs/realtime/overview.mdx +63 -0
  165. package/docs/realtime/react-hooks/overview.mdx +73 -0
  166. package/docs/realtime/react-hooks/streams.mdx +449 -0
  167. package/docs/realtime/react-hooks/subscribe.mdx +674 -0
  168. package/docs/realtime/react-hooks/swr.mdx +87 -0
  169. package/docs/realtime/react-hooks/triggering.mdx +194 -0
  170. package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
  171. package/docs/realtime/run-object.mdx +174 -0
  172. package/docs/replaying.mdx +72 -0
  173. package/docs/request-feature.mdx +6 -0
  174. package/docs/roadmap.mdx +6 -0
  175. package/docs/run-tests.mdx +20 -0
  176. package/docs/run-usage.mdx +113 -0
  177. package/docs/runs/heartbeats.mdx +38 -0
  178. package/docs/runs/max-duration.mdx +139 -0
  179. package/docs/runs/metadata.mdx +734 -0
  180. package/docs/runs/priority.mdx +31 -0
  181. package/docs/runs.mdx +396 -0
  182. package/docs/self-hosting/docker.mdx +458 -0
  183. package/docs/self-hosting/env/supervisor.mdx +74 -0
  184. package/docs/self-hosting/env/webapp.mdx +276 -0
  185. package/docs/self-hosting/kubernetes.mdx +601 -0
  186. package/docs/self-hosting/overview.mdx +108 -0
  187. package/docs/skills.mdx +85 -0
  188. package/docs/tags.mdx +120 -0
  189. package/docs/tasks/overview.mdx +697 -0
  190. package/docs/tasks/scheduled.mdx +382 -0
  191. package/docs/tasks/schemaTask.mdx +413 -0
  192. package/docs/tasks/streams.mdx +884 -0
  193. package/docs/triggering.mdx +1320 -0
  194. package/docs/troubleshooting-alerts.mdx +385 -0
  195. package/docs/troubleshooting-debugging-in-vscode.mdx +8 -0
  196. package/docs/troubleshooting-github-issues.mdx +6 -0
  197. package/docs/troubleshooting-uptime-status.mdx +6 -0
  198. package/docs/troubleshooting.mdx +398 -0
  199. package/docs/upgrading-packages.mdx +80 -0
  200. package/docs/vercel-integration.mdx +207 -0
  201. package/docs/versioning.mdx +56 -0
  202. package/docs/video-walkthrough.mdx +23 -0
  203. package/docs/wait-for-token.mdx +540 -0
  204. package/docs/wait-for.mdx +42 -0
  205. package/docs/wait-until.mdx +53 -0
  206. package/docs/wait.mdx +18 -0
  207. package/docs/writing-tasks-introduction.mdx +33 -0
  208. package/package.json +10 -6
  209. package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
  210. package/skills/trigger-authoring-tasks/SKILL.md +254 -0
  211. package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
  212. package/skills/trigger-cost-savings/SKILL.md +116 -0
  213. package/skills/trigger-realtime-and-frontend/SKILL.md +276 -0
@@ -0,0 +1,504 @@
1
+ ---
2
+ title: "Idempotency"
3
+ description: "An API call or operation is idempotent if it has the same result when called more than once."
4
+ ---
5
+
6
+ We currently support idempotency at the task level, meaning that if you trigger a task with the same `idempotencyKey` twice, the second request will not create a new task run. Instead, the original run's handle is returned, allowing you to track the existing run's progress.
7
+
8
+ ## Why use idempotency keys?
9
+
10
+ The most common use case is **preventing duplicate child tasks when a parent task retries**. Without idempotency keys, each retry of the parent would trigger a new child task run:
11
+
12
+ ```mermaid
13
+ sequenceDiagram
14
+ participant Parent as Parent Task
15
+ participant SDK as Trigger.dev
16
+ participant Child as Child Task
17
+
18
+ Note over Parent: Attempt 1
19
+ Parent->>SDK: trigger(childTask, {idempotencyKey})
20
+ SDK->>Child: Creates child run
21
+ Child-->>SDK: Running...
22
+ Parent->>Parent: Fails, will retry
23
+
24
+ Note over Parent: Attempt 2 (retry)
25
+ Parent->>SDK: trigger(childTask, {idempotencyKey})
26
+ Note over SDK: Same key, returns existing run
27
+ SDK-->>Parent: Returns original child run
28
+
29
+ Note over Child: Child task only runs once
30
+ ```
31
+
32
+ Other common use cases include:
33
+
34
+ - **Preventing duplicate emails** - Ensure a confirmation email is only sent once, even if the parent task retries
35
+ - **Avoiding double-charging customers** - Prevent duplicate payment processing during retries
36
+ - **One-time setup tasks** - Ensure initialization or migration tasks only run once
37
+ - **Deduplicating webhook processing** - Handle the same webhook event only once, even if it's delivered multiple times
38
+
39
+ ## `idempotencyKey` option
40
+
41
+ You can provide an `idempotencyKey` when triggering a task:
42
+
43
+ ```ts
44
+ import { idempotencyKeys, task } from "@trigger.dev/sdk";
45
+
46
+ export const myTask = task({
47
+ id: "my-task",
48
+ retry: {
49
+ maxAttempts: 4,
50
+ },
51
+ run: async (payload: any) => {
52
+ // This idempotency key will be unique to this task run, meaning the childTask will only be triggered once across all retries
53
+ const idempotencyKey = await idempotencyKeys.create("my-task-key");
54
+
55
+ // childTask will only be triggered once with the same idempotency key
56
+ await childTask.trigger({ foo: "bar" }, { idempotencyKey });
57
+
58
+ // Do something else, that may throw an error and cause the task to be retried
59
+ throw new Error("Something went wrong");
60
+ },
61
+ });
62
+ ```
63
+
64
+ You can use the `idempotencyKeys.create` SDK function to create an idempotency key before passing it to the `options` object.
65
+
66
+ We automatically inject the run ID when generating the idempotency key when running inside a task by default. You can turn it off by passing the `scope` option to `idempotencyKeys.create`:
67
+
68
+ ```ts
69
+ import { idempotencyKeys, task } from "@trigger.dev/sdk";
70
+
71
+ export const myTask = task({
72
+ id: "my-task",
73
+ retry: {
74
+ maxAttempts: 4,
75
+ },
76
+ run: async (payload: any) => {
77
+ // This idempotency key will be globally unique, meaning only a single task run will be triggered with this key
78
+ const idempotencyKey = await idempotencyKeys.create("my-task-key", { scope: "global" });
79
+
80
+ // childTask will only be triggered once with the same idempotency key
81
+ await childTask.trigger({ foo: "bar" }, { idempotencyKey });
82
+ },
83
+ });
84
+ ```
85
+
86
+ If you are triggering a task from your backend code, you can use the `idempotencyKeys.create` SDK function to create an idempotency key.
87
+
88
+ ```ts
89
+ import { idempotencyKeys, tasks } from "@trigger.dev/sdk";
90
+
91
+ // You can also pass an array of strings to create a idempotency key
92
+ const idempotencyKey = await idempotencyKeys.create([myUser.id, "my-task"]);
93
+ await tasks.trigger("my-task", { some: "data" }, { idempotencyKey });
94
+ ```
95
+
96
+ You can also pass a string to the `idempotencyKey` option, without first creating it with `idempotencyKeys.create`.
97
+
98
+ ```ts
99
+ import { myTask } from "./trigger/myTasks";
100
+
101
+ // You can also pass an array of strings to create a idempotency key
102
+ await myTask.trigger({ some: "data" }, { idempotencyKey: myUser.id });
103
+ ```
104
+
105
+ <Note>
106
+ When you pass a raw string, it defaults to `"run"` scope (scoped to the parent run). See [Default behavior](#default-behavior) for details on how scopes work and how to use global scope instead.
107
+ </Note>
108
+
109
+ <Note>Make sure you provide sufficiently unique keys to avoid collisions.</Note>
110
+
111
+ <Note>
112
+ Idempotency keys are limited to 2048 characters. Keys produced by `idempotencyKeys.create()` are 64-character hashes and always fit; this limit only matters if you pass a long raw string. Requests above the limit return `400`.
113
+ </Note>
114
+
115
+ You can pass the `idempotencyKey` when calling `batchTrigger` as well:
116
+
117
+ ```ts
118
+ import { tasks } from "@trigger.dev/sdk";
119
+
120
+ await tasks.batchTrigger("my-task", [
121
+ {
122
+ payload: { some: "data" },
123
+ options: { idempotencyKey: await idempotencyKeys.create(myUser.id) },
124
+ },
125
+ ]);
126
+ ```
127
+
128
+ ## Understanding scopes
129
+
130
+ The `scope` option determines how your idempotency key is processed. When you provide a key, it gets hashed together with additional context based on the scope. This means the same key string can produce different idempotency behaviors depending on the scope you choose.
131
+
132
+ ### Available scopes
133
+
134
+ | Scope | What gets hashed | Description | Use case |
135
+ | --- | --- | --- | --- |
136
+ | `"run"` | `key + parentRunId` | Key is combined with the parent run ID | Prevent duplicates within a single parent run (default) |
137
+ | `"attempt"` | `key + parentRunId + attemptNumber` | Key is combined with the parent run ID and attempt number | Allow child tasks to re-run on each retry of the parent |
138
+ | `"global"` | `key` | Key is used as-is, no context added | Ensure a task only runs once ever, regardless of parent |
139
+
140
+ ### `run` scope (default)
141
+
142
+ The `run` scope makes the idempotency key unique to the current parent task run. This is the default behavior for both raw strings and `idempotencyKeys.create()`.
143
+
144
+ ```ts
145
+ import { idempotencyKeys, task } from "@trigger.dev/sdk";
146
+
147
+ export const processOrder = task({
148
+ id: "process-order",
149
+ retry: { maxAttempts: 3 },
150
+ run: async (payload: { orderId: string; email: string }) => {
151
+ // This key is scoped to this specific run of processOrder
152
+ // If processOrder retries, the same key still refers to the same child run
153
+ const idempotencyKey = await idempotencyKeys.create(`send-confirmation-${payload.orderId}`);
154
+
155
+ // sendEmail will only be triggered once, even if processOrder retries multiple times
156
+ await sendEmail.trigger(
157
+ { to: payload.email, subject: "Order confirmed" },
158
+ { idempotencyKey }
159
+ );
160
+
161
+ // ... more processing that might fail and cause a retry
162
+ },
163
+ });
164
+ ```
165
+
166
+ With `run` scope, if you trigger `processOrder` twice with different run IDs, both will send emails because the idempotency keys are different (they include different parent run IDs).
167
+
168
+ ### `attempt` scope
169
+
170
+ The `attempt` scope makes the idempotency key unique to each attempt of the parent task. Use this when you want child tasks to re-execute on each retry.
171
+
172
+ ```ts
173
+ import { idempotencyKeys, task } from "@trigger.dev/sdk";
174
+
175
+ export const syncData = task({
176
+ id: "sync-data",
177
+ retry: { maxAttempts: 3 },
178
+ run: async (payload: { userId: string }) => {
179
+ // This key changes on each retry attempt
180
+ const idempotencyKey = await idempotencyKeys.create(`fetch-${payload.userId}`, {
181
+ scope: "attempt",
182
+ });
183
+
184
+ // fetchLatestData will run again on each retry, getting fresh data
185
+ const result = await fetchLatestData.triggerAndWait(
186
+ { userId: payload.userId },
187
+ { idempotencyKey }
188
+ );
189
+
190
+ // Process the fresh data...
191
+ },
192
+ });
193
+ ```
194
+
195
+ ### `global` scope
196
+
197
+ The `global` scope makes the idempotency key truly global across all runs. Use this when you want to ensure a task only runs once ever (until the TTL expires), regardless of which parent task triggered it.
198
+
199
+ ```ts
200
+ import { idempotencyKeys, task } from "@trigger.dev/sdk";
201
+
202
+ export const onboardUser = task({
203
+ id: "onboard-user",
204
+ run: async (payload: { userId: string; email: string }) => {
205
+ // This key is global - the welcome email will only be sent once per user,
206
+ // even if onboardUser is triggered multiple times from different places
207
+ const idempotencyKey = await idempotencyKeys.create(`welcome-email-${payload.userId}`, {
208
+ scope: "global",
209
+ });
210
+
211
+ await sendWelcomeEmail.trigger(
212
+ { to: payload.email },
213
+ { idempotencyKey, idempotencyKeyTTL: "7d" }
214
+ );
215
+ },
216
+ });
217
+ ```
218
+
219
+ <Note>
220
+ Even with `global` scope, idempotency keys are still isolated to the specific task and environment. Using the same key to trigger *different* tasks will not deduplicate - both tasks will run. See [Environment and task scoping](#environment-and-task-scoping) for more details.
221
+ </Note>
222
+
223
+ ## Default behavior
224
+
225
+ Understanding the default behavior is important to avoid unexpected results:
226
+
227
+ ### Passing a raw string
228
+
229
+ When you pass a raw string directly to the `idempotencyKey` option, it is automatically processed with `run` scope:
230
+
231
+ ```ts
232
+ // These two are equivalent when called inside a task:
233
+ await childTask.trigger(payload, { idempotencyKey: "my-key" });
234
+ await childTask.trigger(payload, { idempotencyKey: await idempotencyKeys.create("my-key") });
235
+ ```
236
+
237
+ <Warning>
238
+ **Breaking change in v4.3.1:** In v4.3.0 and earlier, raw strings defaulted to `global` scope. Starting in v4.3.1, raw strings now default to `run` scope. If you're upgrading and relied on the previous global behavior, you must now explicitly use `idempotencyKeys.create("key", { scope: "global" })`.
239
+ </Warning>
240
+
241
+ This means raw strings are scoped to the parent run by default. If you want global behavior, you must explicitly create the key with `scope: "global"`:
242
+
243
+ ```ts
244
+ // For global idempotency, you must use idempotencyKeys.create with scope: "global"
245
+ const idempotencyKey = await idempotencyKeys.create("my-key", { scope: "global" });
246
+ await childTask.trigger(payload, { idempotencyKey });
247
+ ```
248
+
249
+ ### Triggering from backend code
250
+
251
+ When triggering tasks from your backend code (outside of a task), there is no parent run context. In this case, `run` and `attempt` scopes behave the same as `global` since there's no run ID or attempt number to inject:
252
+
253
+ ```ts
254
+ // In your backend code (e.g., an API route)
255
+ import { idempotencyKeys, tasks } from "@trigger.dev/sdk";
256
+
257
+ // All three of these behave the same when called outside a task:
258
+ await tasks.trigger("my-task", payload, { idempotencyKey: "my-key" });
259
+ await tasks.trigger("my-task", payload, {
260
+ idempotencyKey: await idempotencyKeys.create("my-key", { scope: "run" }),
261
+ });
262
+ await tasks.trigger("my-task", payload, {
263
+ idempotencyKey: await idempotencyKeys.create("my-key", { scope: "global" }),
264
+ });
265
+ ```
266
+
267
+ <Note>
268
+ When triggering from backend code, the scope doesn't matter since there's no task context. All scopes effectively behave as global.
269
+ </Note>
270
+
271
+ ## `idempotencyKeyTTL` option
272
+
273
+ The `idempotencyKeyTTL` option defines a time window during which a task with the same idempotency key will only run once. Here's how it works:
274
+
275
+ 1. When you trigger a task with an idempotency key and set `idempotencyKeyTTL: "5m"`, it creates a 5-minute window.
276
+ 2. During this window, any subsequent triggers with the same idempotency key will return the original task run instead of creating a new one.
277
+ 3. Once the TTL window expires, the next trigger with that idempotency key will create a new task run and start a new time window.
278
+
279
+ ![idempotency-key-ttl](/images/idempotency-key-ttl.png)
280
+
281
+ By default idempotency keys are stored for 30 days. You can change this by passing the `idempotencyKeyTTL` option when triggering a task:
282
+
283
+ ```ts
284
+ import { idempotencyKeys, task, wait } from "@trigger.dev/sdk";
285
+
286
+ export const myTask = task({
287
+ id: "my-task",
288
+ retry: {
289
+ maxAttempts: 4,
290
+ },
291
+ run: async (payload: any) => {
292
+ const idempotencyKey = await idempotencyKeys.create("my-task-key");
293
+
294
+ // The idempotency key will expire after 60 seconds
295
+ await childTask.trigger({ foo: "bar" }, { idempotencyKey, idempotencyKeyTTL: "60s" });
296
+
297
+ await wait.for({ seconds: 61 });
298
+
299
+ // The idempotency key will have expired, so the childTask will be triggered again
300
+ await childTask.trigger({ foo: "bar" }, { idempotencyKey });
301
+
302
+ // Do something else, that may throw an error and cause the task to be retried
303
+ throw new Error("Something went wrong");
304
+ },
305
+ });
306
+ ```
307
+
308
+ You can use the following units for the `idempotencyKeyTTL` option:
309
+
310
+ - `s` for seconds (e.g. `60s`)
311
+ - `m` for minutes (e.g. `5m`)
312
+ - `h` for hours (e.g. `2h`)
313
+ - `d` for days (e.g. `3d`)
314
+
315
+ ## Failed runs and idempotency
316
+
317
+ When a run with an idempotency key **fails**, the key is automatically cleared. This means triggering the same task with the same idempotency key will create a new run. However, **successful** and **canceled** runs keep their idempotency key. If you need to re-trigger after a successful or canceled run, you can:
318
+
319
+ 1. **Reset the idempotency key** using `idempotencyKeys.reset()`:
320
+
321
+ ```ts
322
+ import { idempotencyKeys } from "@trigger.dev/sdk";
323
+
324
+ // Clear the idempotency key to allow re-triggering
325
+ await idempotencyKeys.reset("my-task", "my-idempotency-key");
326
+
327
+ // Now you can trigger the task again
328
+ await myTask.trigger(payload, { idempotencyKey: "my-idempotency-key" });
329
+ ```
330
+
331
+ 2. **Use a shorter TTL** so the key expires automatically:
332
+
333
+ ```ts
334
+ // Key expires after 5 minutes
335
+ await myTask.trigger(payload, {
336
+ idempotencyKey: "my-key",
337
+ idempotencyKeyTTL: "5m"
338
+ });
339
+ ```
340
+
341
+ ## Payload-based idempotency
342
+
343
+ We don't currently support payload-based idempotency, but you can implement it yourself by hashing the payload and using the hash as the idempotency key.
344
+
345
+ ```ts
346
+ import { idempotencyKeys, task } from "@trigger.dev/sdk";
347
+ import { createHash } from "node:crypto";
348
+
349
+ // Somewhere in your code
350
+ const idempotencyKey = await idempotencyKeys.create(hash(childPayload));
351
+ // childTask will only be triggered once with the same idempotency key
352
+ await tasks.trigger("child-task", { some: "payload" }, { idempotencyKey });
353
+
354
+ // Create a hash of the payload using Node.js crypto
355
+ // Ideally, you'd do a stable serialization of the payload before hashing, to ensure the same payload always results in the same hash
356
+ function hash(payload: any): string {
357
+ const hash = createHash("sha256");
358
+ hash.update(JSON.stringify(payload));
359
+ return hash.digest("hex");
360
+ }
361
+ ```
362
+
363
+ ## Resetting idempotency keys
364
+
365
+ You can reset an idempotency key to clear it from all associated runs. This is useful if you need to allow a task to be triggered again with the same idempotency key.
366
+
367
+ When you reset an idempotency key, it will be cleared for all runs that match both the task identifier and the idempotency key in the current environment. This allows you to trigger the task again with the same key.
368
+
369
+ ### API signature
370
+
371
+ ```ts
372
+ idempotencyKeys.reset(
373
+ taskIdentifier: string,
374
+ idempotencyKey: string,
375
+ requestOptions?: ZodFetchOptions
376
+ ): Promise<{ id: string }>
377
+ ```
378
+
379
+ | Parameter | Description |
380
+ | --- | --- |
381
+ | `taskIdentifier` | The identifier of the task (e.g., `"my-task"`) |
382
+ | `idempotencyKey` | The idempotency key hash to reset (the 64-character hash string) |
383
+ | `requestOptions` | Optional fetch options for the API request |
384
+
385
+ ### Resetting keys created with `idempotencyKeys.create()`
386
+
387
+ When you pass an `IdempotencyKey` created with `idempotencyKeys.create()`, the scope and original key are automatically extracted, making it easy to reset:
388
+
389
+ ```ts
390
+ import { idempotencyKeys, task } from "@trigger.dev/sdk";
391
+
392
+ export const parentTask = task({
393
+ id: "parent-task",
394
+ run: async (payload) => {
395
+ const key = await idempotencyKeys.create("my-key", { scope: "global" });
396
+ await childTask.trigger(payload, { idempotencyKey: key });
397
+
398
+ // Later in the same task, reset it - options extracted automatically
399
+ await idempotencyKeys.reset("child-task", key);
400
+ },
401
+ });
402
+ ```
403
+
404
+ ### Resetting global keys
405
+
406
+ Global keys are the simplest to reset since they don't require any run context:
407
+
408
+ ```ts
409
+ import { idempotencyKeys } from "@trigger.dev/sdk";
410
+
411
+ // From anywhere - inside a task or from your backend code
412
+ await idempotencyKeys.reset("my-task", "my-key", { scope: "global" });
413
+ ```
414
+
415
+ ### Resetting run-scoped keys
416
+
417
+ Keys created with `"run"` scope (the default) include the parent run ID in the hash. When resetting from inside the same task, the run ID is automatically available:
418
+
419
+ ```ts
420
+ import { idempotencyKeys, task } from "@trigger.dev/sdk";
421
+
422
+ export const parentTask = task({
423
+ id: "parent-task",
424
+ run: async (payload) => {
425
+ // Create a run-scoped key (default)
426
+ const key = await idempotencyKeys.create("my-key");
427
+ await childTask.trigger(payload, { idempotencyKey: key });
428
+
429
+ // Reset works automatically inside the task - run ID is available from context
430
+ await idempotencyKeys.reset("child-task", key);
431
+ },
432
+ });
433
+ ```
434
+
435
+ When resetting from outside a task, you must provide the `parentRunId` if the key was created within a task context:
436
+
437
+ ```ts
438
+ import { idempotencyKeys } from "@trigger.dev/sdk";
439
+
440
+ // If the key was created within a task, you need the parent run ID
441
+ await idempotencyKeys.reset("my-task", "my-key", {
442
+ scope: "run",
443
+ parentRunId: "run_abc123"
444
+ });
445
+ ```
446
+
447
+ <Note>
448
+ If you triggered the task from backend code, all scopes behave as global (see [Triggering from backend code](#triggering-from-backend-code)). Use `scope: "global"` when resetting.
449
+ </Note>
450
+
451
+ ### Resetting attempt-scoped keys
452
+
453
+ Keys created with `"attempt"` scope include both the parent run ID and attempt number. When resetting from outside a task, you must provide both:
454
+
455
+ ```ts
456
+ import { idempotencyKeys } from "@trigger.dev/sdk";
457
+
458
+ // From your backend code
459
+ await idempotencyKeys.reset("my-task", "my-key", {
460
+ scope: "attempt",
461
+ parentRunId: "run_abc123",
462
+ attemptNumber: 1
463
+ });
464
+ ```
465
+
466
+ <Warning>
467
+ If you try to reset a `"run"` or `"attempt"` scoped key from outside a task without providing the required `parentRunId` (and `attemptNumber` for attempt scope), it will throw an error.
468
+ </Warning>
469
+
470
+ ### Resetting from the dashboard
471
+
472
+ You can also reset idempotency keys directly from the Trigger.dev dashboard:
473
+
474
+ 1. Navigate to the run that has the idempotency key you want to reset
475
+ 2. In the run details panel, find the "Idempotency key" section
476
+ 3. Click the "Reset" button
477
+
478
+ This is useful when you need to manually allow a task to be re-triggered without writing code.
479
+
480
+ ![Idempotency section in the run details pane showing the key, scope, and expiration time](/images/idempotency-key-dashboard.png)
481
+
482
+ <Note>
483
+ Resetting an idempotency key only affects runs in the current environment. The reset is scoped to the specific task identifier and idempotency key combination.
484
+ </Note>
485
+
486
+ ## Important notes
487
+
488
+ ### Environment and task scoping
489
+
490
+ Idempotency keys, even the ones scoped globally, are actually scoped to the task and the environment. This means that you cannot collide with keys from other environments (e.g. dev will never collide with prod), or to other projects and orgs.
491
+
492
+ If you use the same idempotency key for triggering different tasks, the tasks will not be idempotent, and both tasks will be triggered. There's currently no way to make multiple tasks idempotent with the same key.
493
+
494
+ ### How scopes affect the key
495
+
496
+ The scope determines what gets hashed alongside your key:
497
+
498
+ - Same key + `"run"` scope in different parent runs = different hashes = both tasks run
499
+ - Same key + `"global"` scope in different parent runs = same hash = only first task runs
500
+ - Same key + different scopes = different hashes = both tasks run
501
+
502
+ This is why understanding scopes is crucial: the same string key can produce different idempotency behavior depending on the scope and context.
503
+
504
+