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

# Transfer callers on your own Plivo account

> An inbound receptionist on your own Plivo number: the agent answers and finds the right team, your backend starts a Plivo transfer, and Mirai releases the call without hanging up. Billing stops at the release.

The agent answers your number, works out who the caller needs, and passes the
call to a person on your team. On your own Plivo account you can make that
handover yourself: Plivo transfers the call, and Mirai leaves it.

Once Mirai releases the call, the agent stops speaking and listening, recording
and transcription stop, and Mirai's billing ends. The caller stays on the line,
and the rest of the call is between your Plivo account and your team.

| | |
| :- | :- |
| Costs | The agent's minutes at your plan's rate, up to the moment you release the call. Plivo bills the line and the transfer on your account. |
| Runs in | Phone numbers on [your own Plivo account](https://docs.miraiminds.co/cookbooks/inbound-calling.md) |
| You need | A Plivo number connected to Mirai with a published agent, and a server that can call the Plivo API |

> [!NOTE]
>
> This is for numbers on **your own Plivo account**. On a number rented through
> Mirai, or on a SIP trunk, use a [live human takeover](https://docs.miraiminds.co/v2/handoff.md): Mirai rings
> the person and joins them to the caller.

## What you'll build

* A webhook tool, `transfer_to_team`, that the agent calls once it knows which
  team the caller needs.
* A handler on your server that starts a Plivo transfer to that team and then
  releases the call from Mirai.

## How it works

1. A caller rings your Plivo number. Mirai's agent answers on an audio stream
   that Plivo keeps the call on.
2. The agent finds out what the caller needs and calls `transfer_to_team`.
3. Your server asks Plivo to transfer the call to your own XML, for example a
   `<Dial>` to the team's number. Plivo holds the transfer while Mirai's stream
   is still on the call.
4. Your server releases the call with `POST /v2/calls/{id}/release`. Mirai
   stops its stream and never hangs up. Plivo then runs your transfer.
5. The call ends in Mirai as `completed` with `ended_reason: "released"`. Your
   `call.completed` webhook and post-call analysis arrive as for any call.

## Step 1: Give the agent a transfer tool

Add a webhook tool that sends your server the call id, the caller's number,
your number and the team. The agent's prompt tells it when to call the tool,
for example once the caller has said which team and agreed to hold.

```json title="transfer-to-team.json" theme={"system"}
{
  "name": "transfer_to_team",
  "phase": "on_call",
  "execution": "sync",
  "description": "Pass the caller to a person in the right team.",
  "when": "Use this once the caller has said which team they need and agreed to be transferred.",
  "parameters": {
    "type": "object",
    "properties": {
      "department": { "type": "string", "enum": ["sales", "support", "accounts"] }
    },
    "required": ["department"]
  },
  "request": {
    "method": "POST",
    "url": "https://your-server.example.com/mirai/transfer",
    "body": {
      "call_id": "{{ call.id }}",
      "caller": "{{ vars.caller_number }}",
      "number": "{{ vars.called_number }}",
      "department": "{{ args.department }}"
    }
  },
  "timeout_ms": 8000
}
```

Save it on the agent with
`PUT /v2/agents/{agent_id}/tools/transfer_to_team`, as in [Tools](https://docs.miraiminds.co/v2/tools.md).

Have the agent tell the caller it is putting them through before it calls the
tool. Once the call is released, the agent says nothing more.

## Step 2: Start the Plivo transfer, then release

Your handler does two things, in this order.

**First, start the transfer on Plivo.** Find the live call on your Plivo
account with the two numbers, then transfer it to your XML:

```bash theme={"system"}
# The live call between the caller and your number
curl -u "$PLIVO_AUTH_ID:$PLIVO_AUTH_TOKEN" \
  "https://api.plivo.com/v1/Account/$PLIVO_AUTH_ID/Call/?status=live&from_number=919876543210&to_number=918031000000"

# Transfer it: Plivo runs this URL's XML once Mirai's stream ends
curl -X POST -u "$PLIVO_AUTH_ID:$PLIVO_AUTH_TOKEN" \
  "https://api.plivo.com/v1/Account/$PLIVO_AUTH_ID/Call/$CALL_UUID/" \
  -H "Content-Type: application/json" \
  -d '{ "legs": "aleg", "aleg_url": "https://your-server.example.com/plivo/sales.xml", "aleg_method": "GET" }'
```

The XML at `aleg_url` is yours, for example:

```xml theme={"system"}
<Response>
  <Dial>
    <Number>918031000001</Number>
  </Dial>
</Response>
```

**Then release the call from Mirai:**

```bash theme={"system"}
curl -X POST https://prod.voice.miraiminds.co/v2/calls/$CALL_ID/release \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY"
```

```json title="202 Accepted" theme={"system"}
{ "id": "call_01M8XF7AH8PRBSWDND9N6PDEZD", "status": "completed", "ended_reason": "released" }
```

Mirai stops its audio stream on the call and leaves it. Plivo runs your
transfer straight away. Answer the agent's tool call with a short result, such
as `{ "ok": true }`. The agent doesn't speak after the release.

> [!TIP]
>
> Releasing first and transferring a moment later also works. After Mirai's
> stream ends, the call waits 15 seconds before it hangs up, and a Plivo transfer
> in that time takes the caller. Transfer first if you can: the caller then hears
> no gap at all.

### Stopping the stream yourself

If your server already stops Mirai's audio stream with Plivo
(`DELETE /v1/Account/{auth_id}/Call/{call_uuid}/Stream/`) after starting the
transfer, Mirai doesn't hang up either. The call is recorded as released at
that moment, with billing ending there too. `POST /release` does the same thing
in one call and confirms it.

## Step 3: Read the result

The released call is an ordinary completed call:

* `status: "completed"`, `ended_reason: "released"`.
* `duration_secs` and the charge run from when the call was answered to the
  release.
* The transcript and recording cover the conversation with the agent, up to
  the release. The rest of the call isn't recorded by Mirai.
* `call.completed` fires, and post-call analysis runs on the agent's part of
  the conversation.

## Errors

| Status | `error.code` | What it means |
| :- | :- | :- |
| `400` | `release_unsupported` | The call isn't on your own Plivo account. Use a [live human takeover](https://docs.miraiminds.co/v2/handoff.md) or [abort](https://docs.miraiminds.co/v2/calls.md#abort-a-call). |
| `409` | `call_not_active` | The call hasn't been answered yet, has already ended, or is no longer live on Plivo. |
| `409` | `handoff_in_progress` | A Mirai live human takeover is under way on this call. Let it finish or cancel it first. |
| `503` | `release_unavailable` | Plivo didn't confirm that Mirai's stream stopped. The agent stays on the call. Retry the release. |

A release you repeat for a call that is already released answers the same
`202`.

## Privacy and compliance

* Tell callers they are being transferred, and that the call with the agent is
  recorded and transcribed, if your rules require it.
* After the release, Mirai holds no audio from the call. Recording the rest of
  it is up to your Plivo setup.

## Related

> [card] [Calls API](https://docs.miraiminds.co/v2/calls.md#release-a-call)
>
> Release, abort and read a call.

> [card] [Live human takeover](https://docs.miraiminds.co/v2/handoff.md)
>
> Hand a call to a person on any number: Mirai rings them and joins them in.

> [card] [Receive inbound calls](https://docs.miraiminds.co/cookbooks/inbound-calling.md)
>
> Connect your own Plivo number and assign an agent.

> [card] [Tools](https://docs.miraiminds.co/v2/tools.md)
>
> Webhook tools the agent calls during a call.


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