Skip to main content
Build your agent on Neon and provision backends you fully control. See how Ideavo does it
Function templatesFirst Party

Durable workflow with Upstash Workflow

A minimal multi-step job with retries and a durable sleep on Neon Functions, with job state in Lakebase Postgres.

About this template

This template runs a minimal multi-step job as a Neon Function. Upstash Workflow checkpoints each context.run, retries only the steps that fail, and hands context.sleep timers to QStash, so the function stops running while it waits. The function stores job state in Lakebase Postgres.

How it works

  1. POST /api/jobs starts a workflow run through QStash.
  2. QStash calls POST /api/workflow once per step:
    • start-job inserts the job with status running
    • wait-before-finishing sleeps durably for 5 seconds
    • finish-job marks the job completed
  3. GET /api/jobs/:id shows the current row, so you can see the job change from running to completed.
  4. Upstash Workflow retries a failed step without re-running the steps that already succeeded.
RoutePurpose
POST /api/jobsStart a workflow run
GET /api/jobs/:idRead a job's current state
POST /api/workflowQStash's signed workflow endpoint
GET /Route index

Scaffold it

Install the latest Neon CLI, then create the function from the registry:

npm install -g neon@latest
neon login
neon link
neon functions new upstash-workflow --slug upstashworkflow

The CLI writes functions/upstashworkflow/index.ts, declares the function in neon.ts, and adds the QStash keys to .env.example.

Configure Upstash

Create a free account at console.upstash.com, open QStash, pick a region, and copy all four values from the Quickstart section into .env.local:

QSTASH_URL="https://qstash.upstash.io"
QSTASH_TOKEN="eyJVc2VySUQiOi..."
QSTASH_CURRENT_SIGNING_KEY="sig_..."
QSTASH_NEXT_SIGNING_KEY="sig_..."
VariableDescriptionRequired
QSTASH_URLQStash endpoint, copied from the Quickstart section of the Upstash Console. It is region-specific and may differ from https://qstash.upstash.io. Locally, use the URL printed by `npx @upstash/qstash-cli dev`.Yes
QSTASH_TOKENQStash token, copied from the Quickstart section of the Upstash Console. It is region-specific and must match QSTASH_URL. POST /api/jobs uses it to start workflow runs.Yes
QSTASH_CURRENT_SIGNING_KEYCurrent signing key, copied from the Quickstart section of the Upstash Console. It must match QSTASH_URL's region. The workflow endpoint uses it to verify that requests come from QStash.Yes
QSTASH_NEXT_SIGNING_KEYNext signing key, copied from the Quickstart section of the Upstash Console. It must match QSTASH_URL's region and is used to verify requests during key rotation.Yes

Run locally

Local development uses the QStash CLI, which you start in one terminal:

npx @upstash/qstash-cli dev

It prints a local QSTASH_URL (typically http://127.0.0.1:8080), token, and signing keys. Put those values in .env.local in place of the production ones, then load the file and start the function:

set -a && . ./.env.local && set +a
neon dev

Start a job:

curl -X POST http://localhost:8787/api/jobs \
  -H 'Content-Type: application/json' \
  -d '{ "jobId": "job_1" }'

The job starts in running, which you can check right away:

curl http://localhost:8787/api/jobs/job_1

After the 5-second sleep, the same request returns completed.

Deploy

neon deploy --env .env.local
neon functions get upstashworkflow

Then trigger the deployed workflow with the same curl and the function URL as the host.

Notes

  • The function creates the jobs table on its first run with CREATE TABLE IF NOT EXISTS, so there is no migration step.
  • POST /api/jobs has no authentication in this demo, so anyone with the function URL can start a workflow. Add authentication before you expose it.
  • Code outside context.run runs again on every step delivery. Put side effects and non-deterministic values inside a step, and return anything a later step needs.
  • Add more context.run calls around the sleep to extend the workflow. context.sleep("wait-before-finishing", "5s") simulates a processing window without keeping the function running.