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

# Configuration

> openworkflow.config.ts reference for database connections, workflow file discovery, and worker concurrency settings

OpenWorkflow uses a configuration file to connect to your database, locate your
workflow files, and set worker options. The CLI reads this file automatically
for all commands.

If you ran `npx @openworkflow/cli init`, this file was created for you.

If you're using the PostgreSQL backend, install the Postgres driver:

```bash npm theme={null}
npm i openworkflow postgres
```

```bash pnpm theme={null}
pnpm add openworkflow postgres
```

```bash bun theme={null}
bun add openworkflow postgres
```

## Config File

Create `openworkflow.config.ts` (or `.js`) in your project root:

```ts theme={null}
import { defineConfig } from "@openworkflow/cli";
import { BackendPostgres } from "openworkflow/postgres";

export default defineConfig({
  backend: await BackendPostgres.connect(
    process.env.OPENWORKFLOW_POSTGRES_URL!,
  ),
  dirs: ["./openworkflow"],
});
```

The CLI automatically finds this file by searching up from the current
directory. If your config lives somewhere else, pass an explicit path:

```bash theme={null}
npx @openworkflow/cli worker start --config src/openworkflow.config.ts
```

## Verify Configuration

After creating or updating the config, run:

```bash npm theme={null}
npx @openworkflow/cli doctor
```

```bash pnpm theme={null}
pnpx @openworkflow/cli doctor
```

```bash bun theme={null}
bunx @openworkflow/cli doctor
```

This confirms that your config can be loaded, the backend can be reached, and
your workflows can be found.

## Configuration Options

### `backend` (required)

The storage backend for workflow state. See [PostgreSQL](https://openworkflow.dev/docs/postgres.md) or
[SQLite](https://openworkflow.dev/docs/sqlite.md).

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

// PostgreSQL (production)
backend: await BackendPostgres.connect(process.env.OPENWORKFLOW_POSTGRES_URL!),

// SQLite (development)
backend: BackendSqlite.connect("./openworkflow/backend.db"),
```

### `dirs`

Directory or directories to scan for workflow files. Defaults to
`["./openworkflow"]`.

```ts theme={null}
// Single directory
dirs: "./openworkflow",

// Multiple directories
dirs: ["./openworkflow", "./src/workflows"],
```

The CLI recursively scans these directories for files with extensions: `.ts`,
`.mts`, `.cts`, `.js`, `.mjs`, `.cjs`.

### `ignorePatterns`

Glob patterns to exclude when discovering workflow files:

```ts theme={null}
ignorePatterns: [
  "**/*.test.*",      // Ignore test files
  "**/*.run.*",       // Ignore runner scripts
  "**/__fixtures__/**", // Ignore fixtures
],
```

The pattern `**/*.run.*` is ignored by default to exclude runner scripts.

### `worker`

Worker configuration options:

```ts theme={null}
worker: {
  // Number of concurrent workflow executions (default: 1)
  concurrency: 4,
  // How long a claimed run stays leased without a heartbeat (default: 30000)
  leaseDurationMs: 30_000,
},
```

## Full Example

```ts theme={null}
import { defineConfig } from "@openworkflow/cli";
import { BackendPostgres } from "openworkflow/postgres";

export default defineConfig({
  // PostgreSQL for production, with namespace
  backend: await BackendPostgres.connect(
    process.env.OPENWORKFLOW_POSTGRES_URL!,
    {
      namespaceId: process.env.OPENWORKFLOW_NAMESPACE_ID || "default",
    },
  ),

  // Scan multiple directories
  dirs: ["./openworkflow", "./src/workflows"],

  // Ignore test files
  ignorePatterns: ["**/*.test.*", "**/*.spec.*"],

  // Worker configuration
  worker: {
    concurrency: 8,
  },
});
```

## Environment-Based Configuration

Use environment variables for different environments:

```ts theme={null}
import { defineConfig } from "@openworkflow/cli";
import { BackendPostgres } from "openworkflow/postgres";
import { BackendSqlite } from "openworkflow/sqlite";

const isDev = process.env.NODE_ENV !== "production";

export default defineConfig({
  backend: isDev
    ? BackendSqlite.connect("./openworkflow/backend.db")
    : await BackendPostgres.connect(process.env.OPENWORKFLOW_POSTGRES_URL!),

  dirs: ["./openworkflow"],

  worker: {
    concurrency: isDev ? 1 : 8,
  },
});
```

## Loading Environment Variables

The CLI loads `.env` files automatically. Add your database URL and other
secrets there:

```bash theme={null}
# .env
OPENWORKFLOW_POSTGRES_URL=postgresql://user:pass@localhost:5432/openworkflow
OPENWORKFLOW_NAMESPACE_ID=development
```

## Supported Extensions

Config files can use these extensions:

* `openworkflow.config.ts`
* `openworkflow.config.mts`
* `openworkflow.config.cts`
* `openworkflow.config.js`
* `openworkflow.config.mjs`
* `openworkflow.config.cjs`


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