# Mailing Campaign — agent self-approve

> **TL;DR for agents:** Agent flow: `POST /api/email-accounts` → `POST /api/creatives` → `POST /api/tasks` (status DRAFT) → `POST /api/mailing/:taskId/approve` (self-approve, scope `mailing:write`) → `POST /api/tasks/:id/start`. The same `mailing:write` scope is what an admin uses, so the agent literally is its own admin for its own campaigns.

## 1) Connect a sending mailbox

Scope: `accounts:write`. Provide IMAP/SMTP creds or OAuth payload — same shape as the UI account-creation form.

```bash
curl -s -X POST "https://api.live-direct-marketing.online/api/email-accounts" \
  -H "Authorization: Bearer $LDM_KEY" -H "Content-Type: application/json" \
  -d '{
    "name":"Outreach pool 1","email":"sales@example.com",
    "imapHost":"imap.example.com","imapPort":993,"imapUser":"sales@example.com","imapPass":"<secret>",
    "smtpHost":"smtp.example.com","smtpPort":587,"smtpUser":"sales@example.com","smtpPass":"<secret>"
  }'
```

## 2) Author the creative

Scope: `creatives:write`. Subject + bodyHtml support spintax `{a|b|c}` and `{{var}}` substitution from contact fields.

```bash
curl -s -X POST "https://api.live-direct-marketing.online/api/creatives" \
  -H "Authorization: Bearer $LDM_KEY" -H "Content-Type: application/json" \
  -d '{
    "name":"Engineer intro v1",
    "subject":"Quick {question|note} for {{firstName}} at {{companyName}}",
    "bodyHtml":"<p>Hi {{firstName}},</p><p>...</p>"
  }'
```

## 3) Create the mailing task

Scope: `tasks:write`. Mailing tasks created by agents land in `DRAFT` with `approvalStatus=PENDING` until approved. `methodId=2` is plain mailing.

```bash
curl -s -X POST "https://api.live-direct-marketing.online/api/tasks" \
  -H "Authorization: Bearer $LDM_KEY" -H "Content-Type: application/json" \
  -d '{
    "methodId":2,"description":"Engineer outreach — Wave 1",
    "creativeId":"<creative-uuid>","accountId":"<account-uuid>",
    "contactListId":"<list-uuid>"
  }'
```

## 4) Self-approve the task (agent acts as its own admin)

Scope: `mailing:write`. The same endpoint a human admin uses. Issue your bearer key with `mailing:write` and the agent can sign off its own campaigns — no ticket, no human in the loop.

```bash
curl -s -X POST "https://api.live-direct-marketing.online/api/mailing/$TASK_ID/approve" \
  -H "Authorization: Bearer $LDM_KEY" -H "Content-Type: application/json" \
  -d '{"note":"Auto-approved by agent — content reviewed by upstream guardrails"}'
```

After approval the task transitions to `PENDING_APPROVAL → ACTIVE` (when started). Approval is reset automatically if the creative is later edited (content drift safety).

## 5) Start delivery

Scope: `tasks:write`. Pulls items from the contact list, applies suppression/stop-list/marking guardrails, hands off to the SMTP+IMAP pipeline.

```bash
curl -s -X POST "https://api.live-direct-marketing.online/api/tasks/$TASK_ID/start" \
  -H "Authorization: Bearer $LDM_KEY"
```
