# Contact labels API

> Label customers from your own systems and read the labels back.

Label your customers from your own systems, and read the labels back. A
**label** (also called a tag) is a short piece of text on a contact, such as
`VIP` or `primary customer`. The same labels are shown and edited in the
**Customer details** panel next to every chat in the Team Inbox.

Labels are separate from the contact attributes that flows use
(`$contact.<name>`): labelling a customer never changes flow data.

## Authentication

Use an API key with the **whatsapp** scope, sent as a bearer token. Create one
on the API Keys page of the app. The organization is taken from the key; you
never send it.

```
Authorization: Bearer pk_live_xxxxxxxxxxxxxxxx
```

Base URL: `https://api.pingx.ai`

## Add or remove labels

`POST /whatsapp/v1/contacts:setTags`

| Field | Type | Notes |
|---|---|---|
| `phone` | string, required | Any format; contacts are matched on the **digits** (8-15), so `+91 98111 11111` and `919811111111` are the same contact. |
| `add` | array of strings | Labels to add. |
| `remove` | array of strings | Labels to remove. A label cannot be in both `add` and `remove`. |
| `name` | string, optional | The contact's name. Only applied when non-empty. |

At least one of `add`, `remove` or `name` is required.

```bash
curl -X POST https://api.pingx.ai/whatsapp/v1/contacts:setTags \
  -H "Authorization: Bearer $PINGX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "phone": "919811111111",
    "name": "Asha Rao",
    "add": ["VIP", "primary customer"],
    "remove": ["trial"]
  }'
```

Response (`created` is true when the number was new and the contact was made;
`tags` is the contact's full list after the change):

```json
{
  "success": true,
  "message": "contact updated",
  "created": false,
  "tags": ["primary customer", "VIP"],
  "contact": { "id": "9f1c0c52-...", "name": "Asha Rao", "phone": "919811111111" }
}
```

How it behaves:

* **Safe to repeat.** Adding a label the contact already has, or removing one
  it does not have, does nothing. Concurrent calls (several of your systems,
  plus agents labelling in the inbox) never undo each other.
* **Case does not matter.** `vip` and `VIP` are the same label; the spelling
  used first is kept. Extra spaces are collapsed (`"big   spender"` becomes
  `"big spender"`).
* **Limits.** A label is up to 40 characters; a contact has at most 50 labels.
  A change that would exceed that is rejected and nothing is written.
* **It creates the contact** when the number is new.

## Read a contact's labels

`GET /whatsapp/v1/contacts:byPhone?phone=919811111111`

```bash
curl "https://api.pingx.ai/whatsapp/v1/contacts:byPhone?phone=919811111111" \
  -H "Authorization: Bearer $PINGX_API_KEY"
```

```json
{
  "found": true,
  "contact": { "id": "9f1c0c52-...", "name": "Asha Rao", "phone": "919811111111" },
  "tags": ["primary customer", "VIP"],
  "knownTags": ["Gold", "primary customer", "Trial", "VIP"]
}
```

`knownTags` is every label your organization uses, for pickers. `found` is
`false` (and `contact` absent) for an unknown number; that is a normal answer,
not an error.

## Errors

| HTTP | When |
|---|---|
| 400 | `phone` missing or not 8-15 digits; a blank, over-long or control-character label; a label in both `add` and `remove`; nothing to change; more than 50 labels on the contact. The `message` says which. |
| 401 | Missing, wrong or revoked API key, or a key without the `whatsapp` scope. |

Inbox agents can read and label the contact of the chat they have open (the
customer panel); they cannot use the rest of the contacts API.

