Skip to main content

Upstash Workflow SDK

Upstash Workflow lets you write durable, reliable and performant serverless functions. Get delivery guarantees, automatic retries on failure, scheduling and more without managing any infrastructure.

See the documentation for more details

Quick Start

Here, we will briefly showcase how you can get started with Upstash Workflow using FastAPI.

Alternatively, you can check our quickstarts for different frameworks, including FastAPI and Next.js & FastAPI.

Install

First, create a new directory and set up a virtual environment:

python -m venv venv
source venv/bin/activate

Then, install the required packages:

pip install fastapi uvicorn upstash-workflow

Get QStash token

Go to Upstash Console and copy the QSTASH_TOKEN, set it in the .env file.

export QSTASH_TOKEN=

Define a Workflow Endpoint

To declare workflow endpoints, use the @serve.post decorator. Save the following code to main.py:

from fastapi import FastAPI
from upstash_workflow.fastapi import Serve
from upstash_workflow import AsyncWorkflowContext

app = FastAPI()
serve = Serve(app)

# mock function
def some_work(input: str) -> str:
    return f"processed '{input}'"

# serve endpoint which expects a string payload:
@serve.post("/example")
async def example(context: AsyncWorkflowContext[str]) -> None:
    # get request body:
    input = context.request_payload

    async def _step1() -> str:
        output = some_work(input)
        print("step 1 input", input, "output", output)
        return output

    # run the first step:
    result: str = await context.run("step1", _step1)

    async def _step2() -> None:
        output = some_work(result)
        print("step 2 input", result, "output", output)

    # run the second step:
    await context.run("step2", _step2)

In the example, you can see that steps are declared through the context object.

The kinds of steps which are available are:

  • context.run: execute a function
  • context.sleep: sleep for some time
  • context.sleep_until: sleep until some timestamp
  • context.call: make a third party call without consuming any runtime

You can learn more about these methods from our documentation.

Run the Server

Upstash Workflow needs a public URL to orchestrate the workflow. Check out our Local Development guide to learn how to set up a local tunnel.

Create the tunnel and set the UPSTASH_WORKFLOW_URL environment variable in the .env file with the public URL:

ngrok http localhost:8000
export UPSTASH_WORKFLOW_URL=

Then, set the environment variables:

source .env

Finally, run the server:

uvicorn main:app --reload

FastAPI server will be running at localhost:8000.

Contributing

Development

  1. Clone the repository
  2. Install Poetry
  3. Install dependencies with poetry install
  4. Create a .env file with cp .env.example .env and fill in the environment variables
  5. Run tests with poetry run pytest
  6. Format with poetry run ruff format .
  7. Check with poetry run ruff check .
  8. Type check with poetry run mypy --show-error-codes .

Metadata

Release files for upstash-workflow 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for upstash-workflow 2.0.1
File Size Uploaded Provenance Actions
upstash_workflow-2.0.1.tar.gz 31.6 kB Without provenance Details for upstash_workflow-2.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for upstash-workflow 2.0.1
File Interpreter ABI Platform Actions
upstash_workflow-2.0.1-py3-none-any.whl Python 3 none any Details for upstash_workflow-2.0.1-py3-none-any.whl

Total release size: 75.8 kB

Release files / upstash_workflow-2.0.1.tar.gz

Download URL upstash_workflow-2.0.1.tar.gz
Size 31.6 kB
Tags Source
SHA-256 checksum
How to use checksums
359a31b0b8e0337546a2deebf1ceb57d5486883f8075ac38ac025b2c2f3fe8af
BLAKE2b-256 checksum
How to use checksums
1947425b42e7d4751ed526c3962456d5c9f284d1e0854a92b7347a774dcbb1c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.16 Linux/7.0.0-1017-azure

Release files / upstash_workflow-2.0.1-py3-none-any.whl

Download URL upstash_workflow-2.0.1-py3-none-any.whl
Size 44.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bdd2b3374abffa7664b7624bc029371ba0c2bd32876327ca35b60258777c47f1
BLAKE2b-256 checksum
How to use checksums
869967e3dfed2803d9e6cb7d50cf2e4516c7fcae2433efee2f78f0212a8a0f00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.16 Linux/7.0.0-1017-azure

Security for upstash-workflow 2.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page