﻿<head><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4357510538660394"
     crossorigin="anonymous"></script></head>> ## Documentation Index
> Fetch the complete documentation index at: https://openworkflow.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> ### When to use OpenWorkflow
>
> Use OpenWorkflow in TypeScript or JavaScript apps for:
>
> - Background jobs that need retries and must resume after a crash or deploy.
> - Multi-step integrations and data pipelines that need to save progress between operations.
> - AI workflows with multiple model or tool calls that need durable checkpoints.
> - Workflows that wait for a human approval, a webhook, or a timer before continuing.
>
> OpenWorkflow runs on Node.js 22.22+ or Bun, with PostgreSQL or SQLite storing workflow state. Your app enqueues runs, and a worker executes them.
>
> ### How to use OpenWorkflow
>
> Read and follow https://openworkflow.dev/skills/setup.md to set up OpenWorkflow.
>
> Define workflows with `defineWorkflow`, wrap operations in `step.run`, and enqueue runs with `ow.runWorkflow(workflow.spec, input)`. Start a worker with `npx @openworkflow/cli worker start` to execute them. See [Workflows](https://openworkflow.dev/docs/workflows.md), [Steps](https://openworkflow.dev/docs/steps.md), and [Signals](https://openworkflow.dev/docs/signals.md) for the APIs.

# Versioning

> Version workflows for backwards compatibility so existing runs finish with their old logic when you deploy changes

Workflow versioning lets you update a workflow's logic without breaking runs
that are already in progress.

## Why Version Workflows?

Imagine you have a "process-order" workflow and you want to change how it
validates orders. But there are already 50 order workflows running with the old
logic. If you just swap in the new code, those in-flight runs might fail because
their saved step history doesn't match the new code path.

Versioning solves this by letting you run both versions side by side:

* Existing runs continue with the old logic
* New runs use the updated version
* Once all old runs finish, you remove the old version

## Specifying a Version

Add a `version` to your workflow spec:

```ts theme={null}
import { defineWorkflow } from "openworkflow";

export const orderWorkflow = defineWorkflow(
  {
    name: "process-order",
    version: "2.0.0",
  },
  async ({ input, step }) => {
    // New v2 logic
    await step.run({ name: "validate-v2" }, async () => {
      return validateOrderV2(input);
    });

    await step.run({ name: "process-v2" }, async () => {
      return processWithNewLogic(input);
    });
  },
);
```

## Running Multiple Versions

Register multiple versions of the same workflow with different implementations:

```ts theme={null}
import { OpenWorkflow } from "openworkflow";
import { BackendPostgres } from "openworkflow/postgres";

const backend = await BackendPostgres.connect(
  process.env.OPENWORKFLOW_POSTGRES_URL,
);
const ow = new OpenWorkflow({ backend });

// Version 1 - for existing runs
ow.implementWorkflow(
  { name: "process-order", version: "1.0.0" },
  async ({ input, step }) => {
    await step.run({ name: "validate" }, () => validateOrderV1(input));
    await step.run({ name: "process" }, () => processV1(input));
  },
);

// Version 2 - for new runs
ow.implementWorkflow(
  { name: "process-order", version: "2.0.0" },
  async ({ input, step }) => {
    await step.run({ name: "validate-v2" }, () => validateOrderV2(input));
    await step.run({ name: "process-v2" }, () => processWithNewLogic(input));
  },
);

const worker = ow.newWorker();
await worker.start();
```

## Migration Strategy

A typical version migration:

1. **Deploy new version alongside old** - Register both versions in your worker
2. **Start new runs on new version** - Update your client code to use the new
   version
3. **Wait for old runs to complete** - Monitor the dashboard for remaining v1
   runs
4. **Remove old version** - Once all v1 runs are finished, remove the old
   implementation

## Version Matching

The worker matches workflow runs to implementations by name and version:

| Run Version | Registered Versions | Matched |
| - | - | - |
| `1.0.0` | `1.0.0`, `2.0.0` | `1.0.0` |
| `2.0.0` | `1.0.0`, `2.0.0` | `2.0.0` |
| `null` | `null`, `1.0.0` | `null` |
| `3.0.0` | `1.0.0`, `2.0.0` | No match (retry) |

If no matching version is found, the worker records an error and reschedules the
run as `pending` with retry backoff so a worker that has the matching version
can pick it up later.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.