# Workflows

Run long tasks in steps, then check their status and result later with a run id.

Hosts: Cloudflare, Vercel, Node, Docker.

```ts [server/workflows/onboard-user/index.ts]
import { defineWorkflow } from 'vite-hub/workflow'

// Each numbered file in this folder is a step.
// They run in the order you call them.
export default defineWorkflow(
  'onboard-user',
  async (payload: { email: string }, { steps }) => {
    const user = await steps.createUser(payload)
    return await steps.sendWelcome(user)
  },
)
```

```ts [server/workflows/onboard-user/01.create-user.ts]
// Step 1: create the user
export default async function createUser(input: {
  email: string
}) {
  // Save the user in your database here
  const id = `user:${input.email}`
  return { id, email: input.email }
}
```

```ts [server/workflows/onboard-user/02.send-welcome.ts]
// Step 2: send the welcome message
export default async function sendWelcome(user: {
  id: string
  email: string
}) {
  return { userId: user.id, welcomeSent: true }
}
```

```ts [server/api/onboard.post.ts]
import { defineEventHandler, readBody } from 'h3'
import { startWorkflow } from 'vite-hub/workflow'

// Start a run and return its id at once
export default defineEventHandler(async (event) => {
  const body = await readBody<{ email: string }>(event)
  return startWorkflow('onboard-user', body)
})
```

```ts [server/api/runs/[id].get.ts]
import { defineEventHandler, getRouterParam } from 'h3'
import { getWorkflowRun } from 'vite-hub/workflow'

// Read the status and result of one run
export default defineEventHandler((event) => {
  const id = getRouterParam(event, 'id')!
  return getWorkflowRun('onboard-user', id)
})
```

### [One file or folder per workflow](https://vitehub.dev/docs/workflows/configure#define-a-workflow)

Add a file to `server/workflows`. Its name is the workflow name.

### [Split work into steps](https://vitehub.dev/docs/workflows/get-started#define-the-workflow-steps)

Put numbered files in a workflow folder. A durable host can retry a failed step.

### [Start now, check later](https://vitehub.dev/docs/workflows/server-api#start-a-run)

`startWorkflow()` starts a run and returns its id. Your route does not wait for the work.

### [Status and result](https://vitehub.dev/docs/workflows/server-api#inspect-a-run)

`getWorkflowRun()` returns the status (queued, running, completed, or failed) and the result.

### [Choose where runs live](https://vitehub.dev/docs/workflows/hosts#providers)

Use Cloudflare, Vercel, or OpenWorkflow with Postgres or SQLite on your own server. Routes stay the same.

### [Clear error codes](https://vitehub.dev/docs/workflows/limits-and-errors#structured-errors)

Throw an error with a fixed code that is the same on every host.

Use Workflows for provider-tracked work that can outlive a request and needs durable state, retries, or steps. A run id is the handle for inspection.

Make each step idempotent and define terminal failure behavior. Keep request handlers small: start the run, return its id, and inspect the run separately.

Prove start, retry or resume, cancellation, and a terminal result. Inspect provider state instead of assuming the request process owns the run.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/workflows.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/workflows/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/workflows/configure.md)
- [Server API](https://vitehub.dev/raw/docs/workflows/server-api.md)
- [Hosts](https://vitehub.dev/raw/docs/workflows/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/workflows/limits-and-errors.md)
- [Concepts](https://vitehub.dev/raw/docs/workflows/concepts.md)

Next: [WORKFLOW_R0001](https://vitehub.dev/raw/docs/workflows/diagnostics/workflow_r0001.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
