Skip to main content

Python SDK for Upstash Workflow

Project description

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, 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.

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

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 .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

upstash_workflow-0.0.1rc1.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

upstash_workflow-0.0.1rc1-py3-none-any.whl (39.0 kB view details)

Uploaded Python 3

File details

Details for the file upstash_workflow-0.0.1rc1.tar.gz.

File metadata

  • Download URL: upstash_workflow-0.0.1rc1.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.8.18 Linux/6.8.0-1017-azure

File hashes

Hashes for upstash_workflow-0.0.1rc1.tar.gz
Algorithm Hash digest
SHA256 bfbefa6581e13d23adb9c9a12d8487d614645bd3d10cddac2060cc46a89fb2e7
MD5 2761a04a1da252bda72b31e6de4dd68e
BLAKE2b-256 747da21a6aee04783ab14a74544c72ecdc7c02372d75c524291d33e6a28e9920

See more details on using hashes here.

File details

Details for the file upstash_workflow-0.0.1rc1-py3-none-any.whl.

File metadata

  • Download URL: upstash_workflow-0.0.1rc1-py3-none-any.whl
  • Upload date:
  • Size: 39.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.8.18 Linux/6.8.0-1017-azure

File hashes

Hashes for upstash_workflow-0.0.1rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 e237e9a7e081c18f11ddfb0f44d507679c4d9d6c9794f5fc4c20415f3707b223
MD5 d63959a6715f90b8a62be97c69d28820
BLAKE2b-256 4ca6086d60831da4372d01fe9f85a541e13c17782e7635c8c830d3fac45dc6a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page