@upstash/workflow 1.4.0-rc → 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 +18 -2
- package/astro.d.ts +18 -2
- package/astro.js +129 -29
- package/astro.mjs +1 -1
- package/{chunk-UH6O5IJY.mjs → chunk-J3PXPHSB.mjs} +129 -29
- package/cloudflare.d.mts +16 -2
- package/cloudflare.d.ts +16 -2
- package/cloudflare.js +129 -29
- 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 +18 -2
- package/express.d.ts +18 -2
- package/express.js +129 -29
- package/express.mjs +1 -1
- package/h3.d.mts +18 -2
- package/h3.d.ts +18 -2
- package/h3.js +129 -29
- package/h3.mjs +1 -1
- package/hono.d.mts +16 -2
- package/hono.d.ts +16 -2
- package/hono.js +129 -29
- package/hono.mjs +1 -1
- package/index.d.mts +49 -21
- package/index.d.ts +49 -21
- package/index.js +145 -41
- package/index.mjs +17 -13
- package/nextjs.d.mts +39 -2
- package/nextjs.d.ts +39 -2
- package/nextjs.js +129 -29
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +15 -2
- package/react-router.d.ts +15 -2
- package/react-router.js +129 -29
- package/react-router.mjs +1 -1
- package/{serve-many-CbIV7145.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-BCtZg31b.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +9 -1
- package/solidjs.d.ts +9 -1
- package/solidjs.js +129 -29
- 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 +16 -2
- package/svelte.d.ts +16 -2
- package/svelte.js +129 -29
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +14 -2
- package/tanstack.d.ts +14 -2
- package/tanstack.js +129 -29
- package/tanstack.mjs +1 -1
- package/{types-ffLPVG5_.d.ts → types-CrgK5P6p.d.mts} +124 -24
- package/{types-ffLPVG5_.d.mts → types-CrgK5P6p.d.ts} +124 -24
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Upstash Workflow with FastAPI
|
|
3
|
+
sidebarTitle: FastAPI
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
<Card title="GitHub Repository" icon="github" href="https://github.com/upstash/workflow-py/tree/master/examples/fastapi" horizontal>
|
|
7
|
+
You can find the project source code on GitHub.
|
|
8
|
+
</Card>
|
|
9
|
+
|
|
10
|
+
This guide provides detailed, step-by-step instructions on how to use Upstash Workflow with FastAPI. You can also explore [the source code](https://github.com/upstash/workflow-py/tree/master/examples/fastapi) for a detailed, end-to-end example and best practices.
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
1. An Upstash QStash API key.
|
|
15
|
+
2. Python and pip installed.
|
|
16
|
+
|
|
17
|
+
If you haven't obtained your QStash API key yet, you can do so by [signing up](https://console.upstash.com/auth/sign-in) for an Upstash account and navigating to your QStash dashboard.
|
|
18
|
+
|
|
19
|
+
## Step 1: Installation
|
|
20
|
+
|
|
21
|
+
First, create a new directory and set up a virtual environment:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
python -m venv venv
|
|
25
|
+
source venv/bin/activate
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then, install the Workflow SDK and FastAPI:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install fastapi uvicorn upstash-workflow
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## Step 2: Configure Environment Variables
|
|
36
|
+
|
|
37
|
+
Create a `.env` file in your project root and add your QStash token. This token is used to authenticate your application with the QStash service.
|
|
38
|
+
|
|
39
|
+
```bash Terminal
|
|
40
|
+
touch .env
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Upstash Workflow is powered by [QStash](/qstash/overall/getstarted), which requires access to your endpoint to execute workflows. When your app is deployed, QStash will use the app's URL. However, for local development, you have two main options: [use a local QStash server or set up a local tunnel](/workflow/howto/local-development/development-server).
|
|
44
|
+
|
|
45
|
+
### Option 1: Local QStash Server
|
|
46
|
+
|
|
47
|
+
To start the local QStash server, run:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npx @upstash/qstash-cli dev
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Once the command runs successfully, you’ll see `QSTASH_URL` and `QSTASH_TOKEN` values in the console. Add these values to your `.env` file:
|
|
54
|
+
|
|
55
|
+
```bash .env
|
|
56
|
+
export QSTASH_URL="http://127.0.0.1:8080"
|
|
57
|
+
export QSTASH_TOKEN="<QSTASH_TOKEN>"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This approach allows you to test workflows locally without affecting your billing. However, runs won't be logged in the Upstash Console.
|
|
61
|
+
|
|
62
|
+
### Option 2: Local Tunnel
|
|
63
|
+
|
|
64
|
+
Alternatively, you can set up a local tunnel. For this option:
|
|
65
|
+
|
|
66
|
+
1. Copy the `QSTASH_TOKEN` from the Upstash Console (see [Get your QStash Token](/workflow/howto/get-token)).
|
|
67
|
+
2. Update your `.env` file with the following:
|
|
68
|
+
|
|
69
|
+
```bash .env
|
|
70
|
+
export QSTASH_TOKEN="***"
|
|
71
|
+
export UPSTASH_WORKFLOW_URL="<UPSTASH_WORKFLOW_URL>"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
- Replace `***` with your actual QStash token.
|
|
75
|
+
- Set `UPSTASH_WORKFLOW_URL` to the public URL provided by your local tunnel.
|
|
76
|
+
|
|
77
|
+
Here’s where you can find your QStash token:
|
|
78
|
+
|
|
79
|
+
<Frame>
|
|
80
|
+
<img src="/img/qstash-workflow/console-quickstart-env.jpg" alt="QStash environment variables in the Upstash Console" />
|
|
81
|
+
</Frame>
|
|
82
|
+
|
|
83
|
+
Using a local tunnel connects your endpoint to the production QStash, enabling you to view workflow logs in the Upstash Console.
|
|
84
|
+
|
|
85
|
+
## Step 3: Create a Workflow Endpoint
|
|
86
|
+
|
|
87
|
+
A workflow endpoint allows you to define a set of steps that, together, make up a workflow. Each step contains a piece of business logic that is automatically retried on failure, with easy monitoring via our visual workflow dashboard.
|
|
88
|
+
|
|
89
|
+
To define a workflow endpoint in a FastAPI project, create a `main.py` file that contains your workflow:
|
|
90
|
+
|
|
91
|
+
<Tabs>
|
|
92
|
+
<Tab title="Example">
|
|
93
|
+
```python main.py
|
|
94
|
+
from fastapi import FastAPI
|
|
95
|
+
from upstash_workflow.fastapi import Serve
|
|
96
|
+
|
|
97
|
+
app = FastAPI()
|
|
98
|
+
serve = Serve(app)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@serve.post("/api/workflow")
|
|
102
|
+
async def workflow(context) -> None:
|
|
103
|
+
async def _step1() -> None:
|
|
104
|
+
print("initial step ran")
|
|
105
|
+
|
|
106
|
+
await context.run("initial-step", _step1)
|
|
107
|
+
|
|
108
|
+
async def _step2() -> None:
|
|
109
|
+
print("second step ran")
|
|
110
|
+
|
|
111
|
+
await context.run("second-step", _step2)
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
</Tab>
|
|
115
|
+
<Tab title="Sleep">
|
|
116
|
+
```python main.py
|
|
117
|
+
from fastapi import FastAPI
|
|
118
|
+
import time
|
|
119
|
+
from upstash_workflow.fastapi import Serve
|
|
120
|
+
from upstash_workflow import AsyncWorkflowContext
|
|
121
|
+
|
|
122
|
+
app = FastAPI()
|
|
123
|
+
serve = Serve(app)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def some_work(input: str) -> str:
|
|
127
|
+
return f"processed '{input}'"
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@serve.post("/sleep")
|
|
131
|
+
async def sleep(context: AsyncWorkflowContext[str]) -> None:
|
|
132
|
+
input = context.request_payload
|
|
133
|
+
|
|
134
|
+
async def _step1() -> str:
|
|
135
|
+
output = some_work(input)
|
|
136
|
+
print("step 1 input", input, "output", output)
|
|
137
|
+
return output
|
|
138
|
+
|
|
139
|
+
result1: str = await context.run("step1", _step1)
|
|
140
|
+
|
|
141
|
+
await context.sleep_until("sleep1", time.time() + 3)
|
|
142
|
+
|
|
143
|
+
async def _step2() -> str:
|
|
144
|
+
output = some_work(result1)
|
|
145
|
+
print("step 2 input", result1, "output", output)
|
|
146
|
+
return output
|
|
147
|
+
|
|
148
|
+
result2: str = await context.run("step2", _step2)
|
|
149
|
+
|
|
150
|
+
await context.sleep("sleep2", 2)
|
|
151
|
+
|
|
152
|
+
async def _step3() -> None:
|
|
153
|
+
output = some_work(result2)
|
|
154
|
+
print("step 3 input", result2, "output", output)
|
|
155
|
+
|
|
156
|
+
await context.run("step3", _step3)
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
</Tab>
|
|
160
|
+
|
|
161
|
+
<Tab title="Call">
|
|
162
|
+
```python main.py
|
|
163
|
+
from fastapi import FastAPI
|
|
164
|
+
from typing import Dict
|
|
165
|
+
from upstash_workflow.fastapi import Serve
|
|
166
|
+
from upstash_workflow import AsyncWorkflowContext, CallResponse
|
|
167
|
+
|
|
168
|
+
app = FastAPI()
|
|
169
|
+
serve = Serve(app)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def some_work(input: str) -> str:
|
|
173
|
+
return f"processed '{input}'"
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@app.post("/get-data")
|
|
177
|
+
async def get_data() -> Dict[str, str]:
|
|
178
|
+
return {"message": "get data response"}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@serve.post("/call")
|
|
182
|
+
async def call(context: AsyncWorkflowContext[str]) -> None:
|
|
183
|
+
input = context.request_payload
|
|
184
|
+
|
|
185
|
+
async def _step1() -> str:
|
|
186
|
+
output = some_work(input)
|
|
187
|
+
print("step 1 input", input, "output", output)
|
|
188
|
+
return output
|
|
189
|
+
|
|
190
|
+
result1: str = await context.run("step1", _step1)
|
|
191
|
+
|
|
192
|
+
response: CallResponse[Dict[str, str]] = await context.call(
|
|
193
|
+
"get-data",
|
|
194
|
+
url=f"{context.env.get('UPSTASH_WORKFLOW_URL', 'http://localhost:8000')}/get-data",
|
|
195
|
+
method="POST",
|
|
196
|
+
body={"message": result1},
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
async def _step2() -> str:
|
|
200
|
+
output = some_work(response.body["message"])
|
|
201
|
+
print("step 2 input", response, "output", output)
|
|
202
|
+
return output
|
|
203
|
+
|
|
204
|
+
await context.run("step2", _step2)
|
|
205
|
+
|
|
206
|
+
```
|
|
207
|
+
</Tab>
|
|
208
|
+
<Tab title="Auth">
|
|
209
|
+
```python main.py
|
|
210
|
+
from fastapi import FastAPI
|
|
211
|
+
from upstash_workflow.fastapi import Serve
|
|
212
|
+
from upstash_workflow import AsyncWorkflowContext
|
|
213
|
+
|
|
214
|
+
app = FastAPI()
|
|
215
|
+
serve = Serve(app)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def some_work(input: str) -> str:
|
|
219
|
+
return f"processed '{input}'"
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
@serve.post("/auth")
|
|
223
|
+
async def auth(context: AsyncWorkflowContext[str]) -> None:
|
|
224
|
+
if context.headers.get("authentication") != "Bearer secret_password":
|
|
225
|
+
print("Authentication failed.")
|
|
226
|
+
return
|
|
227
|
+
|
|
228
|
+
async def _step1() -> str:
|
|
229
|
+
return "output 1"
|
|
230
|
+
|
|
231
|
+
await context.run("step1", _step1)
|
|
232
|
+
|
|
233
|
+
async def _step2() -> str:
|
|
234
|
+
return "output 2"
|
|
235
|
+
|
|
236
|
+
await context.run("step2", _step2)
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
</Tab>
|
|
240
|
+
</Tabs>
|
|
241
|
+
|
|
242
|
+
## Step 4: Run the Workflow Endpoint
|
|
243
|
+
|
|
244
|
+
Don't forget to source your environment file to set your environment variables:
|
|
245
|
+
|
|
246
|
+
```bash Terminal
|
|
247
|
+
source .env
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
After setting your live URL as the environment variable or `base_url` option, trigger your workflow by first starting your FastAPI app:
|
|
251
|
+
|
|
252
|
+
```bash Terminal
|
|
253
|
+
uvicorn main:app --reload
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
and then making a POST request to your workflow endpoint. For each workflow run, a unique workflow run ID is returned:
|
|
257
|
+
|
|
258
|
+
```bash Terminal
|
|
259
|
+
curl -X POST https://localhost:8000/api/workflow
|
|
260
|
+
|
|
261
|
+
# result: {"workflowRunId":"wfr_xxxxxx"}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
See the [documentation on starting a workflow](/workflow/howto/start) for other ways you can start your workflow.
|
|
265
|
+
|
|
266
|
+
<Frame>
|
|
267
|
+
<img src="/img/qstash-workflow/nextjs_local_request.png" alt="Local workflow request" />
|
|
268
|
+
</Frame>
|
|
269
|
+
|
|
270
|
+
If you are using a local tunnel, you can use this ID to track the workflow run and see its status in your QStash workflow dashboard. All steps are listed with their statuses, headers, and body for a detailed overview of your workflow from start to finish. Click on a step to see its detailed logs.
|
|
271
|
+
|
|
272
|
+
<Frame>
|
|
273
|
+
<img src="/img/qstash-workflow/dashboard.png" alt="Workflow run in the Upstash Console" />
|
|
274
|
+
</Frame>
|
|
275
|
+
|
|
276
|
+
## Next Steps
|
|
277
|
+
|
|
278
|
+
1. Learn how to protect your workflow endpoint from unauthorized access by [securing your workflow endpoint](/workflow/howto/security).
|
|
279
|
+
|
|
280
|
+
2. Explore [the source code](https://github.com/upstash/workflow-py/tree/master/examples/fastapi) for a detailed, end-to-end example and best practices.
|
|
281
|
+
|
|
282
|
+
3. For setting up and testing your workflows in a local environment, check out our [local development guide](/workflow/howto/local-development/development-server).
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Upstash Workflow with Flask
|
|
3
|
+
sidebarTitle: Flask
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
<Card title="GitHub Repository" icon="github" href="https://github.com/upstash/workflow-py/tree/master/examples/flask" horizontal>
|
|
7
|
+
You can find the project source code on GitHub.
|
|
8
|
+
</Card>
|
|
9
|
+
|
|
10
|
+
This guide provides detailed, step-by-step instructions on how to use Upstash Workflow with Flask. You can also explore [the source code](https://github.com/upstash/workflow-py/tree/master/examples/flask) for a detailed, end-to-end example and best practices.
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
1. An Upstash QStash API key.
|
|
15
|
+
2. Python and pip installed.
|
|
16
|
+
|
|
17
|
+
If you haven't obtained your QStash API key yet, you can do so by [signing up](https://console.upstash.com/auth/sign-in) for an Upstash account and navigating to your QStash dashboard.
|
|
18
|
+
|
|
19
|
+
## Step 1: Installation
|
|
20
|
+
|
|
21
|
+
First, create a new directory and set up a virtual environment:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
python -m venv venv
|
|
25
|
+
source venv/bin/activate
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then, install the Workflow SDK and Flask:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install fastapi uvicorn upstash-workflow
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## Step 2: Configure Environment Variables
|
|
36
|
+
|
|
37
|
+
Create a `.env` file in your project root and add your QStash token. This token is used to authenticate your application with the QStash service.
|
|
38
|
+
|
|
39
|
+
```bash Terminal
|
|
40
|
+
touch .env
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Upstash Workflow is powered by [QStash](/qstash/overall/getstarted), which requires access to your endpoint to execute workflows. When your app is deployed, QStash will use the app's URL. However, for local development, you have two main options: [use a local QStash server or set up a local tunnel](/workflow/howto/local-development/development-server).
|
|
44
|
+
|
|
45
|
+
### Option 1: Local QStash Server
|
|
46
|
+
|
|
47
|
+
To start the local QStash server, run:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npx @upstash/qstash-cli dev
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Once the command runs successfully, you’ll see `QSTASH_URL` and `QSTASH_TOKEN` values in the console. Add these values to your `.env` file:
|
|
54
|
+
|
|
55
|
+
```bash .env
|
|
56
|
+
export QSTASH_URL="http://127.0.0.1:8080"
|
|
57
|
+
export QSTASH_TOKEN="<QSTASH_TOKEN>"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This approach allows you to test workflows locally without affecting your billing. However, runs won't be logged in the Upstash Console.
|
|
61
|
+
|
|
62
|
+
### Option 2: Local Tunnel
|
|
63
|
+
|
|
64
|
+
Alternatively, you can set up a local tunnel. For this option:
|
|
65
|
+
|
|
66
|
+
1. Copy the `QSTASH_TOKEN` from the Upstash Console (see [Get your QStash Token](/workflow/howto/get-token)).
|
|
67
|
+
2. Update your `.env` file with the following:
|
|
68
|
+
|
|
69
|
+
```bash .env
|
|
70
|
+
export QSTASH_TOKEN="***"
|
|
71
|
+
export UPSTASH_WORKFLOW_URL="<UPSTASH_WORKFLOW_URL>"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
- Replace `***` with your actual QStash token.
|
|
75
|
+
- Set `UPSTASH_WORKFLOW_URL` to the public URL provided by your local tunnel.
|
|
76
|
+
|
|
77
|
+
Here’s where you can find your QStash token:
|
|
78
|
+
|
|
79
|
+
<Frame>
|
|
80
|
+
<img src="/img/qstash-workflow/console-quickstart-env.jpg" alt="QStash environment variables in the Upstash Console" />
|
|
81
|
+
</Frame>
|
|
82
|
+
|
|
83
|
+
Using a local tunnel connects your endpoint to the production QStash, enabling you to view workflow logs in the Upstash Console.
|
|
84
|
+
|
|
85
|
+
## Step 3: Create a Workflow Endpoint
|
|
86
|
+
|
|
87
|
+
A workflow endpoint allows you to define a set of steps that, together, make up a workflow. Each step contains a piece of business logic that is automatically retried on failure, with easy monitoring via our visual workflow dashboard.
|
|
88
|
+
|
|
89
|
+
To define a workflow endpoint in a Flask project, create a `main.py` file that contains your workflow:
|
|
90
|
+
|
|
91
|
+
<Tabs>
|
|
92
|
+
<Tab title="Example">
|
|
93
|
+
```python main.py
|
|
94
|
+
from flask import Flask
|
|
95
|
+
from upstash_workflow.flask import Serve
|
|
96
|
+
|
|
97
|
+
app = Flask(__name__)
|
|
98
|
+
serve = Serve(app)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@serve.route("/api/workflow")
|
|
102
|
+
def workflow(context) -> None:
|
|
103
|
+
def _step1() -> None:
|
|
104
|
+
print("initial step ran")
|
|
105
|
+
|
|
106
|
+
context.run("initial-step", _step1)
|
|
107
|
+
|
|
108
|
+
def _step2() -> None:
|
|
109
|
+
print("second step ran")
|
|
110
|
+
|
|
111
|
+
context.run("second-step", _step2)
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
</Tab>
|
|
115
|
+
<Tab title="Sleep">
|
|
116
|
+
```python main.py
|
|
117
|
+
from flask import Flask
|
|
118
|
+
import time
|
|
119
|
+
from upstash_workflow.flask import Serve
|
|
120
|
+
from upstash_workflow import WorkflowContext
|
|
121
|
+
|
|
122
|
+
app = Flask(__name__)
|
|
123
|
+
serve = Serve(app)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def some_work(input: str) -> str:
|
|
127
|
+
return f"processed '{input}'"
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@serve.route("/sleep")
|
|
131
|
+
def sleep(context: WorkflowContext[str]) -> None:
|
|
132
|
+
input = context.request_payload
|
|
133
|
+
|
|
134
|
+
def _step1() -> str:
|
|
135
|
+
output = some_work(input)
|
|
136
|
+
print("step 1 input", input, "output", output)
|
|
137
|
+
return output
|
|
138
|
+
|
|
139
|
+
result1: str = context.run("step1", _step1)
|
|
140
|
+
|
|
141
|
+
context.sleep_until("sleep1", time.time() + 3)
|
|
142
|
+
|
|
143
|
+
def _step2() -> str:
|
|
144
|
+
output = some_work(result1)
|
|
145
|
+
print("step 2 input", result1, "output", output)
|
|
146
|
+
return output
|
|
147
|
+
|
|
148
|
+
result2: str = context.run("step2", _step2)
|
|
149
|
+
|
|
150
|
+
context.sleep("sleep2", 2)
|
|
151
|
+
|
|
152
|
+
def _step3() -> None:
|
|
153
|
+
output = some_work(result2)
|
|
154
|
+
print("step 3 input", result2, "output", output)
|
|
155
|
+
|
|
156
|
+
context.run("step3", _step3)
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
</Tab>
|
|
160
|
+
|
|
161
|
+
<Tab title="Call">
|
|
162
|
+
```python main.py
|
|
163
|
+
from flask import Flask
|
|
164
|
+
from typing import Dict
|
|
165
|
+
from upstash_workflow.flask import Serve
|
|
166
|
+
from upstash_workflow import WorkflowContext, CallResponse
|
|
167
|
+
|
|
168
|
+
app = Flask(__name__)
|
|
169
|
+
serve = Serve(app)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def some_work(input: str) -> str:
|
|
173
|
+
return f"processed '{input}'"
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@app.route("/get-data", methods=["POST"])
|
|
177
|
+
def get_data() -> Dict[str, str]:
|
|
178
|
+
return {"message": "get data response"}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@serve.route("/call")
|
|
182
|
+
def call(context: WorkflowContext[str]) -> None:
|
|
183
|
+
input = context.request_payload
|
|
184
|
+
|
|
185
|
+
def _step1() -> str:
|
|
186
|
+
output = some_work(input)
|
|
187
|
+
print("step 1 input", input, "output", output)
|
|
188
|
+
return output
|
|
189
|
+
|
|
190
|
+
result1: str = context.run("step1", _step1)
|
|
191
|
+
|
|
192
|
+
response: CallResponse[Dict[str, str]] = context.call(
|
|
193
|
+
"get-data",
|
|
194
|
+
url=f"{context.env.get('UPSTASH_WORKFLOW_URL', 'http://localhost:8000')}/get-data",
|
|
195
|
+
method="POST",
|
|
196
|
+
body={"message": result1},
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
def _step2() -> str:
|
|
200
|
+
output = some_work(response.body["message"])
|
|
201
|
+
print("step 2 input", response, "output", output)
|
|
202
|
+
return output
|
|
203
|
+
|
|
204
|
+
context.run("step2", _step2)
|
|
205
|
+
|
|
206
|
+
```
|
|
207
|
+
</Tab>
|
|
208
|
+
<Tab title="Auth">
|
|
209
|
+
```python main.py
|
|
210
|
+
from flask import Flask
|
|
211
|
+
from upstash_workflow.flask import Serve
|
|
212
|
+
from upstash_workflow import WorkflowContext
|
|
213
|
+
|
|
214
|
+
app = Flask(__name__)
|
|
215
|
+
serve = Serve(app)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def some_work(input: str) -> str:
|
|
219
|
+
return f"processed '{input}'"
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
@serve.route("/auth")
|
|
223
|
+
def auth(context: WorkflowContext[str]) -> None:
|
|
224
|
+
if context.headers.get("Authentication") != "Bearer secret_password":
|
|
225
|
+
print("Authentication failed.")
|
|
226
|
+
return
|
|
227
|
+
|
|
228
|
+
def _step1() -> str:
|
|
229
|
+
return "output 1"
|
|
230
|
+
|
|
231
|
+
context.run("step1", _step1)
|
|
232
|
+
|
|
233
|
+
def _step2() -> str:
|
|
234
|
+
return "output 2"
|
|
235
|
+
|
|
236
|
+
context.run("step2", _step2)
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
</Tab>
|
|
240
|
+
</Tabs>
|
|
241
|
+
|
|
242
|
+
## Step 4: Run the Workflow Endpoint
|
|
243
|
+
|
|
244
|
+
Don't forget to source your environment file to set your environment variables:
|
|
245
|
+
|
|
246
|
+
```bash Terminal
|
|
247
|
+
source .env
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
After setting your live URL as the environment variable or `base_url` option, trigger your workflow by first starting your Flask app:
|
|
251
|
+
|
|
252
|
+
```bash Terminal
|
|
253
|
+
flask --app main run -p 8000
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
and then making a POST request to your workflow endpoint. For each workflow run, a unique workflow run ID is returned:
|
|
257
|
+
|
|
258
|
+
```bash Terminal
|
|
259
|
+
curl -X POST https://localhost:8000/api/workflow
|
|
260
|
+
|
|
261
|
+
# result: {"workflowRunId":"wfr_xxxxxx"}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
See the [documentation on starting a workflow](/workflow/howto/start) for other ways you can start your workflow.
|
|
265
|
+
|
|
266
|
+
<Frame>
|
|
267
|
+
<img src="/img/qstash-workflow/nextjs_local_request.png" alt="Local workflow request" />
|
|
268
|
+
</Frame>
|
|
269
|
+
|
|
270
|
+
If you are using a local tunnel, you can use this ID to track the workflow run and see its status in your QStash workflow dashboard. All steps are listed with their statuses, headers, and body for a detailed overview of your workflow from start to finish. Click on a step to see its detailed logs.
|
|
271
|
+
|
|
272
|
+
<Frame>
|
|
273
|
+
<img src="/img/qstash-workflow/dashboard.png" alt="Workflow run in the Upstash Console" />
|
|
274
|
+
</Frame>
|
|
275
|
+
|
|
276
|
+
## Next Steps
|
|
277
|
+
|
|
278
|
+
1. Learn how to protect your workflow endpoint from unauthorized access by [securing your workflow endpoint](/workflow/howto/security).
|
|
279
|
+
|
|
280
|
+
2. Explore our [the source code](https://github.com/upstash/workflow-py/tree/master/examples/flask) for a detailed, end-to-end example and best practices.
|
|
281
|
+
|
|
282
|
+
3. For setting up and testing your workflows in a local environment, check out our [local development guide](/workflow/howto/local-development/development-server).
|