> This page location: Functions > Templates > Durable workflow with Upstash Workflow
> Full Neon documentation index: https://neon.com/docs/llms.txt

# Durable workflow with Upstash Workflow

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

- First Party: maintained by Neon
- Built by: Neon
- Categories: First Party, Workflow
- Built with: [Lakebase Postgres](https://neon.com/docs/postgres/overview.md)

## Install

```bash
neon functions new upstash-workflow
```

## About this template

This template runs a minimal multi-step job as a [Neon Function](https://neon.com/docs/compute/functions/overview). [Upstash Workflow](https://upstash.com/docs/workflow/getstarted) 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](https://neon.com/lakebase).

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

| Route                | Purpose                           |
| -------------------- | --------------------------------- |
| `POST /api/jobs`     | Start a workflow run              |
| `GET /api/jobs/:id`  | Read a job's current state        |
| `POST /api/workflow` | QStash's signed workflow endpoint |
| `GET /`              | Route index                       |

### Scaffold it

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

```bash
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](https://console.upstash.com/), open **QStash**, pick a region, and copy all four values from the **Quickstart** section into `.env.local`:

```bash
QSTASH_URL="https://qstash.upstash.io"
QSTASH_TOKEN="eyJVc2VySUQiOi..."
QSTASH_CURRENT_SIGNING_KEY="sig_..."
QSTASH_NEXT_SIGNING_KEY="sig_..."
```

| Variable | Description | Required |
| --- | --- | --- |
| `QSTASH_URL` | The QStash endpoint URL is in the Quickstart section of the Upstash Console. It depends on the region and may differ from https://qstash.upstash.io. Locally, use the URL that \`npx @upstash/qstash-cli dev\` prints. | Yes |
| `QSTASH_TOKEN` | The QStash token is in the Quickstart section of the Upstash Console and must come from the same region as QSTASH\_URL. POST /api/jobs uses it to start workflow runs. | Yes |
| `QSTASH_CURRENT_SIGNING_KEY` | The current signing key is in the Quickstart section of the Upstash Console and must come from the same region as QSTASH\_URL. The workflow endpoint uses it to verify that requests come from QStash. | Yes |
| `QSTASH_NEXT_SIGNING_KEY` | The next signing key is in the Quickstart section of the Upstash Console and must come from the same region as QSTASH\_URL. The workflow endpoint uses it to verify requests during key rotation. | Yes |

### Run locally

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

```bash
npx @upstash/qstash-cli dev
```

It prints a local `QSTASH_URL`, token, and signing keys. The local URL is usually `http://127.0.0.1:8080`. Put those values in `.env.local` in place of the production ones, then load the file and start the function:

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

Start a job:

```bash
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:

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

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

### Deploy

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

Then start a job on the deployed function with the same `curl` commands, using 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.

## Related

- Previous: [Durable workflow with Inngest](https://neon.com/functions/templates/inngest-workflow.md)
- Next: [Send email with Resend](https://neon.com/functions/templates/resend-email.md)
- [All templates](https://neon.com/functions/templates.md)
- [Template source](https://github.com/neondatabase/examples/tree/main/registry/upstash-workflow)
- [Use function templates](https://neon.com/docs/compute/functions/templates.md)
- [Contribute a function template](https://neon.com/docs/compute/functions/registry.md)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/functions/templates/upstash-workflow"}` to https://feedback.neon.tech/ (no auth required).