> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zapier.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Quick start: build a Next Gen Zap in code

> Create a project with the Zapier CLI, write a workflow with the @zapier/zapier-durable package, test it on your machine, and run it on Zapier as a Next Gen Zap.

Write a workflow in TypeScript, test it on your machine, and run it on Zapier as a [Next Gen Zap in code](https://docs.zapier.com/sdk/define-durable.md). A [Next Gen Zap](https://docs.zapier.com/zaps/what-is-a-zap.md) is an automated workflow that runs on Zapier.

To build a Next Gen Zap without code, ask your MCP client to [build one for you](https://docs.zapier.com/mcp/get-started/build-a-workflow.md).

## Agent routing guidance

* Follow these steps in order: `init`, write `workflow.ts`, test locally, `validate-workflow`, `create-workflow`, `publish-workflow-version`, `run-workflow`.
* Set `ZAPIER_STABILITY=beta` before running workflow commands.
* To change a live Zap, use a draft: `create-workflow-draft`, `update-workflow-draft`, `run-workflow-draft`, then `publish-workflow-draft`. Draft runs do not use tasks and do not affect the live Zap.

> [!TIP]
>
> To have your coding agent build it, give it this prompt:
>
> ```text theme={null}
> Build a Zap in code with @zapier/zapier-durable and the Zapier SDK that [describe your workflow]. Follow https://docs.zapier.com/sdk/build-a-zap-in-code.md. Test it locally and run validate-workflow before you deploy it.
> ```

> [!NOTE]
>
> The commands and SDK methods that create, publish, and run Next Gen Zaps are in the beta tier. They are ready to build against, but their shape may still change in a minor release. `@zapier/zapier-durable` is pre-1.0.

## Before you begin

* [Node.js](https://nodejs.org) 20 or later.
* A [Zapier account](https://zapier.com/sign-up). Next Gen Zaps are included in every Zapier plan. Free accounts get them through a [Professional plan trial in December 2026](https://docs.zapier.com/zaps/zap-limitations.md#free-plan). On an Enterprise plan, an account owner or super admin must first opt in to **Next Gen Zaps** on the [Beta features page](https://zapier.com/app/admin/beta-features) of the Admin Center, for each workspace that needs them.
* [jq](https://jqlang.org/download/), to read IDs from command output.

## 1. Run the CLI `init` command

Run `init` where you want the project folder:

```bash npm theme={null}
npx @zapier/zapier-sdk-cli@latest init my-first-zap
```

```bash pnpm theme={null}
pnpm dlx @zapier/zapier-sdk-cli@latest init my-first-zap
```

The `init` command:

1. Creates the `my-first-zap` folder with a `package.json`, a `tsconfig.json`, and a starter app in `src/index.ts`.
2. Asks to install the Zapier SDK, the Zapier CLI, TypeScript, and `tsx`.
3. Asks to log you in to Zapier. A browser window opens for you to approve the login.
4. Asks to run a sample program. You do not need it, so you can answer no.

If you are already logged in, the login step stops with "Already logged in" and the summary shows "Setup interrupted". Your files and packages are already in place, so you can continue.

Then go to the project folder and add the workflow packages. `@zapier/zapier-durable` defines the workflow. [Zod](https://zod.dev) is a TypeScript library for describing the shape of data, and the workflow uses it to define its input.

```bash npm theme={null}
cd my-first-zap
npm install @zapier/zapier-durable zod
```

```bash pnpm theme={null}
cd my-first-zap
pnpm add @zapier/zapier-durable zod
```

## 2. Write your first workflow

In the project folder, create a file named `workflow.ts`. The example that follows defines a workflow with no trigger, so it runs only when you start it. It has three steps: one builds a greeting from the input, one formats it with Formatter by Zapier, and one records the time.

```typescript workflow.ts theme={null}
import { defineDurable } from "@zapier/zapier-durable";
import { createZapierSdk } from "@zapier/zapier-sdk";
import { z } from "zod";

// Create the client outside run: code in run that is not inside a step cannot read the clock.
const zapier = createZapierSdk();

export default defineDurable({
  name: "greet-user",
  description: "Builds a greeting, formats it, and records when it ran.",
  inputSchema: z.object({ name: z.string() }),
  run: async (ctx, input) => {
    const draft = await ctx.step("compose-greeting", async () => {
      return `hello, ${input.name}! welcome to zapier.`;
    });

    const greeting = await ctx.step("format-greeting", async () => {
      const { data } = await zapier.runAction({
        app: "formatter",
        actionType: "write",
        action: "text_line_item",
        inputs: {
          transform: "string.titlecase",
          values: [{ inputs: draft }],
        },
      });
      // runAction returns untyped results, so check the shape before using it.
      return z.object({ output: z.string() }).parse(data[0]).output;
    });

    const sentAt = await ctx.step("record-time", async () => {
      return new Date().toISOString();
    });

    return { greeting, sentAt };
  },
});
```

Zapier runs this file when you publish, so keep the name `workflow.ts` and export the `defineDurable()` call as the default export.

The `format-greeting` step runs an action in [Formatter by Zapier](https://zapier.com/apps/formatter/integrations), a Zapier app that needs no app account or connection. `zapier.runAction()` works the same way for any of the apps on Zapier.

> [accordion] File name and input rules
>
> * If the file is missing, Zapier rejects the version with "No workflow entry point found". `workflow.mts`, `.js`, `.mjs`, `.cjs`, and `.cts` also work.
> * Zapier reads `inputSchema` to find the Next Gen Zap's input fields, so keep it in this file.
> * Your workflow checks input against `inputSchema` only when your own code calls it with input, such as in a local test. Zapier does not check the input of the runs it starts.
> * For every field `defineDurable()` accepts, read the [package README on GitHub](https://github.com/zapier/durable).

## 3. Test it on your machine

Create a file named `test-locally.ts` that calls the workflow with sample input:

```typescript test-locally.ts theme={null}
import greetUser from "./workflow.js";

const response = await greetUser({ name: "Alice" });
console.log(JSON.stringify(response, null, 2));
```

Run it:

```bash theme={null}
npx tsx test-locally.ts
```

The output includes `done: true`, the workflow's result, and the ID of this execution:

```json Response theme={null}
{
  "done": true,
  "result": {
    "greeting": "Hello, Alice! Welcome to Zapier.",
    "sentAt": "2026-11-24T14:44:06.294Z"
  },
  "executionId": "01a20e03-8712-7253-9871-fe2bc7bc780e"
}
```

A local run keeps its journal on disk in `~/.config/zapier-sdk/durable`. The Formatter step runs through your Zapier login, but the workflow itself is not on Zapier yet.

> [accordion] If the Formatter step fails with "Request denied by policy"
>
> The SDK could not find your Zapier login. Run the test from the project folder that `init` created, which has the Zapier CLI installed. If you set the project up another way, install the CLI in it and log in:
>
> ```bash theme={null}
> npm install -D @zapier/zapier-sdk-cli
> npx zapier-sdk login
> ```

## 4. Deploy it to Zapier

Before you deploy, check your code. `validate-workflow` checks `workflow.ts` without publishing anything, so you can also run it in CI. It checks connections and triggers only if you pass them:

```bash theme={null}
export ZAPIER_STABILITY=beta
npx zapier-sdk validate-workflow "$(jq -n --rawfile w workflow.ts '{"/workflow.ts": $w}')" --json
```

If your code passes, `issues` is empty:

```json Response theme={null}
{
  "data": {
    "issues": []
  },
  "errors": []
}
```

Each problem appears in `issues` with a message and the line it is on. The command exits with code 0 even when `issues` lists errors, so in CI, fail the job when `issues` is not empty. For example, add `| jq -e '.data.issues == []'` to the end of the command.

Create an empty Next Gen Zap, then publish `workflow.ts` as its first version. Publishing turns the Next Gen Zap on. It has no trigger, so `--manual` marks it as a Next Gen Zap that runs only when you start it.

```bash CLI theme={null}
export ZAPIER_STABILITY=beta

npx zapier-sdk create-workflow "Greet user" \
  --description "Builds a greeting, formats it, and records when it ran." \
  --json > zap.json
export WORKFLOW_ID=$(jq -r '.data.id' zap.json)

SOURCE_FILES=$(jq -n --rawfile w workflow.ts '{"workflow.ts": $w}')
npx zapier-sdk publish-workflow-version "$WORKFLOW_ID" "$SOURCE_FILES" \
  --manual \
  --json
```

```typescript TypeScript theme={null}
import { readFile } from "node:fs/promises";
import { createZapierSdk } from "@zapier/zapier-sdk/beta";

const zapier = createZapierSdk();

const { data: workflow } = await zapier.createWorkflow({
  name: "Greet user",
  description: "Builds a greeting, formats it, and records when it ran.",
});

const { data: version } = await zapier.publishWorkflowVersion({
  workflow: workflow.id,
  sourceFiles: { "workflow.ts": await readFile("workflow.ts", "utf8") },
  manual: true,
});

// WORKFLOW_ID for the next step, and the link to the Next Gen Zap in the Workflow Manager
console.log(workflow.id, workflow.editor_url, version.id);
```

The CLI saves the Next Gen Zap's details in `zap.json` and its ID in `WORKFLOW_ID`. If you use TypeScript, set `WORKFLOW_ID` to the printed ID before you run the next step: `export WORKFLOW_ID=YOUR_ZAP_ID`. The publish response includes the new version's `id`.

Zapier installs `@zapier/zapier-sdk` and zod for you, so you do not pass `--dependencies`. Formatter needs no connection, so you do not pass `--connections`. In production, set exact versions so each publish installs the same code. To set them, check "Choose package versions" below.

> [accordion] About the beta setting
>
> `ZAPIER_STABILITY=beta` turns on the CLI's beta commands for your terminal session. You can pass `--stability beta` to each command instead. Without it, the CLI stops with `error: unknown command 'create-workflow'`, and `WORKFLOW_ID` is empty, so the publish command fails too.
>
> The TypeScript example imports from `@zapier/zapier-sdk/beta` and needs no setting. Each beta command and method prints a warning that it is in the beta tier. The warning does not affect the result.

> [accordion] Choose package versions
>
> * Zapier installs zod unless you add your own version to `--dependencies`. It also installs `@zapier/zapier-sdk` when your workflow imports it.
> * Without `--zapier-durable-version`, Zapier uses its default version of `@zapier/zapier-durable`. Set the version with this flag, not `--dependencies`.
> * For `@zapier/zapier-durable` and `@zapier/zapier-sdk`, `latest` resolves when you publish to the newest release that has been on npm for at least 24 hours. Zapier stores the exact version with the published version, so its runs always use the same code.
> * If your workflow imports other npm packages, add them to `--dependencies` with exact versions.
> * In production and CI, set exact versions for every package, so that each publish installs the same code.

## 5. Run your Next Gen Zap

Start the Next Gen Zap with sample input. The command waits up to 60 seconds for the run to finish and then returns it.

```bash CLI theme={null}
npx zapier-sdk run-workflow "$WORKFLOW_ID" \
  --input '{"name": "Alice"}' \
  --json
```

```typescript TypeScript theme={null}
import { createZapierSdk } from "@zapier/zapier-sdk/beta";

const zapier = createZapierSdk();

const { data: run } = await zapier.runWorkflow({
  workflow: process.env.WORKFLOW_ID!,
  input: { name: "Alice" },
});

console.log(run.status, run.output);
```

When the run succeeds, `status` is `finished` and `output` holds the value your `run` function returned:

```json Response theme={null}
{
  "data": {
    "id": "01a20e08-faf9-7a36-b072-1397069f2602",
    "status": "finished",
    "input": { "name": "Alice" },
    "output": {
      "greeting": "Hello, Alice! Welcome to Zapier.",
      "sentAt": "2026-11-24T14:50:07.026Z"
    },
    "error": null
  },
  "errors": []
}
```

> [accordion] If the run is still going or failed
>
> If `status` is still `started` when the command returns, the run is still going, for example while a step retries. Check it again later with the run's `id`:
>
> ```bash theme={null}
> npx zapier-sdk get-workflow-run RUN_ID --json
> ```
>
> If `status` is `failed`, `error.message` names the step that failed. Fix `workflow.ts`, then publish a new version and run it again.

## 6. Check your Next Gen Zap in the Workflow Manager

Open the [Workflow Manager](https://docs.zapier.com/zaps/use-the-workflow-manager-to-build-and-view-your-zaps.md) link from `zap.json`:

```bash theme={null}
jq -r '.data.editor_url' zap.json
```

* Each step is a card, titled with the step's name.
* The code of the published version is read-only. To change it, select **Edit Zap**.
* Agentic management is on by default. When a run fails, the agent investigates and suggests a fix.

Each time you publish, Zapier stores a new version and runs that version from then on.

> [accordion] Keep your local code and the Next Gen Zap in sync
>
> Your local `workflow.ts` and the Next Gen Zap do not sync. Publishing sends a copy of your code to Zapier, and edits in the Workflow Manager stay on Zapier. Before you edit locally, download the latest published code with the version's `id`:
>
> ```bash theme={null}
> npx zapier-sdk get-workflow-version "$WORKFLOW_ID" VERSION_ID --json \
>   | jq -r '.data.source_files["workflow.ts"]' > workflow.ts
> ```
>
> To download unpublished edits from the Workflow Manager, use `get-workflow-draft` with the draft's ID from `list-workflow-drafts`.

> [accordion] Change a live Next Gen Zap and test it for free
>
> To change a Next Gen Zap without affecting its live runs, work in a draft:
>
> 1. `create-workflow-draft` copies the live version into a new draft.
> 2. `update-workflow-draft` saves your new `workflow.ts` to the draft.
> 3. `run-workflow-draft` runs the draft on Zapier. Pass the draft's ID and its `draft_revision` from the last create or update response. Draft runs do not use tasks.
> 4. `publish-workflow-draft` makes the draft the new live version.
>
> For each command's options, check the [CLI reference](https://docs.zapier.com/sdk/cli-reference.md).

> [accordion] If publishing fails with open\_drafts\_exist
>
> The Next Gen Zap has an open draft with unpublished edits, or a draft copied from an older version. If you publish directly, that draft could later overwrite your new version. To fix it, choose one:
>
> * Publish the draft with `publish-workflow-draft`.
> * Delete the draft with `discard-workflow-draft`.
> * Publish anyway with `--ignore-open-drafts`.
>
> A draft with no edits does not block publishing while it is copied from the current live version.

## Next steps

> [card] [Call apps from a Next Gen Zap](https://docs.zapier.com/sdk/call-apps-from-a-zap.md)
>
> Use apps that need a connection, such as Gmail and Google Sheets.

> [card] [Add a trigger](https://docs.zapier.com/sdk/call-apps-from-a-zap.md#4-deploy-it-with-your-connections)
>
> Run the Next Gen Zap when something happens in an app, such as a new form response.

> [card] [Run on your own servers](https://docs.zapier.com/sdk/run-workflows-on-your-servers.md)
>
> Keep your code on your servers while Zapier stores the journal and resumes executions.

> [card] [How Next Gen Zaps in code work](https://docs.zapier.com/sdk/define-durable.md)
>
> Learn how steps, waits, callbacks, and replays work.
