# Upsert a Contact

> Create or update one contact by phone number and add it to a contact list

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

Adds one contact to a contact list, creating whatever doesn't exist yet:

- **The contact** is matched by `phone` in your organization. The match ignores formatting, so `+91 98765 43210` and `+919876543210` are the same number. If a match is found, the fields you send are updated; if not, a new contact is created.
- **The list** is matched by `list_name`. If no list has that name, it is created.

Use this endpoint when a record is created or changes in your CRM.

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

## Request body

| Field | Type | Required | Description |
|---|---|---|---|
| `list_name` | string | Yes | The contact list to add the contact to (1 to 255 characters). Created if it doesn't exist. |
| `phone` | string | One of `phone` or `external_id` | Phone number in international format. This is what existing contacts are matched on. |
| `external_id` | string | One of `phone` or `external_id` | Your own id for the person. Stored on the contact, but not used for matching: a request with only `external_id` always creates a new contact. |
| `email` | string | No | A valid email address. |
| `first_name` | string | No | Up to 100 characters. |
| `last_name` | string | No | Up to 100 characters. |
| `company` | string | No | Up to 255 characters. |
| `title` | string | No | Job title, up to 255 characters. |
| `tags` | array of strings | No | Replaces the contact's tags. |
| `custom_fields` | object | No | Extra values. Merged into the contact's existing custom fields: keys you send are updated, others are kept. |

When updating, fields you leave out (or send as `null`) keep their current value.

**Example request:**

**cURL**

```bash
curl -X POST https://app.lemuai.com/api/v1/contacts/upsert \
  -H "X-API-Key: lemu_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "list_name": "October renewals",
    "phone": "+919876543210",
    "external_id": "crm-58213",
    "first_name": "Priya",
    "last_name": "Sharma",
    "tags": ["renewal", "gold"],
    "custom_fields": { "plan": "Gold", "renewal_date": "2026-11-05" }
  }'
```

**JavaScript**

```js
const res = await fetch("https://app.lemuai.com/api/v1/contacts/upsert", {
  method: "POST",
  headers: {
    "X-API-Key": process.env.LEMU_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "list_name": "October renewals",
    "phone": "+919876543210",
    "external_id": "crm-58213",
    "first_name": "Priya",
    "last_name": "Sharma",
    "tags": [
      "renewal",
      "gold"
    ],
    "custom_fields": {
      "plan": "Gold",
      "renewal_date": "2026-11-05"
    }
  }),
});
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/upsert",
    headers={
        "X-API-Key": os.environ["LEMU_API_KEY"],
    },
    json={
        "list_name": "October renewals",
        "phone": "+919876543210",
        "external_id": "crm-58213",
        "first_name": "Priya",
        "last_name": "Sharma",
        "tags": [
            "renewal",
            "gold",
        ],
        "custom_fields": {
            "plan": "Gold",
            "renewal_date": "2026-11-05",
        },
    },
    timeout=30,
)
response.raise_for_status()
print(response.json())
```

## Response

**`200`**
```json
{
  "contact": {
    "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", "renewal_date": "2026-11-05" },
    "created_at": "2026-10-20T10:00:00Z",
    "updated_at": "2026-10-24T09:12:40Z"
  },
  "contact_list": {
    "id": 55,
    "organization_id": 7,
    "name": "October renewals",
    "description": null,
    "metadata": {},
    "column_schema": [],
    "created_at": "2026-10-24T09:12:40Z",
    "updated_at": "2026-10-24T09:12:40Z"
  },
  "contact_created": false,
  "list_created": true
}
```

| Field | Type | Description |
|---|---|---|
| `contact` | object | The contact after the update. See [contact fields](https://docs.lemuai.com/api-reference/contacts/overview#contact-fields). |
| `contact_list` | object | The list the contact was added to: `id`, `name`, `description`, and timestamps. |
| `contact_created` | boolean | `true` if a new contact was created, `false` if an existing one was updated. |
| `list_created` | boolean | `true` if the list was created by this request. |

## Errors

| Status | When |
|---|---|
| `402` | Your plan's contact limit is reached |
| `422` | Neither `phone` nor `external_id` was sent, `list_name` is missing, or a field is too long or not valid (for example `email`) |
