# Use several numbers with the API

> Choose which WhatsApp number sends a message and send incoming messages to the right system.

Source: https://docs.soosh.io/guides/api/use-several-numbers/

If your workspace has a sales number and a support number, use the number's
ID to keep each message on the right number. This guide replies to a support
message from the same number the customer wrote to.

## Before you start

- Two or more [WhatsApp numbers connected](https://docs.soosh.io/guides/workspace/connect-a-whatsapp-number/)
- [An API key](https://docs.soosh.io/guides/integrations/create-an-api-key/) with `numbers:read`, `messages:read` and `messages:send`
- [A webhook endpoint](https://docs.soosh.io/guides/integrations/set-up-a-webhook/) subscribed to **Message received**

The examples use `SOOSH_API_KEY` for your key. Replace the sample IDs with
the IDs from your workspace.

## 1. Find your numbers

```sh
curl https://app.soosh.io/v1/numbers \
  -H "Authorization: Bearer $SOOSH_API_KEY"
```

Find your support number in `data` and save its `id`. For example:

```json
{
  "id": "0f6d3c1e-5b7a-4c52-9a55-3f6a2d5c8b10",
  "name": "Support",
  "phone": "+919876543210",
  "status": "active",
  "is_default": false
}
```

Some fields are left out here. Use `id` in your code: it stays the same if
your team renames the number.

## 2. Route an incoming message

Your webhook receives its subscribed events from every number in the
workspace. Creating a second endpoint does not split sales and support:
both endpoints receive both numbers' events if they subscribe to them.

After [checking the signature](https://docs.soosh.io/guides/api/verify-webhooks/), read
`workspace_id` and `data.number_id`. For a `message.received` event, keep
`data.id` and `data.contact_id` too. For example, these fields identify a
message on Support:

```json
{
  "type": "message.received",
  "workspace_id": "6f7e8d9c-1b2a-4c3d-8e5f-6a7b8c9d0e1f",
  "data": {
    "id": "8c2e4f6a-1d3b-4a5c-9e7f-2b4d6f8a0c1e",
    "contact_id": "4ea0d485-435d-407f-837a-c7d67095f23a",
    "number_id": "0f6d3c1e-5b7a-4c52-9a55-3f6a2d5c8b10"
  }
}
```

Other event fields are left out here. Send messages with Support's
`number_id` to your support system, and messages with Sales' ID to your
sales system. Apply the same routing to `message.sent` and `message.status`.

## 3. Reply from Support

Copy the incoming message's `contact_id` and `number_id` into your send.
To quote that message, use its `id` as `reply_to_message_id`:

```sh
curl https://app.soosh.io/v1/messages \
  -H "Authorization: Bearer $SOOSH_API_KEY" \
  -H "Idempotency-Key: support-reply-order-1042" \
  -H "Content-Type: application/json" \
  -d '{
    "contact_id": "4ea0d485-435d-407f-837a-c7d67095f23a",
    "number_id": "0f6d3c1e-5b7a-4c52-9a55-3f6a2d5c8b10",
    "reply_to_message_id": "8c2e4f6a-1d3b-4a5c-9e7f-2b4d6f8a0c1e",
    "type": "text",
    "text": { "body": "Your replacement is on its way." }
  }'
```

Use a new idempotency key for each new reply, and reuse it only when retrying
that same reply. The quoted message must belong to this contact and sending
number. A quote from Sales cannot be sent from Support.

The customer must have written to Support in the last 24 hours. Writing to
Sales does not open Support's window. If the window has closed,
[send an approved template](https://docs.soosh.io/guides/api/send-a-template/) belonging to Support.

Keep `number_id` on later sends too. If you leave it out, Soosh chooses the
template's number, then the contact's number, then a workspace fallback.
See [sending-number selection](https://docs.soosh.io/reference/api/messages/send-message/#which-number-it-s-sent-from)
for the exact rules.

## 4. Read just Support's messages

Combine the contact and number filters:

```sh
curl "https://app.soosh.io/v1/messages?contact_id=4ea0d485-435d-407f-837a-c7d67095f23a&number_id=0f6d3c1e-5b7a-4c52-9a55-3f6a2d5c8b10" \
  -H "Authorization: Bearer $SOOSH_API_KEY"
```

Without `number_id`, the contact's messages from all your workspace numbers
are included. Renaming a number keeps its history together. Removing it
keeps its historical messages and number ID, but you cannot send from it.

## If a reply is refused

| Response | What to do |
|---|---|
| `invalid_field` on `reply_to_message_id` | Quote a message from the same contact and sending number, or leave out the quote. |
| `window_closed` | Send an approved template from that number. A system notice or reaction does not open the reply window. |
| `number_unavailable` | Reconnect the sending number. To use another number, choose its template and check its own reply window. |
| `invalid_field` on `number_id` | List your numbers again. A removed number can appear in history but cannot send. |

## Result

The reply goes from Support, and your support system follows that number's
messages. The customer still has one contact record in Soosh: assignment and
closing or reopening the chat apply across their numbers.

API keys also apply across the workspace. Creating a separate key for each
system does not restrict either key to one WhatsApp number.

## Related

- [Work with several numbers in the inbox](https://docs.soosh.io/guides/inbox/work-with-several-numbers/)
- [Send a template](https://docs.soosh.io/guides/api/send-a-template/)
- [Webhook delivery](https://docs.soosh.io/reference/webhook-delivery/)
- [Messages API](https://docs.soosh.io/reference/api/messages/)
