# Create an automation draft

This guide creates an inactive automation draft. It does not activate the automation or send a reply. Creating it
requires `automations:write` and a Creator or Manager role. Mochi must also have enabled the feature for the
organization.

Use the [generated OpenAPI contract](https://themochi.app/docs/api/openapi.json) for exact operation availability, input fields, and response schemas.

Creating an inactive definition is different from activating it. Creating a flow is also different from starting a
run. A general request to connect Mochi does not approve activation, a flow run, a message, or a stage change.

## Agree on the exact draft

Before any request, write a plan with:

- organization, member role, operation, and minimum scope;
- exact trigger keywords and media, active hours, whether it runs once per lead, and any lead-stage changes;
- every reply or flow step in execution order;
- whether the definition remains inactive;
- one caller-generated UUID4 idempotency key per intended write;
- what to do if only part succeeds or the provider result is unknown;
- post-write verification and rollback; and
- the separate explicit approval required before activation or a flow run.

Do not continue if you cannot confirm the current OpenAPI input fields or feature availability. Do not expand the
approved plan with loops, arbitrary expressions, unrelated recipients, or invented CLI commands.

## Create an inactive keyword automation

The following examples create one inactive `ANY_POST` keyword automation. They do not activate it and do not send the configured reply. Preserve the same idempotency key on a retry of this exact operation.

### cURL

```bash
curl --fail-with-body --silent --show-error \
  --request POST \
  --url "${MOCHI_BASE_URL}/v1/automations/keyword/" \
  --header "Authorization: Bearer ${MOCHI_API_KEY}" \
  --header "Content-Type: application/json" \
  --header "Idempotency-Key: ${MOCHI_IDEMPOTENCY_KEY}" \
  --data '{"name":"Agent draft - pricing","is_active":false,"keywords":["pricing"],"comment_type":"ANY_POST","replies":[{"text":"Thanks—our team will follow up."}]}'
```

### Python

```python
import os

import requests

payload = {
    "name": "Agent draft - pricing",
    "is_active": False,
    "keywords": ["pricing"],
    "comment_type": "ANY_POST",
    "replies": [{"text": "Thanks—our team will follow up."}],
}
response = requests.post(
    f"{os.environ['MOCHI_BASE_URL']}/v1/automations/keyword/",
    headers={
        "Authorization": f"Bearer {os.environ['MOCHI_API_KEY']}",
        "Idempotency-Key": os.environ["MOCHI_IDEMPOTENCY_KEY"],
    },
    json=payload,
    timeout=30,
)
response.raise_for_status()
print({"id": response.json()["id"], "is_active": response.json()["is_active"]})
```

### Node.js / TypeScript

```typescript
const baseUrl = process.env.MOCHI_BASE_URL ?? "https://api.themochi.app";
const payload = {
  name: "Agent draft - pricing",
  is_active: false,
  keywords: ["pricing"],
  comment_type: "ANY_POST",
  replies: [{ text: "Thanks—our team will follow up." }],
};
const response = await fetch(new URL("/v1/automations/keyword/", baseUrl), {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MOCHI_API_KEY}`,
    "Content-Type": "application/json",
    "Idempotency-Key": process.env.MOCHI_IDEMPOTENCY_KEY!,
  },
  body: JSON.stringify(payload),
  signal: AbortSignal.timeout(30_000),
});
if (!response.ok) throw new Error(`Mochi HTTP ${response.status}`);
const automation = (await response.json()) as { id: string; is_active: boolean };
console.log({ id: automation.id, isActive: automation.is_active });
```

### PHP

```php
<?php
$payload = [
    "name" => "Agent draft - pricing",
    "is_active" => false,
    "keywords" => ["pricing"],
    "comment_type" => "ANY_POST",
    "replies" => [["text" => "Thanks—our team will follow up."]],
];
$baseUrl = getenv("MOCHI_BASE_URL") ?: "https://api.themochi.app";
$handle = curl_init($baseUrl . "/v1/automations/keyword/");
curl_setopt_array($handle, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("MOCHI_API_KEY"),
        "Content-Type: application/json",
        "Idempotency-Key: " . getenv("MOCHI_IDEMPOTENCY_KEY"),
    ],
    CURLOPT_POSTFIELDS => json_encode($payload, JSON_THROW_ON_ERROR),
]);
$body = curl_exec($handle);
$status = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
curl_close($handle);
if ($body === false || $status < 200 || $status >= 300) throw new RuntimeException("Mochi write failed");
$automation = json_decode($body, true, flags: JSON_THROW_ON_ERROR);
echo json_encode(["id" => $automation["id"], "is_active" => $automation["is_active"]]);
```

## Verify and activate separately

Verify the returned name, trigger type, comment type, reply count, and `is_active: false`. Inspect the definition in Mochi with a human reviewer. If it is wrong, delete or correct the inactive draft through a currently documented and separately approved operation; do not activate it as a test.

Creating a flow and starting a flow run are separate writes with separate idempotency keys. A run must name one
approved flow and lead.

Each flow is locked to one channel. `POST /v1/flows/` takes an optional `channel`, `INSTAGRAM` or `WHATSAPP`, and
flow responses return it. A flow created without `channel` is an Instagram flow, as every flow created before the
field existed. A `WHATSAPP` flow:

- can only be created when Mochi has enabled WhatsApp for the organization; otherwise the request fails with the
  typed error `whatsapp_not_enabled`;
- starts only for a lead who has written to the organization on WhatsApp; otherwise the run fails with
  `lead_history_required`. An Instagram flow likewise needs Instagram history, and WhatsApp messages do not count
  for it;
- sends on the lead's WhatsApp conversation, and only inside WhatsApp's 24-hour reply window. Outside the window
  the run pauses and resumes when the lead writes again on WhatsApp.

A lead's reply resumes or stops only the flows of the channel it arrived on. Recheck the current send and messaging-window rules before starting it. Stop if the provider
result is unknown. Starting a run requires new explicit approval after someone reviews the inactive definition.

Follow [Idempotency](https://themochi.app/docs/api/troubleshooting-and-safety/idempotency), [Errors](https://themochi.app/docs/api/troubleshooting-and-safety/errors), and [Rate limits](https://themochi.app/docs/api/troubleshooting-and-safety/rate-limits). Never retry a mutation with a new key merely because its response was lost.

## Work on canvas automations

With `automations:write`, read `GET /v1/automations/definitions/capabilities/` for this workspace's supported features and document format. Use `GET /v1/automations/definitions/assets/?query=guide` to look up reusable tracked links and finished Content Hub media. A media variant references its workspace's file by `file_id` and `media_kind`. Voice generation or recording happens before that file is attached.

1. List definitions with `GET /v1/automations/definitions/`. Continue with `?after_id=<next_after_id>` when more results are available.
2. Validate a candidate via `POST /v1/automations/definitions/validate/` with `{"document": ...}`. Include `automation_id` for an edit.
3. Create an inactive automation via `POST /v1/automations/definitions/` with `{"document": ...}`.
4. Read an existing definition at `GET /v1/automations/definitions/{automation_id}/`. The response contains its draft when present.
5. Save draft edits with `PUT` at the same URL. Send the full `document` and the `revision` and `etag` returned by the read. After a conflict, fetch the current document and reapply the intended changes. Review and publish in the app.

All POST and PUT requests require an Idempotency-Key UUID4. Reuse that key only to retry the same request. Draft endpoints are available where the workspace supports drafts.

MCP exposes the same operations as `get_automation_capabilities`, `list_automation_definitions`, `get_automation_definition`, `search_automation_assets`, `validate_automation_definition`, `create_automation_draft` and `edit_automation_draft`. Provide a stable caller-generated UUID for each new MCP draft. Reusing it returns the existing automation. MCP creation and editing require an active Creator or Manager.

Creating, reading or editing a definition returns an absolute `editor_url` for the saved automation and a
`flow_preview` with readable trigger, reply and step summaries. The editor requires access to the workspace.
MCP Apps hosts can display the same saved snapshot inline. Text-only clients can show the flow and editor link
from the tool result. Use `document` for the complete definition and for subsequent edits.
