> ## Documentation Index
> Fetch the complete documentation index at: https://www.bolna.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Receive inbound calls on a SIP trunk

> Assign Bolna agents to your SIP trunk numbers, set a default agent, and use the caller details each inbound call puts in recipient_data.

Once your carrier sends calls to Bolna, each number on the trunk needs an agent to answer it. This page covers assigning agents and what an agent knows about each caller.

Before you start, [connect your carrier](/docs/sip-trunking/connect-carrier): the trunk needs your carrier's IPs and at least one number.

## How an inbound call is routed

1. Your carrier sends the INVITE to `trunk.bolna.ai`. Bolna accepts it only from an address on the carrier IPs of an active trunk. Any other source gets `403 Forbidden`.
2. Bolna matches the called number, the user part of the Request-URI, against that trunk's numbers. It matches the E.164 number with or without the `+`, or the number's destination key.
3. The call goes to the number's agent. A number without its own agent goes to the trunk's default agent. If there is neither, the call is refused with a `404` response.
4. The agent's inbound rules then apply, such as [blocked callers](/docs/customizations/block-incoming-callers) and inbound call limits, followed by your account balance and the trunk's channel limit.

Any agent in the same organization or sub-account as the trunk can answer its numbers. The agent's **Telephony Provider** setting does not affect inbound calls.

## Assign an agent to a number

You can assign agents from the trunk or from the agent.

<Steps>
  <Step title="From the trunk's Numbers tab">
    Open **SIP Trunks**, open the trunk and go to the **Numbers** tab. In the **Agent** column, pick an agent for each number. A number with no agent, on a trunk with no default agent, is marked **Unrouted**, and calls to it are rejected.

    <Frame caption="Assigning agents on the Numbers tab">
      <img src="https://mintcdn.com/bolna-54a2d4fe/7dEowS9LSMkFi965/images/sip-trunks/numbers-tab-assign-agent.png?fit=max&auto=format&n=7dEowS9LSMkFi965&q=85&s=45f160d45f7666472468a1a4ec379397" alt="Trunk Numbers tab with a Default agent selector above a table of numbers, one row's Agent dropdown open listing agents, and another row marked Unrouted" width="2496" height="862" data-path="images/sip-trunks/numbers-tab-assign-agent.png" />
    </Frame>
  </Step>

  <Step title="Or set a default agent">
    On the same tab, **Default agent** answers every number that has no agent of its own. Leave it at **None (optional)** to reject calls to unassigned numbers instead.
  </Step>

  <Step title="Or from the agent">
    Open the agent, go to the **Calling** tab and, under **Inbound Calling**, pick the trunk number as the **Inbound number**.

    <Frame caption="Linking a trunk number from the agent">
      <img src="https://mintcdn.com/bolna-54a2d4fe/7dEowS9LSMkFi965/images/sip-trunks/agent-inbound-number.png?fit=max&auto=format&n=7dEowS9LSMkFi965&q=85&s=44651271fa78849a6a35f5c88e04401b" alt="Agent Calling tab, Inbound Calling section, with a SIP trunk number selected as the Inbound number" width="1828" height="300" data-path="images/sip-trunks/agent-inbound-number.png" />
    </Frame>
  </Step>
</Steps>

<Note>
  Linking a number from the agent's **Calling** tab makes it that agent's only inbound number; the agent's other numbers are unlinked. On the trunk's **Numbers** tab, or through the API, several numbers can point at the same agent.
</Note>

To check the setup, call the number from any phone. The call appears on the trunk's **Activity** tab under **Recent calls** and in Call History.

## What the agent knows about the caller

Every inbound trunk call fills these variables in `recipient_data`, so you can use them in the prompt as `{from_number}` and so on:

| Variable | Value |
| - | - |
| `from_number`, `user_number` | The caller's number, as the user part of the INVITE's `From` header |
| `to_number`, `agent_number` | The called number, as the carrier sent it in the Request-URI, without a leading `+` |
| `call_sid` | The `Call-ID` of the carrier's INVITE |
| `agent_id` | The agent answering the call |
| `timezone` | The call's time zone |

Two more sources add to `recipient_data`:

* **Carrier headers.** Bolna passes the `P-Asserted-Identity` and `Diversion` headers from the INVITE to the agent. Map either one to a variable on the agent to use it; see [SIP headers](/docs/sip-trunking/sip-headers#inbound-header-variables).
* **Your caller data.** With [Identify incoming callers](/docs/customizations/identify-incoming-callers) set up, Bolna looks up the caller in your CSV, API or Google Sheet and adds the matching row. The lookup uses `from_number`, or the value of a mapped header if you set an ingest lookup key. When a header variable and a looked-up value have the same name, the looked-up value wins.

<Tip>
  Some carriers put the real caller ID in `P-Asserted-Identity` and a trunk or pilot number in `From`. If `from_number` is not the caller you expect, map `P-Asserted-Identity` to a variable and compare.
</Tip>

## Using the API

Assign an agent to a number with [assign agent](/docs/api-reference/trunks/assign_agent), using the number's `id` from [get trunk](/docs/api-reference/trunks/get). Send `"agent_id": null` to unassign it. To set the trunk's default agent, [update the trunk](/docs/api-reference/trunks/update) with `default_inbound_agent_id`.

<CodeGroup>
  ```bash Assign an agent theme={"system"}
  curl -X PATCH https://api.bolna.ai/trunk/numbers/<number_id> \
    -H "Authorization: Bearer <api_key>" \
    -H "Content-Type: application/json" \
    -d '{ "agent_id": "<agent_id>" }'
  ```

  ```bash Set the default agent theme={"system"}
  curl -X PATCH https://api.bolna.ai/trunk/trunks/<trunk_id> \
    -H "Authorization: Bearer <api_key>" \
    -H "Content-Type: application/json" \
    -d '{ "default_inbound_agent_id": "<agent_id>" }'
  ```

  ```bash Add a number with its agent theme={"system"}
  curl -X POST https://api.bolna.ai/trunk/trunks/<trunk_id>/numbers \
    -H "Authorization: Bearer <api_key>" \
    -H "Content-Type: application/json" \
    -d '{
      "e164": "+918045678901",
      "destination_key": "918045678901",
      "agent_id": "<agent_id>"
    }'
  ```

  ```bash List recent inbound calls theme={"system"}
  curl "https://api.bolna.ai/trunk/trunks/<trunk_id>/calls?direction=inbound&limit=20" \
    -H "Authorization: Bearer <api_key>"
  ```
</CodeGroup>

An agent outside the trunk's organization or sub-account is refused with `400` and `agent not in tenant`. To stop a trunk taking inbound calls without deleting it, update it with `"inbound_enabled": false`; calls to its numbers are then refused.

[List calls](/docs/api-reference/trunks/list_calls) returns each call's `execution_id`, `direction`, the far party's `number`, `duration_s`, `outcome`, and the `sip_code` and `sip_cause` it ended with. `direction` is `all`, `inbound`, `outbound` or `failed`, and `limit` is at most 100.

## Next steps

<CardGroup cols={2}>
  <Card title="SIP headers" icon="tag" href="/docs/sip-trunking/sip-headers">
    Map P-Asserted-Identity and Diversion to prompt variables.
  </Card>

  <Card title="Troubleshooting" icon="stethoscope" href="/docs/sip-trunking/troubleshooting">
    Calls refused with 403, 404 or 486.
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.