# Bulk Upsert Contacts

> Create or update up to 5,000 contacts in one contact list in a single request

URL: https://docs.lemuai.com/api-reference/contacts/bulk-upsert

Loads or refreshes a contact list in one request. For each contact:

1. Lemu AI looks for an existing contact with the same `phone`, then with the same `external_id`. A row that has neither a phone nor an `external_id` is matched by `email` instead (ignoring upper and lower case), so sending the same email-only rows again updates them rather than creating duplicates.
2. If one is found, it is updated with the fields you send. If not, a new contact is created.
3. The contact is added to the list named `list_name`, which is created if it doesn't exist.

Each contact is handled on its own, so one bad row doesn't stop the rest. Rows that fail are reported in `errors`.

This endpoint matches rows by phone, `external_id` and email only. It does not merge contacts. To link activity from several identifiers (for example a website visitor id) to one person, use [Send an event](https://docs.lemuai.com/api-reference/events/trigger).

**Note:** Bulk matching compares phone numbers exactly as stored. Send every number in the same international format (for example `+919876543210`) so existing contacts are found instead of duplicated.

**Endpoint:** `POST /api/v1/contacts/bulk-upsert`

## Headers

| Name | Required | Description |
|---|---|---|
| `X-API-Key` | Yes | An API key from your organization. See [Authentication](https://docs.lemuai.com/api-reference/authentication). |
| `Content-Type` | Yes | `application/json` |

## Request body

| Field | Type | Required | Description |
|---|---|---|---|
| `list_name` | string | Yes | The list to put the contacts in (1 to 255 characters). Created if it doesn't exist. |
| `contacts` | array | Yes | Between 1 and 5,000 contacts. Each one accepts the fields below. |

**Each contact:**

| Field | Type | Description |
|---|---|---|
| `phone` | string | Phone number in international format. |
| `external_id` | string | Your own id for the person. |
| `email` | string | Email address. |
| `first_name`, `last_name` | string | Name. A single `name` (or `full_name`) is also accepted and split into first and last name. |
| `company` | string | Company name. |
| `title` | string | Job title. |
| `tags` | array of strings | Replaces the contact's tags. |
| `custom_fields` | object | Extra values, merged into the contact's custom fields. |
| *any other key* | any | Stored in `custom_fields`, so you can send a row from a spreadsheet or CRM as it is. A `mobile` key is used as `phone` and an `id` key as `external_id` when those aren't set. |

**Example request:**

**cURL**

```bash
curl -X POST https://app.lemuai.com/api/v1/contacts/bulk-upsert \
  -H "X-API-Key: lemu_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "list_name": "October renewals",
    "contacts": [
      { "phone": "+919876543210", "first_name": "Priya", "plan": "Gold" },
      { "phone": "+919812345678", "name": "Rahul Verma", "id": "crm-58214", "tags": ["renewal"] },
      { "phone": "+919876543210", "first_name": "Priya S." }
    ]
  }'
```

**JavaScript**

```js
const res = await fetch("https://app.lemuai.com/api/v1/contacts/bulk-upsert", {
  method: "POST",
  headers: {
    "X-API-Key": process.env.LEMU_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "list_name": "October renewals",
    "contacts": [
      {
        "phone": "+919876543210",
        "first_name": "Priya",
        "plan": "Gold"
      },
      {
        "phone": "+919812345678",
        "name": "Rahul Verma",
        "id": "crm-58214",
        "tags": [
          "renewal"
        ]
      },
      {
        "phone": "+919876543210",
        "first_name": "Priya S."
      }
    ]
  }),
});
if (!res.ok) throw new Error(`LemuAI API ${res.status}: ${await res.text()}`);
const data = await res.json();
console.log(data);
```

**Python**

```python
import os

import requests

response = requests.post(
    "https://app.lemuai.com/api/v1/contacts/bulk-upsert",
    headers={
        "X-API-Key": os.environ["LEMU_API_KEY"],
    },
    json={
        "list_name": "October renewals",
        "contacts": [
            {
                "phone": "+919876543210",
                "first_name": "Priya",
                "plan": "Gold",
            },
            {
                "phone": "+919812345678",
                "name": "Rahul Verma",
                "id": "crm-58214",
                "tags": [
                    "renewal",
                ],
            },
            {
                "phone": "+919876543210",
                "first_name": "Priya S.",
            },
        ],
    },
    timeout=30,
)
response.raise_for_status()
print(response.json())
```

## Response

**`200`**
```json
{
  "created": 1,
  "updated": 1,
  "failed": 1,
  "errors": [
    { "index": 2, "error": "Duplicate phone +919876543210 in batch, skipped" }
  ],
  "list_created": false,
  "list_id": 42,
  "contacts": [
    {
      "id": 901,
      "organization_id": 7,
      "external_id": "crm-58213",
      "phone": "+919876543210",
      "phone_last4": "3210",
      "phone_masked": false,
      "email": null,
      "first_name": "Priya",
      "last_name": "Sharma",
      "company": null,
      "title": null,
      "tags": ["renewal", "gold"],
      "custom_fields": { "plan": "Gold" },
      "created_at": "2026-10-20T10:00:00Z",
      "updated_at": "2026-10-24T09:20:03Z"
    }
  ]
}
```

| Field | Type | Description |
|---|---|---|
| `created` | integer | Contacts created. |
| `updated` | integer | Existing contacts updated. |
| `failed` | integer | Rows that were skipped. |
| `errors` | array | One entry per failed row: `index` (its position in your `contacts` array, starting at `0`) and `error` (why). A phone number, `external_id` or email (for email-only rows) repeated within the same request is skipped after its first appearance. |
| `list_created` | boolean | `true` if the list was created by this request. |
| `list_id` | integer | The id of the list. Use it with [List the contacts in one list](https://docs.lemuai.com/api-reference/contacts/list-members), or to pick the list as a journey or campaign audience. |
| `contacts` | array | The contacts that were created or updated. See [contact fields](https://docs.lemuai.com/api-reference/contacts/overview#contact-fields). |

## Errors

| Status | When |
|---|---|
| `401` | The API key is missing or not valid |
| `402` | The contacts would take your organization over its plan's contact limit |
| `422` | `list_name` is missing, `contacts` is empty or has more than 5,000 items, or a field is too long |
