> For the complete documentation index, see [llms.txt](https://docs.mailofly.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mailofly.com/changelog/automation/rest-api-automation.md).

# REST API automation

Build custom integrations with Mailofly's REST API — sync CRMs, trigger sends from your backend, and automate campaign workflows.

**Base URL:** `https://www.mailofly.com/api/v1`

***

## Common patterns

### 1. Transactional send on user action

When a user signs up, completes a purchase, or resets a password:

```bash
curl -sS -X POST "https://www.mailofly.com/api/v1/compose" \
  -H "Authorization: Bearer mf_live_…" \
  -H "Content-Type: application/json" \
  -d '{
    "account_key": "acc_your_key",
    "template_id": "welcome-template-uuid",
    "recipients": { "emails": ["newuser@example.com"] },
    "variables": { "first_name": "Alex" }
  }'
```

### 2. Sync contacts from your database

```bash
# Upsert a contact
curl -sS -X POST "https://www.mailofly.com/api/v1/contacts" \
  -H "Authorization: Bearer mf_live_…" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "first_name": "Alex",
    "custom_fields": { "plan": "pro" }
  }'

# Add to a segment
curl -sS -X POST "https://www.mailofly.com/api/v1/segments/SEGMENT_ID/contacts" \
  -H "Authorization: Bearer mf_live_…" \
  -H "Content-Type: application/json" \
  -d '{ "contact_id": "CONTACT_UUID" }'
```

### 3. Trigger a campaign send

```bash
curl -sS -X POST "https://www.mailofly.com/api/v1/campaigns/CAMPAIGN_ID/send" \
  -H "Authorization: Bearer mf_live_…" \
  -H "Content-Type: application/json" \
  -d '{ "sendNow": true }'
```

### 4. Monitor delivery

```bash
curl -sS "https://www.mailofly.com/api/v1/mail-logs?status=failed&page_size=20" \
  -H "Authorization: Bearer mf_live_…"
```

***

## Zapier / n8n / Make

No native connector yet — use **HTTP request** steps:

| Step    | Config                                                              |
| ------- | ------------------------------------------------------------------- |
| Method  | POST                                                                |
| URL     | `https://www.mailofly.com/api/v1/compose`                           |
| Headers | `Authorization: Bearer mf_live_…`, `Content-Type: application/json` |
| Body    | JSON with `account_key`, `subject`, `body`, `recipients`            |

Trigger on: new CRM lead, form submission, Stripe payment, etc.

***

## SDK & code samples

| Language | Approach                                      |
| -------- | --------------------------------------------- |
| Node.js  | `fetch` or `axios` with Bearer header         |
| Python   | `requests.post(url, headers={…}, json={…})`   |
| Ruby     | `Net::HTTP` or `Faraday`                      |
| Go       | `http.NewRequest` with `Authorization` header |

Every endpoint page includes ready-to-run **cURL** — translate directly to your language.

***

## Error handling

```javascript
const res = await fetch("https://www.mailofly.com/api/v1/compose", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MAILOFLY_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(payload),
});

if (!res.ok) {
  const err = await res.json();
  // { error: "…", message: "…" }
  throw new Error(err.message);
}

const { data } = await res.json();
```

Retry **5xx** errors with exponential backoff. Don't retry **4xx** except **429** (rate limit).

***

## Related

* [API overview](https://docs.mailofly.com/api)
* [API keys](https://docs.mailofly.com/getting-started/api-keys)
* [Webhooks](/changelog/automation/webhooks.md) — coming soon for push-based events


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.mailofly.com/changelog/automation/rest-api-automation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
