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

# Campaigns

A campaign sends emails to a list of people or companies, from one of the workspace's senders. It is the API equivalent of the campaigns of the app: a campaign created through the API opens in the app like one created there, and the other way round.

> [!NOTE]
>
> The campaign endpoints are in beta. Through the API, you can list the
> senders you can send from, read campaigns and their messages, create, edit
> and delete drafts, send messages you wrote for each recipient, review
> campaigns and edit their messages, remove recipients, launch or schedule
> them, cancel a schedule, and stop a campaign or some of its recipients.

## Before you start

* An [API key](https://developer.folk.app/guides/quick-start.md#step-1-create-a-new-api-key).
* A plan that includes campaigns. Campaigns with follow-ups need sequences on your plan too.
* A sender, set up in the app: a Gmail account, or an address of your own domain, verified in the app. [List message senders](https://developer.folk.app/api-reference/campaigns/list-message-senders.md) returns the senders you can send from: yours, those shared with the workspace, and every sender of the workspace for admins. Senders still waiting for verification are left out.

## Send your first campaign

Send your first campaign to yourself: a person whose email address is yours, in your workspace. The steps below review the campaign before it sends, so you can read every message first.

**1. Pick a sender**

```bash theme={null}
curl "https://api.folk.app/v1/message-senders" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

Note the `id` of the sender to send from. `supportsSequences` says whether it can send follow-ups, and `provider` whether it can send an unsubscribe link (`custom_domain` only).

**2. Create the campaign and review it**

```bash theme={null}
curl -X POST "https://api.folk.app/v1/campaigns" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "target": "review",
    "sender": { "id": "snd_…" },
    "recipients": { "type": "contacts", "ids": ["per_…"] },
    "steps": [
      {
        "subject": "Quick question, {{firstName}}",
        "body": "<p>Hi {{firstName}},</p><p>Do you have 10 minutes this week?</p><p>{{signature}}</p>"
      }
    ]
  }'
```

The response is a [`202`](https://developer.folk.app/api-reference/success-responses#accepted) with the campaign `processing`, while its messages are generated. Check its `warnings`: they list the recipients that were skipped or have no email address.

**3. Wait for the review**

```bash theme={null}
curl "https://api.folk.app/v1/campaigns/cmp_…" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

Poll every few seconds until the campaign is `in_review`. If its `error` is set, its messages could not all be generated.

**4. Read the messages**

```bash theme={null}
curl "https://api.folk.app/v1/campaigns/cmp_…/messages" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

Each message is what one recipient receives, with the variables filled in. Edit one with [Update a message of a campaign](https://developer.folk.app/api-reference/campaigns/update-a-message-of-a-campaign.md) if needed.

**5. Launch it**

```bash theme={null}
curl -X POST "https://api.folk.app/v1/campaigns/cmp_…/launch" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Idempotency-Key: 9b1a3c2e-0d4f-4e5a-8b6c-7d8e9f0a1b2c"
```

The campaign is `sending`. Add `{ "scheduledAt": "2030-01-15T09:00:00+02:00" }` as the body to schedule it instead.

> [!WARNING]
>
> Launching sends emails that can't be unsent. An application or agent that
> launches campaigns for someone else should show them the campaign (its
> recipients, steps and send date) and get their approval first.

## Campaign, step and message

* A **campaign** is what the app calls a campaign: its recipients, its steps and its status. Its `name` is the subject of its first step, with each variable written `{label}`, as the app shows it.
* A **step** is one email of the campaign: a template, written once with `{{variables}}`, and sent to every recipient. The first step is the first email; the next ones are follow-ups, sent a number of days after the previous step (`delayDays`, 1 to 365), in the same email thread or not (`sameThread`). A campaign with more than one step is what the app calls a sequence.
* A **message** is the version of a step for one recipient, with the variables filled in. There is one message per recipient and per step.

For example, a campaign to 50 people with a first email and one follow-up has 2 steps and 100 messages. The first step reads `Hi {{firstName}}`, and the message to Alice reads `Hi Alice`.

## Writing a step

The subject is plain text and the body is HTML, with variables written `{{key}}`, such as `{{firstName}}`. [List the variables of a campaign](https://developer.folk.app/api-reference/campaigns/list-the-variables-of-a-campaign.md) returns every variable a campaign can use, with its `token` to write as given: the fields of its people or companies, and the text and numeric custom fields of its group, written `{{customFieldValues.grp_<uuid>.<field name>}}`. A variable without a value for a recipient is removed. Write `\{{` for literal braces.

* **Plain text**: set `bodyFormat: "text"` to write the body as plain text: a blank line starts a new paragraph, and a single newline is a line break. Bodies always read back as HTML.
* **HTML**: the body follows the format of the app's email editor. Other HTML, such as styles or tables, is simplified, and the response lists what changed in `warnings`.
* **Signature**: end the body with `<p>{{signature}}</p>` to sign it with the sender's signature, as returned by [List message senders](https://developer.folk.app/api-reference/campaigns/list-message-senders.md), and don't write the signature yourself. A body without `{{signature}}` is sent unsigned. When you write back a body you read, the token keeps the step's signature as it is, even if it was edited in the app.
* **Unsubscribe link**: write `<a href="{{unsubscribeLink}}">Unsubscribe</a>` in the body. Only `custom_domain` senders accept it.
* **Copies**: `cc` and `bcc` take up to 5 distinct addresses each.

Editing a draft with [Update a campaign](https://developer.folk.app/api-reference/campaigns/update-a-campaign.md) replaces all its steps, matched by position: read the campaign, change what you need, and send all its steps back.

## Send messages you wrote

When you write each recipient's email yourself, for instance with an LLM, pass the messages to [Create a campaign](https://developer.folk.app/api-reference/campaigns/create-a-campaign.md) in `messages`, with `target: "launch"` to send them, or `target: "review"` to read them first. They are sent as written, instead of being generated from a template.

```bash theme={null}
curl -X POST "https://api.folk.app/v1/campaigns" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 3f6c1d2e-8a4b-4c5d-9e0f-1a2b3c4d5e6f" \
  -d '{
    "target": "launch",
    "sender": { "id": "snd_…" },
    "steps": [{ "subject": "Congrats on the round" }],
    "messages": [
      {
        "recipient": { "id": "per_…" },
        "step": 1,
        "body": "<p>Hi Ana, congrats on the Series A!</p><p>{{signature}}</p>"
      },
      {
        "recipient": { "id": "per_…" },
        "step": 1,
        "subject": "Loved your talk, Tom",
        "body": "<p>Hi Tom, your talk on pricing stuck with me.</p><p>{{signature}}</p>"
      }
    ]
  }'
```

* **One message per recipient and step**, at most 100 per call. `step` is the position of the step the message belongs to.
* **Recipients**: leave `recipients` out to send to the recipients of the messages. With `recipients`, a recipient without a message on a step gets that step's template.
* **Steps**: a step with a `body` is the template for the recipients without a message on it. A step without a `body` is written entirely by its messages, so it needs one for every recipient.
* **Subjects**: a message without a `subject` takes the step's, which then can't have `{{variables}}`. A follow-up sent in the same thread takes the subject of the message that starts its thread.
* **Content is final**: `{{variables}}` are rejected, apart from `{{signature}}` and `{{unsubscribeLink}}`. `cc` and `bcc` default to the step's.
* **All or nothing**: a message whose recipient you can't access, isn't among `recipients`, or has no email address answers `422`, and nothing is created. The request body is limited to 500 KB, and answers `413` above.

For more than 100 messages, create the campaign from step templates with `target: "review"`, then rewrite its messages with [Update messages of a campaign in batch](https://developer.folk.app/api-reference/campaigns/update-messages-of-a-campaign-in-batch.md), 100 at a time, before launching it.

> [!WARNING]
>
> If you personalized messages, at create or during review, and move the
> campaign back to draft, the personalization is lost. The messages for every
> step are regenerated, the same way it works in the folk app.

## Recipients

A campaign sends to people or to companies, never both, and to 1 to 5,000 recipients. Pick them from a list of ids (`{ "type": "contacts", "ids": [...] }`), or from the contacts of the campaign's group (`{ "type": "group", "entityType": "person" }`), optionally filtered with the [filter syntax](https://developer.folk.app/api-reference/filtering.md) of the people and companies endpoints. Recipients can't change once the campaign is created, apart from removing them while it is a draft or in review.

Recipients you can't access are skipped, and recipients without an email address get no message: both are listed in the `warnings` of the response. Merged contacts resolve to the contact they were merged into.

## Senders

Every step is sent from a sender of the workspace. Write it as `{ "id": "snd_…" }` or `{ "email": "…" }`, either once for every step (root `sender`), or on every step, not both. Without one, the campaign uses the sender you last used.

* Gmail senders can only send campaigns with a single step: `supportsSequences` is `false` for them.
* `stopOnReply` (default `true`) stops the remaining steps for a recipient who replied. With follow-ups, it needs a sender whose mailbox is connected in the app, so that replies are tracked: with any other sender, set it to `false`, or the campaign can't go to review. The response warns about it.

## Statuses

| Status | Meaning | What you can do |
| - | - | - |
| `draft` | Stored, without messages. | [Update](https://developer.folk.app/api-reference/campaigns/update-a-campaign.md), [review](https://developer.folk.app/api-reference/campaigns/review-a-campaign.md), [remove recipients](https://developer.folk.app/api-reference/campaigns/remove-a-recipient-from-a-campaign.md), [delete](https://developer.folk.app/api-reference/campaigns/delete-a-campaign.md). |
| `processing` | Its messages are being generated, for a few seconds. | Wait: requests that change it answer `409` with a `Retry-After` header. |
| `in_review` | Its messages are ready to read and edit. | [Edit messages](https://developer.folk.app/api-reference/campaigns/update-a-message-of-a-campaign.md), remove recipients, [put it back to draft](https://developer.folk.app/api-reference/campaigns/put-a-campaign-back-to-draft.md), [launch](https://developer.folk.app/api-reference/campaigns/launch-a-campaign.md), delete. |
| `scheduled` | Launched, waiting for `scheduledAt`. | [Cancel the schedule](https://developer.folk.app/api-reference/campaigns/cancel-the-schedule-of-a-campaign.md), [stop](https://developer.folk.app/api-reference/campaigns/stop-a-campaign.md). |
| `sending` | Its steps are sending. | Stop it, or some of its recipients. |
| `finished` | Every step was sent. | Read it and its messages. |
| `cancelled` | Stopped before every step was sent. | Read it and its messages. |
| `failed` | Sending failed: `error` says why. | Read it and its messages. |

Each step has a status too: it reads `scheduled` once the campaign is launched, until it sends, then `sending` and `sent`. A request that the status of the campaign doesn't allow answers [`409`](https://developer.folk.app/api-reference/errors#conflict), with a `code` such as `CAMPAIGN_NOT_EDITABLE` or `CAMPAIGN_STATUS_CONFLICT`.

## Review

Reviewing a campaign generates its messages from its steps, as the app does before launch. Create the campaign with `target: "review"`, or review a draft with [`POST /v1/campaigns/:campaignId/review`](https://developer.folk.app/api-reference/campaigns/review-a-campaign). Both answer `202` with the campaign `processing`: it becomes `in_review` within seconds, to poll with [`GET /v1/campaigns/:campaignId`](https://developer.folk.app/api-reference/campaigns/get-a-campaign). If generation fails, the campaign is `in_review` with an `error`.

Every step needs a sender, a subject and a body, apart from follow-ups sent in the same thread, which take the subject of the first step, and steps written entirely by [your messages](#send-messages-you-wrote), which need no body.

While a campaign is in review:

* Edit a message with [`PATCH /v1/campaigns/:campaignId/messages/:messageId`](https://developer.folk.app/api-reference/campaigns/update-a-message-of-a-campaign), or up to 100 at once with [the batch endpoint](https://developer.folk.app/api-reference/campaigns/update-messages-of-a-campaign-in-batch.md). The content of a message is final, so variables are rejected, apart from `{{signature}}`, which keeps the message's signature, and `{{unsubscribeLink}}`, which links to the unsubscribe page of that message.
* Remove a recipient with [`DELETE /v1/campaigns/:campaignId/recipients/:contactId`](https://developer.folk.app/api-reference/campaigns/remove-a-recipient-from-a-campaign), which also works on drafts.
* Put it back to draft with [`POST /v1/campaigns/:campaignId/draft`](https://developer.folk.app/api-reference/campaigns/put-a-campaign-back-to-draft) to edit its steps. Its messages are generated again on the next review, so edits to single messages, and the messages you wrote when creating it, are lost, as in the app.

## Launch

Launching sends a campaign in review now, or schedules it. Launch it with [`POST /v1/campaigns/:campaignId/launch`](https://developer.folk.app/api-reference/campaigns/launch-a-campaign), with `scheduledAt` to schedule it, an ISO date and time with a timezone offset or `Z`. Campaigns send on the quarter hour (:00, :15, :30, :45): `scheduledAt` is rounded up to the next one, and the campaign returns the rounded time. Or create it with `target: "launch"` to review and launch it in one call.

Send an [`Idempotency-Key`](https://developer.folk.app/api-reference/idempotency) with every launch: a retry with the same key answers the first response and sends nothing.

A launch runs the checks of the app. A failed check answers an error and leaves the campaign in review:

| Check | Status | `code` |
| - | - | - |
| Messages left on your plan | [`402`](https://developer.folk.app/api-reference/errors#payment-required) | `MESSAGE_LIMIT_REACHED` |
| Follow-ups need sequences on your plan | `402` | `SEQUENCES_NOT_AVAILABLE` |
| You can send from the sender of every step | [`403`](https://developer.folk.app/api-reference/errors#forbidden) | `SENDER_NOT_ALLOWED`, with the step in `details.step` |
| Daily quota of a Gmail sender | [`429`](https://developer.folk.app/api-reference/errors#rate-limiting) | `SENDER_QUOTA_EXCEEDED` |

Once launched, the campaign goes through the spam check of the app: a campaign flagged as spam goes back to `in_review` with the error `spam_detected`.

With `target: "launch"`, create answers `202` with the campaign `processing`, which becomes `sending` or `scheduled` once its messages are generated, reading `in_review` for a moment while it launches. A check that fails then can't answer an error, so the campaign stays `in_review` with an `error` instead, such as `message_limit_reached`: fix it, then launch it with `POST /v1/campaigns/:campaignId/launch`, which answers the error of the table above.

> [!NOTE]
>
> Two fields name what went wrong. The `code` of an error response says why a
> request failed, in capitals (`MESSAGE_LIMIT_REACHED`). The `error` of a
> campaign says what went wrong with the campaign itself, in lowercase
> (`message_limit_reached`), and stays on it until it is fixed.

## After launch

Track a campaign with [Get a campaign](https://developer.folk.app/api-reference/campaigns/get-a-campaign.md): each step counts its `messageCount`, `sentCount` and `notSentCount`, and the campaign is `finished` once every step was sent. There are no webhooks for campaigns yet: poll it.

[List the messages of a campaign](https://developer.folk.app/api-reference/campaigns/list-the-messages-of-a-campaign.md) filters by `step`, `recipientId` and `status`. A message is `sent` or `not_sent`:

* `notSentReason` says why a message was not sent, such as `recipient_no_email`, `stopped_after_reply` or a bounce (`bounce_invalid_address`, …). The [message](https://developer.folk.app/api-reference/campaigns/get-a-message-of-a-campaign.md) lists the known values: handle unknown ones as `send_failed`.
* `unsubscribed` is `true` once the recipient unsubscribed through the link of that message.

## Change or stop a launched campaign

* **Scheduled**: [cancel its schedule](https://developer.folk.app/api-reference/campaigns/cancel-the-schedule-of-a-campaign.md) to edit it. It goes back to `in_review`, editable and launchable again, and nothing is sent.
* **Scheduled or sending**: [stop it](https://developer.folk.app/api-reference/campaigns/stop-a-campaign.md) for good. Messages already sent stay sent, and those that are stopped are not sent, with `notSentReason: "stopped"`.
  * Without a body, a scheduled campaign sends nothing, and a sending one sends none of the steps still waiting: the campaign is `cancelled`. The step being sent keeps sending to its remaining recipients, and once the last step is sending, this answers `409`.
  * With `recipientIds`, every message of these recipients not sent yet is stopped, in the step being sent too. The others still receive theirs. Use it for those who replied elsewhere, or to stop the remaining messages of the step being sent.
* **Draft or in review**: nothing was sent, so there is nothing to stop. [Delete it](https://developer.folk.app/api-reference/campaigns/delete-a-campaign.md), with its messages, or remove some of its recipients.


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