# Create or Update a Record

> Create or update one custom record by its key, and set the contact it belongs to

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

Creates a record, or updates it if a record with the same key already exists. Fields you send replace the stored values, fields you don't send are kept, and `null` clears a field. See [how updates work](https://docs.lemuai.com/api-reference/records/overview#how-updates-work).

**Endpoint:** `PUT /api/v1/objects/{api_name}/records/{key}`

## 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` |

## Path parameters

| Name | Type | Required | Description |
|---|---|---|---|
| `api_name` | string | Yes | The record type's API name, for example `subscription`. |
| `key` | string | Yes | The record's key, for example `SUB-1042`. If `data` also contains the key field, it must have the same value. |

## Request body

| Field | Type | Required | Description |
|---|---|---|---|
| `data` | object | No | The record's fields, as field name and value. Only fields defined on the record type are accepted. See [field values](https://docs.lemuai.com/api-reference/records/overview#field-values). |
| `phone` | string | No | The owner's phone number in international format. |
| `email` | string | No | The owner's email address. |
| `external_id` | string | No | Your own id for the owner. |
| `shopify_customer_id` | string | No | The owner's Shopify customer id. |
| `contact_id` | integer | No | The owner's Lemu AI contact id. If that contact was merged into another, the record goes to the contact it was merged into. |

## Choosing the owner

Records of a normal record type belong to one contact. A [catalog](https://docs.lemuai.com/records/catalog) has no owner, so these fields are ignored for it.

1. If you send `contact_id`, that contact is the owner.
2. Otherwise, if you send `phone`, `email`, `external_id` or `shopify_customer_id`, Lemu AI finds the contact the same way as [Send an event](https://docs.lemuai.com/api-reference/events/trigger#how-the-contact-is-found). A new contact (tagged `record`) is only created from a `phone` or `email`.
3. If you send no owner fields and the record already exists, it keeps its owner.
4. A new record with no owner fields is rejected.

Sending a different owner for an existing record moves the record to that contact.

**Example request:**

**cURL**

```bash
curl -X PUT https://app.lemuai.com/api/v1/objects/subscription/records/SUB-1042 \
  -H "X-API-Key: lemu_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "priya@example.com",
    "data": {
      "plan": "Gold",
      "status": "active",
      "renews_on": "2026-11-30",
      "monthly_amount": 499
    }
  }'
```

**JavaScript**

```js
const res = await fetch("https://app.lemuai.com/api/v1/objects/subscription/records/SUB-1042", {
  method: "PUT",
  headers: {
    "X-API-Key": process.env.LEMU_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "email": "priya@example.com",
    "data": {
      "plan": "Gold",
      "status": "active",
      "renews_on": "2026-11-30",
      "monthly_amount": 499
    }
  }),
});
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.put(
    "https://app.lemuai.com/api/v1/objects/subscription/records/SUB-1042",
    headers={
        "X-API-Key": os.environ["LEMU_API_KEY"],
    },
    json={
        "email": "priya@example.com",
        "data": {
            "plan": "Gold",
            "status": "active",
            "renews_on": "2026-11-30",
            "monthly_amount": 499,
        },
    },
    timeout=30,
)
response.raise_for_status()
print(response.json())
```

## Response

**`200`**
```json
{
  "status": "created",
  "record": {
    "id": 18342,
    "key": "SUB-1042",
    "contact_id": 901,
    "data": {
      "subscription_id": "SUB-1042",
      "plan": "Gold",
      "status": "active",
      "renews_on": "2026-11-30",
      "monthly_amount": 499
    },
    "created_at": "2026-10-27T09:30:12Z",
    "updated_at": "2026-10-27T09:30:12Z",
    "deleted_at": null
  }
}
```

| Field | Type | Description |
|---|---|---|
| `status` | string | `created` (new record, or a deleted one brought back), `updated` (a field or the owner changed) or `unchanged` (nothing changed). |
| `record.id` | integer | The record's id in Lemu AI. |
| `record.key` | string | The record's key. |
| `record.contact_id` | integer or null | The owner. `null` for catalog records. |
| `record.data` | object | All of the record's fields after the update, including the key field. |
| `record.created_at`, `record.updated_at` | string | Timestamps, in ISO 8601 (UTC). |
| `record.deleted_at` | string or null | When the record was deleted. Always `null` here. |

## Errors

| Status | When |
|---|---|
| `400` | There's no record type with this `api_name`, the record type is archived, the record has no owner, no contact matches the owner, a new owner contact couldn't be created because your plan's contact limit is reached, a field is unknown or has a value of the wrong type, a required field is missing, the key in `data` doesn't match the path, or the record is larger than 16 KB. `detail` lists every problem. |
| `401` | The API key is missing or not valid. |
| `402` | Creating the record would take your organization over its plan's record limit. |
| `422` | The body isn't valid JSON, or an owner field is too long. |

See [Errors](https://docs.lemuai.com/api-reference/errors) for the error format.
