@upstash/workflow 1.4.0 → 1.5.0
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.
- package/README.md +9 -0
- package/astro.d.mts +19 -2
- package/astro.d.ts +19 -2
- package/astro.js +670 -159
- package/astro.mjs +1 -1
- package/{chunk-DRHEHWMS.mjs → chunk-J3PXPHSB.mjs} +828 -317
- package/cloudflare.d.mts +17 -2
- package/cloudflare.d.ts +17 -2
- package/cloudflare.js +670 -159
- package/cloudflare.mjs +1 -1
- package/docs/agents/examples.mdx +8 -0
- package/docs/agents/features.mdx +374 -0
- package/docs/agents/getting-started.mdx +139 -0
- package/docs/agents/overview.mdx +40 -0
- package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
- package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
- package/docs/agents/patterns/parallelization.mdx +77 -0
- package/docs/agents/patterns/prompt-chaining.mdx +84 -0
- package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
- package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
- package/docs/basics/best-practices/deterministic-code.mdx +132 -0
- package/docs/basics/best-practices/idempotency.mdx +38 -0
- package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
- package/docs/basics/best-practices/overview.mdx +35 -0
- package/docs/basics/best-practices/promise-any.mdx +9 -0
- package/docs/basics/best-practices/return-step-results.mdx +80 -0
- package/docs/basics/best-practices/workflow-limits.mdx +62 -0
- package/docs/basics/client/cancel.mdx +137 -0
- package/docs/basics/client/dlq/callback.mdx +21 -0
- package/docs/basics/client/dlq/delete.mdx +120 -0
- package/docs/basics/client/dlq/list.mdx +96 -0
- package/docs/basics/client/dlq/restart.mdx +164 -0
- package/docs/basics/client/dlq/resume.mdx +167 -0
- package/docs/basics/client/logs.mdx +139 -0
- package/docs/basics/client/notify.mdx +79 -0
- package/docs/basics/client/trigger.mdx +139 -0
- package/docs/basics/client/waiters.mdx +31 -0
- package/docs/basics/client.mdx +40 -0
- package/docs/basics/how.mdx +65 -0
- package/docs/basics/serve/advanced.mdx +435 -0
- package/docs/basics/serve.mdx +142 -0
- package/docs/changelog.mdx +174 -0
- package/docs/examples/allInOne.mdx +377 -0
- package/docs/examples/authWebhook.mdx +567 -0
- package/docs/examples/customRetry.mdx +309 -0
- package/docs/examples/customerOnboarding.mdx +230 -0
- package/docs/examples/dynamicWorkflow.mdx +203 -0
- package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
- package/docs/examples/imageProcessing.mdx +339 -0
- package/docs/examples/paymentRetry.mdx +438 -0
- package/docs/examples/waitForEvent.mdx +201 -0
- package/docs/features/dlq/callback.mdx +42 -0
- package/docs/features/dlq/delete.mdx +29 -0
- package/docs/features/dlq/restart.mdx +33 -0
- package/docs/features/dlq/resume.mdx +37 -0
- package/docs/features/dlq.mdx +39 -0
- package/docs/features/failure-callback.mdx +102 -0
- package/docs/features/failureFunction/advanced.mdx +36 -0
- package/docs/features/failureFunction/reliability.mdx +33 -0
- package/docs/features/flow-control/monitor.mdx +22 -0
- package/docs/features/flow-control/parallelism.mdx +45 -0
- package/docs/features/flow-control/rate-period.mdx +46 -0
- package/docs/features/flow-control.mdx +164 -0
- package/docs/features/invoke/serveMany.mdx +109 -0
- package/docs/features/invoke.mdx +39 -0
- package/docs/features/notify.mdx +127 -0
- package/docs/features/parallel-steps.mdx +41 -0
- package/docs/features/retries/prevent-retries.mdx +140 -0
- package/docs/features/retries.mdx +93 -0
- package/docs/features/wait-for-event.mdx +104 -0
- package/docs/features/wait.mdx +44 -0
- package/docs/features/webhooks.mdx +116 -0
- package/docs/getstarted.mdx +200 -0
- package/docs/howto/cancel.mdx +50 -0
- package/docs/howto/changes.mdx +33 -0
- package/docs/howto/configure.mdx +42 -0
- package/docs/howto/failures.mdx +185 -0
- package/docs/howto/get-token.mdx +18 -0
- package/docs/howto/local-development/development-server.mdx +130 -0
- package/docs/howto/local-development/local-tunnel.mdx +73 -0
- package/docs/howto/middlewares.mdx +263 -0
- package/docs/howto/migrations.mdx +335 -0
- package/docs/howto/multi-region.mdx +107 -0
- package/docs/howto/parallel-runs.mdx +52 -0
- package/docs/howto/readonly-token.mdx +19 -0
- package/docs/howto/realtime/basic.mdx +315 -0
- package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
- package/docs/howto/redact-fields.mdx +61 -0
- package/docs/howto/retry-now.mdx +41 -0
- package/docs/howto/schedule.mdx +104 -0
- package/docs/howto/security.mdx +143 -0
- package/docs/howto/singleton.mdx +137 -0
- package/docs/howto/start.mdx +69 -0
- package/docs/howto/use-webhooks.mdx +256 -0
- package/docs/integrations/aisdk.mdx +316 -0
- package/docs/integrations/anthropic.mdx +51 -0
- package/docs/integrations/datadog.mdx +69 -0
- package/docs/integrations/openai.mdx +87 -0
- package/docs/integrations/prometheus.mdx +65 -0
- package/docs/integrations/resend.mdx +82 -0
- package/docs/llms-txt.mdx +4 -0
- package/docs/openapi.yaml +2944 -0
- package/docs/pricing.mdx +22 -0
- package/docs/quickstarts/astro.mdx +185 -0
- package/docs/quickstarts/cloudflare-workers.mdx +190 -0
- package/docs/quickstarts/express.mdx +190 -0
- package/docs/quickstarts/fastapi.mdx +282 -0
- package/docs/quickstarts/flask.mdx +282 -0
- package/docs/quickstarts/hono.mdx +221 -0
- package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
- package/docs/quickstarts/nextjs-flask.mdx +147 -0
- package/docs/quickstarts/nuxt.mdx +169 -0
- package/docs/quickstarts/platforms.mdx +78 -0
- package/docs/quickstarts/solidjs.mdx +161 -0
- package/docs/quickstarts/svelte.mdx +170 -0
- package/docs/quickstarts/tanstack-start.mdx +205 -0
- package/docs/quickstarts/vercel-nextjs.mdx +233 -0
- package/docs/roadmap.mdx +8 -0
- package/docs/sdk/workflow-js.mdx +4 -0
- package/docs/sdk/workflow-py.mdx +4 -0
- package/docs/steps/api.mdx +64 -0
- package/docs/steps/call.mdx +183 -0
- package/docs/steps/cancel.mdx +25 -0
- package/docs/steps/createWebhook.mdx +40 -0
- package/docs/steps/invoke.mdx +100 -0
- package/docs/steps/notify.mdx +105 -0
- package/docs/steps/run.mdx +150 -0
- package/docs/steps/sleep.mdx +97 -0
- package/docs/steps/sleepUntil.mdx +88 -0
- package/docs/steps/waitForEvent.mdx +60 -0
- package/docs/steps/waitForWebhook.mdx +78 -0
- package/docs/steps.mdx +108 -0
- package/docs/troubleshooting/general.mdx +331 -0
- package/docs/troubleshooting/vercel.mdx +82 -0
- package/express.d.mts +19 -2
- package/express.d.ts +19 -2
- package/express.js +670 -159
- package/express.mjs +1 -1
- package/h3.d.mts +19 -2
- package/h3.d.ts +19 -2
- package/h3.js +670 -159
- package/h3.mjs +1 -1
- package/hono.d.mts +17 -2
- package/hono.d.ts +17 -2
- package/hono.js +670 -159
- package/hono.mjs +1 -1
- package/index.d.mts +50 -21
- package/index.d.ts +50 -21
- package/index.js +686 -171
- package/index.mjs +17 -13
- package/nextjs.d.mts +40 -2
- package/nextjs.d.ts +40 -2
- package/nextjs.js +670 -159
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +16 -2
- package/react-router.d.ts +16 -2
- package/react-router.js +670 -159
- package/react-router.mjs +1 -1
- package/{serve-many-CG3BFvO3.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-iJF1IUXk.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +10 -1
- package/solidjs.d.ts +10 -1
- package/solidjs.js +670 -159
- package/solidjs.mjs +1 -1
- package/src/platforms/astro.ts +84 -0
- package/src/platforms/cloudflare.ts +127 -0
- package/src/platforms/express.ts +147 -0
- package/src/platforms/h3.ts +105 -0
- package/src/platforms/hono.ts +103 -0
- package/src/platforms/nextjs.ts +193 -0
- package/src/platforms/react-router.ts +92 -0
- package/src/platforms/solidjs.ts +59 -0
- package/src/platforms/svelte.ts +99 -0
- package/src/platforms/tanstack.ts +96 -0
- package/src/src/client/dlq.ts +387 -0
- package/src/src/client/filter-types.ts +166 -0
- package/src/src/client/index.ts +350 -0
- package/src/src/client/types.ts +501 -0
- package/src/src/client/utils.ts +307 -0
- package/src/src/constants.ts +82 -0
- package/src/src/context/api/anthropic.ts +58 -0
- package/src/src/context/api/base.ts +52 -0
- package/src/src/context/api/index.ts +24 -0
- package/src/src/context/api/openai.ts +125 -0
- package/src/src/context/api/resend.ts +57 -0
- package/src/src/context/auto-executor.ts +707 -0
- package/src/src/context/context.ts +681 -0
- package/src/src/context/index.ts +1 -0
- package/src/src/context/provider.ts +37 -0
- package/src/src/context/steps.ts +965 -0
- package/src/src/error.ts +203 -0
- package/src/src/index.ts +13 -0
- package/src/src/middleware/default-callbacks.ts +20 -0
- package/src/src/middleware/index.ts +2 -0
- package/src/src/middleware/logging.ts +39 -0
- package/src/src/middleware/manager.ts +148 -0
- package/src/src/middleware/middleware.ts +46 -0
- package/src/src/middleware/types.ts +72 -0
- package/src/src/qstash/headers.ts +397 -0
- package/src/src/qstash/step-config.ts +268 -0
- package/src/src/qstash/submit-steps.ts +284 -0
- package/src/src/serve/authorization.ts +118 -0
- package/src/src/serve/index.ts +475 -0
- package/src/src/serve/multi-region/handlers.ts +225 -0
- package/src/src/serve/multi-region/utils.ts +88 -0
- package/src/src/serve/options.ts +241 -0
- package/src/src/serve/serve-many.ts +117 -0
- package/src/src/serve/utils.ts +11 -0
- package/src/src/types.ts +590 -0
- package/src/src/utils.ts +141 -0
- package/src/src/workflow-parser.ts +472 -0
- package/src/src/workflow-requests.ts +575 -0
- package/svelte.d.mts +17 -2
- package/svelte.d.ts +17 -2
- package/svelte.js +670 -159
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +15 -2
- package/tanstack.d.ts +15 -2
- package/tanstack.js +670 -159
- package/tanstack.mjs +1 -1
- package/{types-CekOpKvz.d.ts → types-CrgK5P6p.d.mts} +372 -32
- package/{types-CekOpKvz.d.mts → types-CrgK5P6p.d.ts} +372 -32
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Upstash Workflow with Next.js
|
|
3
|
+
sidebarTitle: Next.js
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This guide provides step-by-step instructions on how to use and deploy Upstash Workflow with Next.js.
|
|
7
|
+
|
|
8
|
+
<Columns cols={2}>
|
|
9
|
+
<Card title="GitHub Repository" icon="github" href="https://github.com/upstash/workflow-js/tree/main/examples/nextjs" horizontal>
|
|
10
|
+
You can find the project source code on GitHub.
|
|
11
|
+
</Card>
|
|
12
|
+
<Card title="Deploy With Vercel" icon="triangle" iconType="sharp-solid" href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fupstash%2Fworkflow-js%2Ftree%2Fmain%2Fworkflow%2Fnextjs&env=QSTASH_TOKEN&envDescription=You%20can%20access%20this%20variable%20from%20Upstash%20Console%2C%20under%20QStash%20page.%20&project-name=workflow-nextjs&repository-name=workflow-nextjs&demo-title=Upstash%20Workflow%20Example&demo-description=A%20Next.js%20application%20utilizing%20Upstash%20Workflow" horizontal>
|
|
13
|
+
Deploy the project to Vercel with a single click.
|
|
14
|
+
</Card>
|
|
15
|
+
</Columns>
|
|
16
|
+
|
|
17
|
+
## Prerequisites
|
|
18
|
+
|
|
19
|
+
- Node.js and npm (or another package manager) installed.
|
|
20
|
+
|
|
21
|
+
You can integrate Upstash Workflow into an existing Next.js app, or follow [this guide](https://nextjs.org/docs/app/getting-started/installation) to create a new Next.js project from scratch.
|
|
22
|
+
|
|
23
|
+
## Step 1: Installation
|
|
24
|
+
|
|
25
|
+
Run the following command to install the Upstash Workflow SDK in your Next.js app.
|
|
26
|
+
|
|
27
|
+
<Tabs>
|
|
28
|
+
<Tab title="npm">
|
|
29
|
+
```bash
|
|
30
|
+
npm install @upstash/workflow
|
|
31
|
+
```
|
|
32
|
+
</Tab>
|
|
33
|
+
<Tab title="pnpm">
|
|
34
|
+
```bash
|
|
35
|
+
pnpm install @upstash/workflow
|
|
36
|
+
```
|
|
37
|
+
</Tab>
|
|
38
|
+
<Tab title="bun">
|
|
39
|
+
```bash
|
|
40
|
+
bun add @upstash/workflow
|
|
41
|
+
```
|
|
42
|
+
</Tab>
|
|
43
|
+
</Tabs>
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Step 2: Set up the development server
|
|
47
|
+
|
|
48
|
+
Upstash Workflow is built on top of Upstash QStash.
|
|
49
|
+
|
|
50
|
+
In a production environment, your application connects to the managed QStash servers hosted by Upstash.
|
|
51
|
+
This ensures that requests are delivered reliably, securely, and at scale without requiring you to run and maintain your own infrastructure.
|
|
52
|
+
|
|
53
|
+
For local development, you don't need to depend on the managed QStash servers. Since you are using `@upstash/workflow`, you can just set `QSTASH_DEV=true`, and the SDK will download, start, and connect to a local QStash development server automatically — no tokens or signing keys to copy over.
|
|
54
|
+
|
|
55
|
+
In the root of your project, create a `.env.local` file (or update your existing one):
|
|
56
|
+
|
|
57
|
+
```txt .env.local
|
|
58
|
+
QSTASH_DEV=true
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Both the workflow client and the `serve()` endpoint pick up the dev server automatically, including signature verification, so there's nothing else to configure. The local server behaves just like the production version but does not require external network calls.
|
|
62
|
+
|
|
63
|
+
You can enable local mode in the Upstash Workflow dashboard to use the UI while developing locally.
|
|
64
|
+
|
|
65
|
+
<Frame>
|
|
66
|
+
<img src="/img/qstash-workflow/local-dev.png" alt="Enable local mode on dashboard" />
|
|
67
|
+
</Frame>
|
|
68
|
+
|
|
69
|
+
<Tip>
|
|
70
|
+
Prefer to start the server yourself, or using a runtime other than Node.js? See the [Development Server guide](/workflow/howto/local-development/development-server) for the manual QStash CLI setup, custom ports, and the `registerQStashDev()` helper for Next.js edge routes.
|
|
71
|
+
</Tip>
|
|
72
|
+
|
|
73
|
+
<Note>
|
|
74
|
+
For production, set your actual credentials from the Upstash Workflow dashboard instead of `QSTASH_DEV`.
|
|
75
|
+
</Note>
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
## Step 3: Create a Workflow Endpoint
|
|
79
|
+
|
|
80
|
+
With your environment ready, the next step is to define your first workflow endpoint.
|
|
81
|
+
|
|
82
|
+
In Upstash Workflow, every workflow is exposed as an endpoint.
|
|
83
|
+
Every endpoint you expose using the SDK’s `serve()` function acts as a workflow that can be triggered independently.
|
|
84
|
+
|
|
85
|
+
In Next.js, these endpoints are implemented as **API routes**.
|
|
86
|
+
|
|
87
|
+
Create the file according to your router setup:
|
|
88
|
+
|
|
89
|
+
- App Router → put the endpoint under `app/api`
|
|
90
|
+
- Pages Router → put the endpoint under `pages/api`
|
|
91
|
+
|
|
92
|
+
<Tabs>
|
|
93
|
+
<Tab title="App router">
|
|
94
|
+
```typescript app/api/workflow/route.ts
|
|
95
|
+
import { serve } from "@upstash/workflow/nextjs"
|
|
96
|
+
|
|
97
|
+
export const { POST } = serve(
|
|
98
|
+
async (context) => {
|
|
99
|
+
await context.run("initial-step", () => {
|
|
100
|
+
console.log("initial step ran")
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
await context.run("second-step", () => {
|
|
104
|
+
console.log("second step ran")
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
)
|
|
108
|
+
```
|
|
109
|
+
</Tab>
|
|
110
|
+
|
|
111
|
+
<Tab title="App router with request object">
|
|
112
|
+
```typescript app/api/workflow/route.ts
|
|
113
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
114
|
+
import { NextRequest } from "next/server";
|
|
115
|
+
|
|
116
|
+
export const POST = async (request: NextRequest) => {
|
|
117
|
+
// do something with the native request object
|
|
118
|
+
const { POST: handler } = serve(async (context) => {
|
|
119
|
+
// Your workflow steps
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return await handler(request);
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
</Tab>
|
|
126
|
+
<Tab title="Pages Router">
|
|
127
|
+
```typescript src/pages/api/workflow.ts
|
|
128
|
+
import { servePagesRouter } from "@upstash/workflow/nextjs";
|
|
129
|
+
|
|
130
|
+
const { handler } = servePagesRouter<string>(
|
|
131
|
+
async (context) => {
|
|
132
|
+
await context.run("initial-step", () => {
|
|
133
|
+
console.log("initial step ran")
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
await context.run("second-step", () => {
|
|
137
|
+
console.log("second step ran")
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
)
|
|
141
|
+
export default handler;
|
|
142
|
+
```
|
|
143
|
+
</Tab>
|
|
144
|
+
|
|
145
|
+
<Tab title="Pages Router with request object">
|
|
146
|
+
```typescript src/pages/api/workflow.ts
|
|
147
|
+
import type { NextApiRequest, NextApiResponse } from "next";
|
|
148
|
+
import { servePagesRouter } from "@upstash/workflow/nextjs";
|
|
149
|
+
|
|
150
|
+
export default async function handler(
|
|
151
|
+
req: NextApiRequest,
|
|
152
|
+
res: NextApiResponse
|
|
153
|
+
) {
|
|
154
|
+
// do something with the native request object
|
|
155
|
+
const { handler } = servePagesRouter(
|
|
156
|
+
async (context) => {
|
|
157
|
+
// Your workflow steps
|
|
158
|
+
}
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
await handler(req, res)
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
</Tab>
|
|
165
|
+
</Tabs>
|
|
166
|
+
|
|
167
|
+
## Step 4: Run the Workflow Endpoint
|
|
168
|
+
|
|
169
|
+
Once your endpoint is defined, the next step is to trigger a workflow run.
|
|
170
|
+
|
|
171
|
+
You can start a new workflow run using the `trigger()` function from the Upstash Workflow SDK.
|
|
172
|
+
|
|
173
|
+
```javascript
|
|
174
|
+
import { Client } from "@upstash/workflow";
|
|
175
|
+
|
|
176
|
+
const client = new Client({ token: process.env.QSTASH_TOKEN! })
|
|
177
|
+
|
|
178
|
+
const { workflowRunId } = await client.trigger({
|
|
179
|
+
url: `http://localhost:3000/api/workflow`,
|
|
180
|
+
retries: 3
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
<Info>
|
|
185
|
+
The `trigger()` function should typically be called from a server-side action (not directly in client-side code) to keep your credentials secure.
|
|
186
|
+
</Info>
|
|
187
|
+
|
|
188
|
+
Check the Upstash Workflow dashboard to view logs of your workflow run:
|
|
189
|
+
|
|
190
|
+
<Frame>
|
|
191
|
+
<img src="/img/qstash-workflow/run-view.png" alt="Debug a workflow run on UI" />
|
|
192
|
+
</Frame>
|
|
193
|
+
|
|
194
|
+
<Tip>
|
|
195
|
+
Inside the `trigger()` call, you need to provide the URL of your workflow endpoint:
|
|
196
|
+
|
|
197
|
+
- Local development → use the URL where your app is running, for example: http://localhost:3000/api/PATH
|
|
198
|
+
- Production → use the URL of your deployed app, for example: https://yourapp.com/api/PATH
|
|
199
|
+
|
|
200
|
+
To avoid hardcoding URLs, you can define a `BASE_URL` constant and set it based on the environment.
|
|
201
|
+
A common pattern is to check an environment variable that only exists in production:
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
const BASE_URL = process.env.VERCEL_URL
|
|
205
|
+
? `https://${process.env.VERCEL_URL}`
|
|
206
|
+
: `http://localhost:3000`
|
|
207
|
+
|
|
208
|
+
const { workflowRunId } = await client.trigger({
|
|
209
|
+
url: `${BASE_URL}/api/workflow`,
|
|
210
|
+
retries: 3
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
</Tip>
|
|
214
|
+
|
|
215
|
+
## Step 5: Deploying to Production
|
|
216
|
+
|
|
217
|
+
You now have everything you need to deploy your application to production! 🎉
|
|
218
|
+
|
|
219
|
+
Before deploying, make sure your configuration no longer relies on local development settings:
|
|
220
|
+
|
|
221
|
+
- **Workflow URL**: Update the `trigger()` call to use your production domain (see the tip above for using a dynamic `BASE_URL`).
|
|
222
|
+
- **Credentials**: Remove `QSTASH_DEV=true` and set your production `QSTASH_TOKEN` and signing keys from the Upstash Console (see [Get your QStash Token](/workflow/howto/get-token)).
|
|
223
|
+
- **Environment Variables**: Verify that all required variables are set correctly in your deployment environment.
|
|
224
|
+
|
|
225
|
+
## Next Steps
|
|
226
|
+
|
|
227
|
+
Now that you've created and deployed your first workflow, here are some recommended guides to continue learning:
|
|
228
|
+
|
|
229
|
+
1. **[Learn the Workflow API](/workflow/basics/context)**: Dive deeper into the full API surface and advanced capabilities.
|
|
230
|
+
|
|
231
|
+
2. **[Configure Workflow Runs](/workflow/basics/client)**: Learn how to configure workflow execution to fit your app's needs.
|
|
232
|
+
|
|
233
|
+
3. **[Handle Failures](/workflow/howto/failures)**: Understand how to detect and recover from failed workflow runs.
|
package/docs/roadmap.mdx
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Roadmap"
|
|
3
|
+
url: https://github.com/orgs/upstash/discussions
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
<Note>
|
|
7
|
+
We have moved the roadmap and the changelog to [Github Discussions](https://github.com/orgs/upstash/discussions) starting from October 2025.Now you can follow `In Progress` features. You can see that your `Feature Requests` are recorded. You can vote for them and comment your specific use-cases to shape the feature to your needs.
|
|
8
|
+
</Note>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "context.api"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
In addition to `context.call`, you can also make third‑party requests using the `context.api` namespace.
|
|
6
|
+
|
|
7
|
+
This namespace provides built‑in integrations for **OpenAI**, **Anthropic**, and **Resend**, allowing you to make requests in a **type‑safe** manner.
|
|
8
|
+
|
|
9
|
+
<CodeGroup>
|
|
10
|
+
|
|
11
|
+
```typescript OpenAI
|
|
12
|
+
const { status, body } = await context.api.openai.call("Call OpenAI", {
|
|
13
|
+
token: "<OPENAI_API_KEY>",
|
|
14
|
+
operation: "chat.completions.create",
|
|
15
|
+
body: {
|
|
16
|
+
model: "gpt-4o",
|
|
17
|
+
messages: [
|
|
18
|
+
{
|
|
19
|
+
role: "system",
|
|
20
|
+
content: "Assistant says 'hello!'",
|
|
21
|
+
},
|
|
22
|
+
{ role: "user", content: "User shouts back 'hi!'" },
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```typescript Anthropic
|
|
29
|
+
const { status, body } = await context.api.anthropic.call(
|
|
30
|
+
"Call Anthropic",
|
|
31
|
+
{
|
|
32
|
+
token: "<ANTHROPIC_API_KEY>",
|
|
33
|
+
operation: "messages.create",
|
|
34
|
+
body: {
|
|
35
|
+
model: "claude-3-5-sonnet-20241022",
|
|
36
|
+
max_tokens: 1024,
|
|
37
|
+
messages: [
|
|
38
|
+
{"role": "user", "content": "Hello, world"}
|
|
39
|
+
]
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```typescript Resend
|
|
46
|
+
const { status, body } = await context.api.resend.call("Call Resend", {
|
|
47
|
+
token: "<RESEND_API_KEY>",
|
|
48
|
+
body: {
|
|
49
|
+
from: "Acme <onboarding@resend.dev>",
|
|
50
|
+
to: ["delivered@resend.dev"],
|
|
51
|
+
subject: "Hello World",
|
|
52
|
+
html: "<p>It works!</p>",
|
|
53
|
+
},
|
|
54
|
+
headers: {
|
|
55
|
+
"content-type": "application/json",
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
</CodeGroup>
|
|
61
|
+
|
|
62
|
+
We'll continue adding more integrations over time. If you'd like to see a specific integration, feel free to contribute to the SDK or contact us with your suggestion.
|
|
63
|
+
|
|
64
|
+
For detailed guides on usage and configuration, see the [Integrations section](/workflow/integrations/openai).
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "context.call"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
`context.call()` performs an HTTP request as a workflow step, supporting longer response times up to 12 hours.
|
|
6
|
+
|
|
7
|
+
The request is executed by **Upstash on your behalf**, so your application does not consume compute resources during the request duration.
|
|
8
|
+
|
|
9
|
+
If the endpoint responds with a non‑success status code (anything outside `200–299`),
|
|
10
|
+
`context.call()` still returns the response and the workflow continues.
|
|
11
|
+
This allows you to inspect the response (via the `status` field) and decide how to handle failure cases in your logic.
|
|
12
|
+
|
|
13
|
+
If you want requests to retry automatically, you can explicitly pass a retry configuration.
|
|
14
|
+
|
|
15
|
+
## Arguments
|
|
16
|
+
|
|
17
|
+
<ParamField body="url" type="string">
|
|
18
|
+
The URL of the HTTP endpoint to call.
|
|
19
|
+
</ParamField>
|
|
20
|
+
|
|
21
|
+
<ParamField body="method" type="string">
|
|
22
|
+
TThe HTTP method to use (`GET`, `POST`, `PUT`, etc.). Defaults to `GET`.
|
|
23
|
+
</ParamField>
|
|
24
|
+
|
|
25
|
+
<ParamField body="body" type="string">
|
|
26
|
+
The request body as a string.
|
|
27
|
+
</ParamField>
|
|
28
|
+
|
|
29
|
+
<ParamField body="headers" type="object">
|
|
30
|
+
A map of headers to include in the request.
|
|
31
|
+
</ParamField>
|
|
32
|
+
|
|
33
|
+
<ParamField body="retries" >
|
|
34
|
+
Number of retry attempts if the request fails. Defaults to `0` (no retries).
|
|
35
|
+
</ParamField>
|
|
36
|
+
|
|
37
|
+
<ParamField body="retryDelay" >
|
|
38
|
+
Delay between retries (in milliseconds). By default, uses exponential backoff. You can use mathematical expressions and the special variable `retried` (current retry attempt count starting from 0). Examples: `1000`, `pow(2, retried)`, `max(10, pow(2, retried))`.
|
|
39
|
+
</ParamField>
|
|
40
|
+
|
|
41
|
+
<ParamField body="flowControl" type="object" optional>
|
|
42
|
+
Throttle outbound requests.
|
|
43
|
+
|
|
44
|
+
See [Flow Control](/workflow/features/flow-control) for details.
|
|
45
|
+
|
|
46
|
+
<Expandable title="properties">
|
|
47
|
+
<ParamField body="key" type="string">
|
|
48
|
+
A logical grouping key that identifies which requests share the same flow control limits.
|
|
49
|
+
</ParamField>
|
|
50
|
+
|
|
51
|
+
<ParamField body="rate" type="number">
|
|
52
|
+
The maximum number of allowed requests per second.
|
|
53
|
+
</ParamField>
|
|
54
|
+
|
|
55
|
+
<ParamField body="parallelism" type="number">
|
|
56
|
+
The maximum number of concurrent requests allowed.
|
|
57
|
+
</ParamField>
|
|
58
|
+
|
|
59
|
+
<ParamField body="period" type="string|number">
|
|
60
|
+
The time window used to enforce the defined rate limit. Default is `1s`.
|
|
61
|
+
</ParamField>
|
|
62
|
+
</Expandable>
|
|
63
|
+
</ParamField>
|
|
64
|
+
|
|
65
|
+
<ParamField body="timeout" type="number" >
|
|
66
|
+
Maximum time (in seconds) to wait for a response.
|
|
67
|
+
If retries are enabled, this timeout applies individually to each attempt.
|
|
68
|
+
</ParamField>
|
|
69
|
+
|
|
70
|
+
<ParamField body="workflow" >
|
|
71
|
+
When using [`serveMany`](/workflow/features/invoke/serveMany), you can call another workflow defined in the same `serveMany` by passing it to this parameter.
|
|
72
|
+
</ParamField>
|
|
73
|
+
|
|
74
|
+
## Response
|
|
75
|
+
|
|
76
|
+
<ResponseField name="status" type="number">
|
|
77
|
+
The HTTP response status code.
|
|
78
|
+
</ResponseField>
|
|
79
|
+
|
|
80
|
+
<ResponseField name="body" type="string">
|
|
81
|
+
The response body.
|
|
82
|
+
|
|
83
|
+
`context.call()` attempts to parse the body as JSON.
|
|
84
|
+
If parsing fails, the raw body string is returned.
|
|
85
|
+
</ResponseField>
|
|
86
|
+
|
|
87
|
+
<ResponseField name="headers" type="dictionary">
|
|
88
|
+
The response headers.
|
|
89
|
+
</ResponseField>
|
|
90
|
+
|
|
91
|
+
<Tip>
|
|
92
|
+
In TypeScript, you can declare the expected result type for strong typing:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
type ResultType = {
|
|
96
|
+
field1: string,
|
|
97
|
+
field2: number
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const result = await context.call<ResultType>( ... );
|
|
101
|
+
```
|
|
102
|
+
</Tip>
|
|
103
|
+
|
|
104
|
+
## Usage
|
|
105
|
+
|
|
106
|
+
<CodeGroup>
|
|
107
|
+
|
|
108
|
+
```javascript TypeScript
|
|
109
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
110
|
+
|
|
111
|
+
export const { POST } = serve<{ topic: string }>(async (context) => {
|
|
112
|
+
const { userId, name } = context.requestPayload;
|
|
113
|
+
|
|
114
|
+
const { status, headers, body } = await context.call("sync-user-data", {
|
|
115
|
+
url: "https://my-third-party-app", // Endpoint URL
|
|
116
|
+
method: "POST",
|
|
117
|
+
body: JSON.stringify({
|
|
118
|
+
userId,
|
|
119
|
+
name
|
|
120
|
+
}),
|
|
121
|
+
headers: {
|
|
122
|
+
authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```python Python
|
|
131
|
+
from fastapi import FastAPI
|
|
132
|
+
from upstash_workflow.fastapi import Serve
|
|
133
|
+
from upstash_workflow import AsyncWorkflowContext
|
|
134
|
+
|
|
135
|
+
app = FastAPI()
|
|
136
|
+
serve = Serve(app)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@dataclass
|
|
140
|
+
class Request:
|
|
141
|
+
topic: str
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@serve.post("/api/example")
|
|
145
|
+
async def example(context: AsyncWorkflowContext[Request]) -> None:
|
|
146
|
+
request: Request = context.request_payload
|
|
147
|
+
|
|
148
|
+
result = await context.call(
|
|
149
|
+
"generate-long-essay",
|
|
150
|
+
url="https://api.openai.com/v1/chat/completions",
|
|
151
|
+
method="POST",
|
|
152
|
+
body={
|
|
153
|
+
"model": "gpt-4o",
|
|
154
|
+
"messages": [
|
|
155
|
+
{
|
|
156
|
+
"role": "system",
|
|
157
|
+
"content": "You are a helpful assistant writing really long essays that would cause a normal serverless function to timeout.",
|
|
158
|
+
},
|
|
159
|
+
{"role": "user", "content": request["topic"]},
|
|
160
|
+
],
|
|
161
|
+
},
|
|
162
|
+
headers={
|
|
163
|
+
"authorization": f"Bearer {os.environ['OPENAI_API_KEY']}",
|
|
164
|
+
},
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
status, headers, body = result.status, result.headers, result.body
|
|
168
|
+
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
</CodeGroup>
|
|
172
|
+
|
|
173
|
+
<Tip>
|
|
174
|
+
We provide integrations for **OpenAI, Anthropic, and Resend**, allowing you to call their APIs with strongly typed request bodies using `context.call`.
|
|
175
|
+
See [`context.api`](/workflow/steps/api) for details.
|
|
176
|
+
</Tip>
|
|
177
|
+
|
|
178
|
+
<Info>
|
|
179
|
+
The `context.call()` function can make requests to any public API endpoint. However, it cannot:
|
|
180
|
+
|
|
181
|
+
- Make requests to localhost (unless you set up a local tunnel, [here's how](/workflow/howto/local-development/local-tunnel))
|
|
182
|
+
- Make requests to internal Upstash QStash endpoints.
|
|
183
|
+
</Info>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "context.cancel"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
All of the methods covered so far are used to define workflow steps.
|
|
6
|
+
|
|
7
|
+
`context.cancel` is different — it allows you to **explicitly cancel the current workflow run**.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
export const { POST } = serve<{ topic: string }>(async (context) => {
|
|
11
|
+
const payload = context.requestPayload
|
|
12
|
+
|
|
13
|
+
const result = await context.run("check if canceled", () => { ... });
|
|
14
|
+
|
|
15
|
+
if (result.cancel) {
|
|
16
|
+
await context.cancel() // cancel the workflow run
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
When a workflow run is canceled:
|
|
22
|
+
|
|
23
|
+
- It is labeled as **canceled** (not failed).
|
|
24
|
+
- The configured `failureFunction` **is not triggered**.
|
|
25
|
+
- No entries are sent to the **dead-letter queue (DLQ)**.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "context.createWebhook"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
`context.createWebhook()` creates a unique webhook that can be called by external services to trigger workflow continuation.
|
|
6
|
+
|
|
7
|
+
The webhook URL generated can be called multiple times to resume multiple [`context.waitForWebhook`](/workflow/basics/context/waitForWebhook) steps.
|
|
8
|
+
|
|
9
|
+
## Arguments
|
|
10
|
+
|
|
11
|
+
<ParamField body="stepName" type="string" required>
|
|
12
|
+
Name of the step.
|
|
13
|
+
</ParamField>
|
|
14
|
+
|
|
15
|
+
## Response
|
|
16
|
+
|
|
17
|
+
<ResponseField name="webhookUrl" type="string">
|
|
18
|
+
The unique webhook URL that external services should call to resume the workflow.
|
|
19
|
+
|
|
20
|
+
Can be called multiple times to resume multiple [`context.waitForWebhook`](/workflow/basics/context/waitForWebhook) steps.
|
|
21
|
+
</ResponseField>
|
|
22
|
+
|
|
23
|
+
<ResponseField name="eventId" type="string">
|
|
24
|
+
The internal event identifier associated with this webhook.
|
|
25
|
+
This is primarily used internally by [`context.waitForWebhook`](/workflow/basics/context/waitForWebhook).
|
|
26
|
+
</ResponseField>
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```typescript highlight={4}
|
|
31
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
32
|
+
|
|
33
|
+
export const { POST } = serve(async (context) => {
|
|
34
|
+
const webhook = await context.createWebhook("create webhook");
|
|
35
|
+
|
|
36
|
+
console.log(webhook.webhookUrl); // Use this URL with external services
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
For more complete examples and use cases, see [the page on webhooks](/workflow/features/webhooks).
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "context.invoke"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
`context.invoke()` triggers another workflow run and pauses until the invoked workflow finishes.
|
|
6
|
+
|
|
7
|
+
The calling workflow resumes once the invoked workflow either **succeeds**, **fails**, or is **canceled**.
|
|
8
|
+
|
|
9
|
+
<Note>
|
|
10
|
+
Workflows can only invoke other workflows that were served together in the same `serveMany` route.
|
|
11
|
+
For details, see [Invoke other workflows](/workflow/features/invoke).
|
|
12
|
+
</Note>
|
|
13
|
+
|
|
14
|
+
## Arguments
|
|
15
|
+
|
|
16
|
+
<ParamField body="workflow" type="function" required>
|
|
17
|
+
The workflow definition to invoke.
|
|
18
|
+
Must be a workflow exposed under the same `serveMany`.
|
|
19
|
+
</ParamField>
|
|
20
|
+
|
|
21
|
+
<ParamField body="body" type="any">
|
|
22
|
+
The payload to send to the invoked workflow.
|
|
23
|
+
This value will be set as `context.requestPayload` in the invoked workflow.
|
|
24
|
+
</ParamField>
|
|
25
|
+
|
|
26
|
+
<ParamField body="headers" type="object" optional>
|
|
27
|
+
Optional HTTP headers to forward to the invoked workflow.
|
|
28
|
+
This value will be set as `context.headers` in the invoked workflow.
|
|
29
|
+
</ParamField>
|
|
30
|
+
|
|
31
|
+
<ParamField body="workflowRunId" type="string" optional>
|
|
32
|
+
Override the workflow run ID for the invoked workflow.
|
|
33
|
+
Defaults to a new ID if not specified.
|
|
34
|
+
</ParamField>
|
|
35
|
+
|
|
36
|
+
<ParamField body="retries" type="number" optional>
|
|
37
|
+
Number of retry attempts configuration of the invoked workflow.
|
|
38
|
+
Defaults to `3`. Retries use exponential backoff.
|
|
39
|
+
</ParamField>
|
|
40
|
+
|
|
41
|
+
<ParamField body="retryDelay" type="number|string" optional>
|
|
42
|
+
Delay between retries of the invoked workflow.
|
|
43
|
+
</ParamField>
|
|
44
|
+
|
|
45
|
+
<ParamField body="flowControl" type="object" optional>
|
|
46
|
+
Flow control configuration of the invoked workflow.
|
|
47
|
+
|
|
48
|
+
See [Flow Control](/workflow/features/flow-control) for details.
|
|
49
|
+
|
|
50
|
+
<Expandable title="properties">
|
|
51
|
+
<ParamField body="key" type="string">
|
|
52
|
+
A logical grouping key that identifies which requests share the same flow control limits.
|
|
53
|
+
</ParamField>
|
|
54
|
+
|
|
55
|
+
<ParamField body="rate" type="number">
|
|
56
|
+
The maximum number of allowed requests per second.
|
|
57
|
+
</ParamField>
|
|
58
|
+
|
|
59
|
+
<ParamField body="parallelism" type="number">
|
|
60
|
+
The maximum number of concurrent requests allowed.
|
|
61
|
+
</ParamField>
|
|
62
|
+
|
|
63
|
+
<ParamField body="period" type="string|number">
|
|
64
|
+
The time window used to enforce the defined rate limit. Default is `1s`.
|
|
65
|
+
</ParamField>
|
|
66
|
+
</Expandable>
|
|
67
|
+
</ParamField>
|
|
68
|
+
|
|
69
|
+
## Response
|
|
70
|
+
|
|
71
|
+
<ResponseField name="body" type="any">
|
|
72
|
+
The response body returned by the invoked workflow.
|
|
73
|
+
</ResponseField>
|
|
74
|
+
|
|
75
|
+
<ResponseField name="isFailed" type="boolean">
|
|
76
|
+
`true` if the invoked workflow completed with failure.
|
|
77
|
+
</ResponseField>
|
|
78
|
+
|
|
79
|
+
<ResponseField name="isCanceled" type="boolean">
|
|
80
|
+
`true` if the invoked workflow was canceled before completion.
|
|
81
|
+
</ResponseField>
|
|
82
|
+
|
|
83
|
+
## Usage
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const { body, isFailed, isCanceled } = await context.invoke(
|
|
87
|
+
"invoke another workflow",
|
|
88
|
+
{
|
|
89
|
+
workflow: anotherWorkflow,
|
|
90
|
+
body: "test",
|
|
91
|
+
header: {...}, // headers to pass to anotherWorkflow (optional)
|
|
92
|
+
retries, // number of retries (optional, default: 3)
|
|
93
|
+
retryDelay, // delay between retries (optional, uses exponential backoff by default)
|
|
94
|
+
flowControl, // flow control settings (optional)
|
|
95
|
+
workflowRunId // workflowRunId to set (optional)
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
|