# Write a workflow

A Whenever workflow is a TypeScript module: it declares when it runs, then does the work. A manifest of keyed triggers, named steps, one run function, and a typed context that carries everything the runtime supplies.

[Deploy your workflow](/deploy)

[Start for free](https://app.whenever.dev/)

## Let your coding agent write it

You do not have to write any of this by hand. Connect your coding agent to the Whenever MCP server at https://mcp.whenever.dev and describe the automation: the agent reads this contract, writes the module, checks it against the real build and saves it as a draft for you to publish. Everything below is what it writes, against the open-source SDK at [github.com/wix-incubator/whenever](https://github.com/wix-incubator/whenever).

## Three declarations, on purpose

Everything comes from one package, `@wix/whenever-workflow-sdk`, and a workflow module declares three kinds of thing:

`manifest`

When the workflow runs. Its name, and at least one trigger, each under its own stable key.

```ts
export const manifest: WorkflowManifest = {
  name: 'daily-rate-report',
  triggers: [daily({ key: 'morning', at: '09:00', tz: 'Europe/Vilnius' })],
};
```

`defineStep(name, operation)`

One named unit of work, recorded on every call. Calls to other services live inside a step.

```ts
export const readRate = defineStep('read-rate', async (ctx) => {
  const response = await ctx.integrations.http.get({ url: ctx.config.RATE_URL });
  return response.body;
});
```

`defineWorkflow(run)`

The default export: the run function that calls your steps in order, handed a typed context.

```ts
export default defineWorkflow<void, { rate: unknown }>(async (ctx) => {
  const rate = await readRate(ctx);
  ctx.log('read rate', { rate });
  return { rate };
});
```

They are separate so the publish-time extractor can read the manifest without invoking your run function. Triggers are known before a single line of your code has executed, so keep them as literals in the manifest, not something the run function computes.

## A daily rate, filed onward

Reads a rate every morning, annotates what it saw, and files a report — one step per externally visible action. For complete workflows with webhooks, several triggers and an AI agent, see [Workflow examples](/docs/examples).

```ts
import {
  daily,
  defineStep,
  defineWorkflow,
  manual,
  NonRetryableError,
  RetryableError,
  type WorkflowContext,
  type WorkflowManifest,
} from '@wix/whenever-workflow-sdk';

export const manifest: WorkflowManifest = {
  name: 'daily-rate-report',
  triggers: [
    daily({ key: 'morning-report', at: '09:00', tz: 'Europe/Vilnius' }),
    manual({ key: 'rerun' }),
  ],
};

// Every http.* result types body as unknown, so a guard is the only way in.
function isRate(body: unknown): body is { rate: number } {
  return (
    typeof body === 'object' && body !== null && 'rate' in body && typeof body.rate === 'number'
  );
}

export const readRate = defineStep('read-rate', async (ctx: WorkflowContext) => {
  const response = await ctx.integrations.http.get({ url: ctx.config.RATE_URL });

  if (!isRate(response.body)) {
    throw new NonRetryableError('the rate endpoint answered with no rate', {
      operation: 'http.get',
    });
  }

  ctx.log('read rate', { rate: response.body.rate });
  return response.body.rate;
});

export const fileReport = defineStep('file-report', async (ctx: WorkflowContext, rate: number) => {
  const receipt = await ctx.integrations.http.post({
    url: ctx.config.REPORT_URL,
    headers: { Authorization: `Bearer ${ctx.secrets.REPORT_API_KEY}` },
    body: { rate, observedAt: ctx.now() },
  });

  if (receipt.status >= 500) {
    throw new RetryableError('the report endpoint is not answering yet', {
      retryAfterMs: 60_000,
    });
  }
  return receipt;
});

export default defineWorkflow<void, { rate: number; status: number }>(async (ctx) => {
  const rate = await readRate(ctx);
  const receipt = await fileReport(ctx, rate);
  return { rate, status: receipt.status };
});
```

## Steps

[Workflow steps and context](/docs/steps#steps) covers named units of work, recorded results and safe error details.

## Trigger builders

[Workflow triggers](/docs/triggers#trigger-builders) lists manual, scheduled, one-time, webhook and managed event triggers.

## Signed webhooks

[Workflow webhooks](/docs/webhooks#signed-webhooks) documents sender verification and event filtering.

## WorkflowContext

[The context reference](/docs/steps#workflowcontext) describes input, config, secrets, trigger metadata and runtime utilities.

## Integrations

[Integrations in workflow code](/docs/integrations#integrations) covers built-in ports, workspace bindings and credential handling.

## Errors

[The error reference](/docs/steps#errors) explains failure recording and why a retryable error does not schedule a retry.

## Deploy and operate

Follow the [deployment guide](/docs/deploy) to prepare Setup and publish. Read [runtime limits and responsibilities](/docs/runtime-limits) before enabling a schedule, webhook or AI step.

## Stay deterministic

Take time and randomness from the context, using ctx.now() and ctx.random(), never from Date.now() or Math.random(). The runtime supplies both with the context, so a step that invents its own instead cannot be reproduced — the same input gives a different answer the next time you run it.
