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

# Send messages and place calls

> Write to leads on WhatsApp, Instagram and the web chat, read what they send, and have a voice agent call them.

[`POST /v1/messages`](https://docs.newlead.ai/api-reference/messages/send-a-message) sends free-form text or a WhatsApp template. [`POST /v1/calls`](https://docs.newlead.ai/api-reference/calls/create-a-call) makes a voice agent call a phone number. Both accept an `Idempotency-Key`: send one with every request, so that a retry after a timeout never sends or calls twice. See [Idempotency](https://docs.newlead.ai/idempotency.md).

## What each channel accepts

The lead's `channel` decides what you can send it:

| `channel` | `type: text` | `type: template` |
| - | - | - |
| `whatsapp`, `meta_ads` | Yes. Outside the [24-hour window](#the-24-hour-window), it goes out inside a template | Yes |
| `instagram` | Only within 24 hours of the lead's last message | No |
| `webchat` | Yes, at any time | No |
| `voice` | Through WhatsApp, if the voice agent shares its leads with a text agent | No. Send it to `text_agent_id` and `to` instead |
| `email` | No | No |

Where the table says no, the API returns `400 recipient_invalid` with `param: lead_id`. Instagram outside the window returns `409 messaging_window_closed`.

## Send text

```bash theme={null}
curl -X POST https://rest.newlead.ai/v1/messages \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: reply-ana-ferrer-002" \
  -d '{
    "type": "text",
    "lead_id": "<LEAD_ID>",
    "text": "Hi Ana, the apartment is still available. Would Thursday at 10:00 work for a viewing?"
  }'
```

The API returns `201` with the message:

```json theme={null}
{
  "object": "message",
  "id": "00000000-0000-4000-8000-000000000010",
  "lead_id": "00000000-0000-4000-8000-000000000003",
  "direction": "outbound",
  "sender": "agent",
  "origin": "api",
  "type": "text",
  "text": "Hi Ana, the apartment is still available. Would Thursday at 10:00 work for a viewing?",
  "template": null,
  "attachments": [],
  "reply_to_message_id": null,
  "status": "accepted",
  "error": null,
  "created_at": "2026-11-25T10:00:00.000Z",
  "sent_at": null,
  "delivered_at": null,
  "read_at": null,
  "failed_at": null,
  "lead_created": false,
  "warnings": []
}
```

* Text goes only to a `lead_id`, with up to 4096 characters. A long text can reach the lead as several messages: the response returns the first one.
* Sending text cancels a reply the AI was about to send, but it does not pause the AI, which keeps answering the lead's next messages. To take over the conversation, [pause the AI](#pause-and-resume-the-ai).

## The 24-hour window

WhatsApp and Instagram only accept free-form text within 24 hours of the lead's last message. The lead's `messaging_window_expires_at` says when the window closes. On those channels, `null` means the lead has not written yet, so the window is closed.

| Channel | When the window is closed |
| - | - |
| WhatsApp | The text goes out inside the agent's **Reply after 24 h** template, set in the agent's **Templates** panel, with your text in its blank. The message's `text` is the whole template. If the agent has no such template, or it is not approved, the API returns `409 messaging_window_closed`. |
| Instagram | Instagram has no templates, so the API returns `409 messaging_window_closed`. You can write again when the lead does. |
| Web chat | There is no window. |

```json theme={null}
{
  "error": {
    "type": "conflict_error",
    "code": "messaging_window_closed",
    "message": "The 24-hour messaging window with this lead is closed.",
    "param": null,
    "request_id": "req_m3k2a9_4f1c2b7e",
    "doc_url": "https://docs.newlead.ai/errors#messaging_window_closed"
  }
}
```

To reopen the conversation with a template of your choice, [send a template](#send-a-template).

## Send a template

Templates only go out on WhatsApp, and only when their `status` is `approved`.

### Find the template and its variables

```bash theme={null}
curl https://rest.newlead.ai/v1/text_agents/<TEXT_AGENT_ID>/templates \
  -H "Authorization: Bearer $NEWLEAD_API_KEY"
```

```json theme={null}
{
  "object": "list",
  "data": [
    {
      "object": "template",
      "id": "00000000-0000-4000-8000-00000000000b",
      "name": "viewing_follow_up",
      "language": "en",
      "category": "marketing",
      "status": "approved",
      "body_text": "Hi {{1}}, are you still interested in {{2}}?",
      "components": [
        {
          "type": "body",
          "format": null,
          "text": "Hi {{1}}, are you still interested in {{2}}?",
          "buttons": null
        }
      ],
      "variables": { "name": "{{1}}", "property": "{{2}}" },
      "rejection_reason": null,
      "created_at": "2026-11-13T09:00:00.000Z"
    }
  ],
  "has_more": false,
  "next_cursor": null
}
```

Send `name` and `language` exactly as listed: the API looks the template up by both. `variables` says what each placeholder stands for, for example `name` is `{{1}}`. The API does not fill placeholders for you: send every value in `template.components`. With no WhatsApp connected, the list is empty.

### Send it

A template goes to one of two recipients:

* An existing WhatsApp lead, with `lead_id`.
* A phone number, with `text_agent_id` and `to` in E.164. If the agent has no lead with that phone, the send creates one and the response carries `lead_created: true`.

```bash theme={null}
curl -X POST https://rest.newlead.ai/v1/messages \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: viewing-follow-up-ana-ferrer-001" \
  -d '{
    "type": "template",
    "lead_id": "<LEAD_ID>",
    "template": {
      "name": "viewing_follow_up",
      "language": "en",
      "components": [
        {
          "type": "body",
          "parameters": [
            { "type": "text", "text": "Ana" },
            { "type": "text", "text": "the apartment on Calle Mayor" }
          ]
        }
      ]
    },
    "pause_ai": true
  }'
```

The values go in WhatsApp's format, in placeholder order. A header with an image, video or document takes a parameter such as `{ "type": "image", "image": { "link": "https://example.com/flat.jpg" } }`. A URL button with a variable takes a component with `"type": "button"`, `"sub_type": "url"` and the button's `index`. If the values do not match the template, the API returns `400 parameter_invalid` with `param: template.components`.

### Template options

| Field | What it does |
| - | - |
| `lead_variables` | Merged into the lead's variables. `null` or `""` deletes a key. |
| `pause_ai` | Pauses the AI with no end date, so a person can take over. |
| `reset_conversation` | An existing lead starts the conversation over. |
| `source_lead_id` | The same person's lead on another channel, such as the call that led to this conversation. Both leads end up with the same `contact_id`. |

These steps run after WhatsApp accepts the template. If one fails, the template has still gone out: the API returns `201`, and `warnings` names the step that failed with `pause_ai_failed`, `variables_failed` or `conversation_reset_failed`. Do not send the template again. Repeat only that step, for example with [`pause_ai`](#pause-and-resume-the-ai) or [`PATCH /v1/leads/{lead_id}`](https://docs.newlead.ai/api-reference/leads/update-a-lead).

### Template errors

| Error | Cause |
| - | - |
| `404 resource_not_found` with `param: template.name` | The agent has no template with that `name` and `language`. |
| `409 template_not_approved` | The template is `pending`, `rejected`, `paused` or `disabled`. |
| `409 lead_do_not_contact` | The lead [asked not to be contacted](#when-a-lead-asks-not-to-be-contacted). |
| `409 agent_not_ready` | The agent has no WhatsApp connected. `details.blockers` is `["channel"]`. |
| `400 recipient_invalid` | WhatsApp rejected the number, or `lead_id` is not a WhatsApp lead with a phone. |

## Track delivery

The response carries the status at send time, usually `accepted`: WhatsApp took the message. `sent`, `delivered`, `read` and `failed` arrive later. To follow a message, read it again with [`GET /v1/messages/{message_id}`](https://docs.newlead.ai/api-reference/messages/retrieve-a-message), or listen to [`message.failed`](https://docs.newlead.ai/webhook-events#message-failed), which fires when a message to the lead fails. No event fires for `delivered` or `read`.

When a message fails, `error.code` is the provider's error code and `error.category` groups it, for example `recipient_not_reachable`.

## Read messages

[`GET /v1/leads/{lead_id}/messages`](https://docs.newlead.ai/api-reference/messages/list-messages) lists a lead's messages, newest first, in [pages](https://docs.newlead.ai/pagination.md). Internal platform notices are not included.

```bash theme={null}
curl "https://rest.newlead.ai/v1/leads/<LEAD_ID>/messages?limit=50" \
  -H "Authorization: Bearer $NEWLEAD_API_KEY"
```

| Field | Values |
| - | - |
| `direction` | `inbound` from the lead, `outbound` to the lead |
| `sender` | `lead`; `agent` for the AI or a person; `skills` for an agent tool |
| `origin` | `lead`, `ai`, `manual` for a person in the dashboard, `api`, `system` or `unclassified` |
| `type` | `text`, `template`, or `media` when the message has attachments |

No route lists messages across leads. To hear about new ones, subscribe to [`message.received`](https://docs.newlead.ai/webhook-events#message-received), or [poll the leads with new activity](https://docs.newlead.ai/sync-leads-to-crm.md) and read their messages.

### Attachments

A message from the lead can carry photos, videos, audio, voice notes, documents, stickers, locations or contacts. `attachments` lists each one with its `id`, `type`, `mime_type`, `file_name` and `file_size`. A caption arrives in `text`; without one, `text` is `null`. A voice note or an audio carries `transcription`, the text of what the lead said, or `null` if it could not be transcribed.

```json theme={null}
{
  "object": "message",
  "id": "00000000-0000-4000-8000-000000000011",
  "lead_id": "00000000-0000-4000-8000-000000000003",
  "direction": "inbound",
  "sender": "lead",
  "origin": "lead",
  "type": "media",
  "text": null,
  "template": null,
  "attachments": [
    {
      "object": "attachment",
      "id": "00000000-0000-4000-8000-000000000012",
      "type": "voice",
      "mime_type": "audio/ogg",
      "file_name": null,
      "file_size": 5120,
      "transcription": "Could I see the apartment on Saturday morning?"
    }
  ],
  "reply_to_message_id": null,
  "status": "received",
  "error": null,
  "created_at": "2026-11-25T10:05:00.000Z",
  "sent_at": null,
  "delivered_at": null,
  "read_at": null,
  "failed_at": null
}
```

`message.received` fires once the attachment has been processed, usually a few seconds after the message arrives.

### Download an attachment

[`GET /v1/messages/{message_id}/attachments/{attachment_id}/file`](https://docs.newlead.ai/api-reference/messages/download-a-message-attachment) redirects (`302`) to the file, with a signed link that is valid for one hour and downloads it under its `file_name`. Request this route each time you need the file instead of storing the link.

```bash theme={null}
curl https://rest.newlead.ai/v1/messages/<MESSAGE_ID>/attachments/<ATTACHMENT_ID>/file \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -L -o voice-note.ogg
```

Send the key in `Authorization: Bearer`, as with call recordings: clients do not forward it to the file's domain. An attachment without a stored file returns `404 resource_not_found`.

## Pause and resume the AI

When a person takes over a conversation, pause the AI for that lead so it stops replying:

```bash theme={null}
curl -X POST https://rest.newlead.ai/v1/leads/<LEAD_ID>/pause_ai \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "duration_minutes": 120,
    "reason": "The sales team follows up by phone"
  }'
```

```bash theme={null}
curl -X POST https://rest.newlead.ai/v1/leads/<LEAD_ID>/resume_ai \
  -H "Authorization: Bearer $NEWLEAD_API_KEY"
```

Both return the lead, with `ai_paused` and `ai_paused_until`.

* Without `duration_minutes`, or with `0`, the pause lasts until you call `resume_ai`. The maximum is 525600 minutes, one year.
* While the AI is paused, you can still send text and templates through the API. The voice agent cannot call the lead: `POST /v1/calls` returns `409 lead_ai_paused`.
* Pausing a lead that is already paused keeps the pause it has, with its end, and returns the lead. Resuming a lead that is not paused also returns the lead. A pause whose time is up counts as ended.

## When a lead asks not to be contacted

A lead's `do_not_contact` is `true` when they asked the agent to stop, or when you set it with [`PATCH /v1/leads/{lead_id}`](https://docs.newlead.ai/api-reference/leads/update-a-lead), for example after an opt-out in your CRM:

```bash theme={null}
curl -X PATCH https://rest.newlead.ai/v1/leads/<LEAD_ID> \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "do_not_contact": true }'
```

* The AI no longer replies to the lead, and on a call it only says the team will see it. Pending follow-ups are cancelled, and no follow-ups or appointment reminders are sent.
* Unlike a pause, it also stops you: text, templates and calls through the API return `409 lead_do_not_contact`.
* If the lead writes or calls again, the team gets an alert in the dashboard.
* `"do_not_contact": false` resumes contact, like **Resume contact** in the dashboard: the AI replies again and you can write and call. Follow-ups and appointment reminders stay off.
* It applies to one lead. A person can have one lead per channel and agent: find them all with `GET /v1/leads?phone=` and mark each one.

## When the send outcome is unknown

`502 send_outcome_unknown` means the message may have reached the lead even though the API could not confirm it.

> [!WARNING]
>
> Do not send it again with a new `Idempotency-Key` until you know what happened. A new key sends the message again. Repeating the request with the same key returns the same error and sends nothing.

1. Look for the message in [`GET /v1/leads/{lead_id}/messages`](https://docs.newlead.ai/api-reference/messages/list-messages).

2. For a template sent to a phone, find the lead first:

   ```bash theme={null}
   curl "https://rest.newlead.ai/v1/leads?phone=%2B34600111222" \
     -H "Authorization: Bearer $NEWLEAD_API_KEY"
   ```

3. Use a new key only once you know the message is not there, or that it `failed`.

## Calls

### Before you call

| Requirement | Error if it is not met |
| - | - |
| The voice agent's `status` is `active` in [`GET /v1/voice_agents`](https://docs.newlead.ai/api-reference/voice-agents/list-voice-agents) | `409 agent_not_ready`, with `details.blockers` set to `["inactive"]` when the voice agent is off, or `["text_agent"]` when the text agent it shares leads with is paused or deleted |
| The subscription and the subaccount are active | `402 subscription_inactive` |
| Your plan includes phone calls | `403 feature_disabled` |
| The voice agent has an outbound number | `409 voice_number_missing` |
| It is within the agent's hours for starting conversations | `409 outside_initiation_hours` |
| The AI is not paused for the lead, and the lead has not asked not to be contacted | `409 lead_ai_paused` or `409 lead_do_not_contact` |
| The wallet has balance | `402 insufficient_balance` |

These checks run before dialing. The ones about hours, the number, the plan and the contact do not create or change the lead. The balance check runs after the lead is found or created: the lead keeps the `lead` values you sent, and the refused call appears in [`GET /v1/calls`](https://docs.newlead.ai/api-reference/calls/list-calls) with `status: rejected` and `end_reason: rejected_balance`.

### Place a call

```bash theme={null}
curl -X POST https://rest.newlead.ai/v1/calls \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: call-ana-ferrer-001" \
  -d '{
    "voice_agent_id": "<VOICE_AGENT_ID>",
    "to": "+34600111222",
    "lead": {
      "name": "Ana Ferrer",
      "variables": { "property": "the apartment on Calle Mayor", "budget": 300000 }
    }
  }'
```

The API returns `201` with the ongoing call:

```json theme={null}
{
  "object": "call",
  "id": "00000000-0000-4000-8000-000000000001",
  "voice_agent_id": "00000000-0000-4000-8000-000000000002",
  "lead_id": "00000000-0000-4000-8000-000000000003",
  "direction": "outbound",
  "from": "+34910000000",
  "to": "+34600111222",
  "status": "in_progress",
  "end_reason": null,
  "outcome": null,
  "summary": null,
  "variables": {},
  "started_at": "2026-11-25T10:00:00.000Z",
  "ended_at": null,
  "duration_sec": null,
  "recording_url": null,
  "transcript_ready": false
}
```

* The voice agent calls from its outbound number. The API finds the agent's lead with that phone, or creates it, and returns its `lead_id`.
* `lead.name` replaces the lead's name and `lead.variables` are merged into its variables. Both are saved before dialing, and the agent uses them during the call. If the call then fails, the lead keeps them.
* Each API key can place 5 calls per minute. Over that, the API returns `429 rate_limit_exceeded` with `Retry-After`. Calls refused after the agent check, for example outside the agent's hours, also count. A request repeated with the same `Idempotency-Key` that returns the stored response does not. See [Rate limits](https://docs.newlead.ai/rate-limits.md).

### Retry a call safely

Idempotency is stricter on calls, because a repeated call rings a person's phone again.

* If the API cannot check the `Idempotency-Key`, it does not place the call and returns `502 provider_unavailable`. Retry with the same key.
* `502 send_outcome_unknown` means the phone may be ringing. `error.details.call_id` carries the call when it is known:

```json theme={null}
{
  "error": {
    "type": "provider_error",
    "code": "send_outcome_unknown",
    "message": "The voice provider did not confirm the call and the phone may be ringing. Check GET /v1/calls/00000000-0000-4000-8000-000000000001 before calling again with a new Idempotency-Key.",
    "param": null,
    "request_id": "req_m3k2a9_4f1c2b7e",
    "doc_url": "https://docs.newlead.ai/errors#send_outcome_unknown",
    "details": { "call_id": "00000000-0000-4000-8000-000000000001" }
  }
}
```

Read that call with [`GET /v1/calls/{call_id}`](https://docs.newlead.ai/api-reference/calls/retrieve-a-call). While its `status` is `in_progress`, the phone may still be ringing: wait for it to end. Without a `call_id`, list the agent's recent calls:

```bash theme={null}
curl "https://rest.newlead.ai/v1/calls?voice_agent_id=<VOICE_AGENT_ID>&started_after=2026-11-25T09:55:00Z" \
  -H "Authorization: Bearer $NEWLEAD_API_KEY"
```

Call again with a new key only once you know the first call did not reach the lead.

### Follow the call

| `status` | Meaning |
| - | - |
| `in_progress` | The call is ongoing. |
| `completed` | The call finished. |
| `failed` | It failed while dialing, opening the room or during the call. |
| `dropped` | It was cut off without the agent ending it. |
| `no_answer` | Nobody picked up. |
| `rejected` | It was never handled, for example for lack of balance. `end_reason` says why. |

To hear about the call as it happens, register a [webhook endpoint](https://docs.newlead.ai/webhooks.md) with `voice_agent_id` and these events:

* [`call.started`](https://docs.newlead.ai/webhook-events#call-started) fires once dialing is confirmed.
* [`call.ended`](https://docs.newlead.ai/webhook-events#call-ended) carries the final `status`, `end_reason`, duration and transcript.
* [`call.analyzed`](https://docs.newlead.ai/webhook-events#call-analyzed) carries the variables the AI extracted from the call.

Without webhooks, read the call with [`GET /v1/calls/{call_id}`](https://docs.newlead.ai/api-reference/calls/retrieve-a-call), which includes the transcript once `transcript_ready` is `true`. [`GET /v1/calls`](https://docs.newlead.ai/api-reference/calls/list-calls) lists calls without the transcript, and filters by `voice_agent_id`, `lead_id`, `direction`, `status`, `started_after` and `started_before`.

### Download the recording

[`GET /v1/calls/{call_id}/recording`](https://docs.newlead.ai/api-reference/calls/retrieve-a-call-recording) redirects (`302`) to the audio, with a signed link that is valid for one hour. Request this route each time you need the audio instead of storing the link.

```bash theme={null}
curl https://rest.newlead.ai/v1/calls/<CALL_ID>/recording \
  -H "Authorization: Bearer $NEWLEAD_API_KEY" \
  -L -o recording.ogg
```

Send the key in `Authorization: Bearer`: clients do not forward it when they follow the redirect to another domain, but they do forward `x-api-key`. A call without a recording returns `404 resource_not_found`, and its `recording_url` is `null`. The recording can arrive shortly after `call.ended`.

## Not available yet

* Sending images, documents or other files. `type` accepts only `text` and `template`; a template header can carry an image, video or document by link.

## Related

> [card] [Retry safely](https://docs.newlead.ai/idempotency.md)
>
> How `Idempotency-Key` works and what each response stores.

> [card] [Webhook events](https://docs.newlead.ai/webhook-events.md)
>
> The payload of each message and call event.

> [card] [Handle errors](https://docs.newlead.ai/errors.md)
>
> Every error code and when to retry.

> [card] [Sync leads to a CRM](https://docs.newlead.ai/sync-leads-to-crm.md)
>
> Keep a CRM up to date from lead events.


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