# mv.phoneNumbers

> The phoneNumbers methods of @morevoice/sdk: Phone numbers on your account.

Phone numbers on your account. These methods are on `mv.phoneNumbers`, where `mv` is your client (see [the Node.js SDK](https://docs.morevoice.ai/sdk/node/#connect)). Each one returns the response object and throws when the API answers with an error.

## `create()`

**Buy a number, or create a sandbox number (test mode).** With a live key: buys `number` (or `available_id`), one of `GET /v1/phone_numbers/available`, and routes it to `assistant_id`, `flow_id` or the target of `inbound_route_id` (none: your most recently updated assistant). It is billed monthly at the listed price from today. Errors: 409 `number_unavailable` (taken meanwhile: pick another), 402 `balance_exhausted` (a prepaid balance that doesn't cover the first month), 402 `plan_limit` (your plan's number limit), 403 `feature_unavailable` (number purchases are not enabled for the organisation, or the plan lacks them). `status` is `active`, or `pending` while the line is being set up. Send an `Idempotency-Key`: a retry with the same key never buys twice.

With a test key: allocates a sandbox number (`source: sandbox`) in the +972 50 999 range, at no cost and with no carrier, at most 5 per organisation. Dial it with `POST /v1/test_helpers/inbound_calls`. A sandbox number never receives real calls.

```ts
mv.phoneNumbers.create(body?: PhoneNumbersCreateData["body"], options?: RequestOptions): Promise<PhoneNumbersCreateResponse>
```

`POST /phone_numbers` · [API reference](https://docs.morevoice.ai/api/operations/phone_numbers_create/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `body.assistant_id` | `string` | no | Inbound calls to the number reach this assistant. |
| `body.available_id` | `string` | no | Live keys: the `id` of a number from GET /v1/phone_numbers/available (`avn_…`). Send this or `number`. |
| `body.flow_id` | `string` | no | Inbound calls to the number run this published flow. |
| `body.inbound_route_id` | `string` | no | Test keys: inbound test calls to the number follow this inbound route. Live keys: the number gets its own route with this route's target (its flow, queue, person or assistant). |
| `body.label` | `string` | no | A name for the number, shown in lists. |
| `body.number` | `string` | no | Live keys: the number to buy, in E.164 (one of GET /v1/phone_numbers/available). Send this or `available_id`. |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options.headers["Idempotency-Key"]` | `string` | no | A unique key (for example a UUID) that makes this request safe to retry: for 24 hours, a retry with the same key and parameters returns the first response instead of acting twice. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

`PhoneNumber`:

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | The number's ID (stable). |
| `object` | `"phone_number"` | Always `phone_number`. |
| `livemode` | `boolean` | `true` in live mode, `false` in test mode. |
| `e164` | `string` | The number in E.164. |
| `country` | `string \| null` | ISO 3166-1 alpha-2, when known. |
| `source` | `"provisioned" \| "connection" \| "sandbox"` | `provisioned`: bought through the platform. `connection`: a number of your own SIP connection (its caller IDs and the numbers your inbound routes match). `sandbox`: a test-mode number. |
| `provider` | `string \| null` | The carrier of a provisioned number; null for your own connections. |
| `status` | `string` | `active`; provisioned numbers can also be `pending`, `pending_kyc`, `porting_in`, `suspended`, `releasing` or `released`. |
| `connection_id` | `string \| null` | `conn_…` ID. |
| `inbound_route_id` | `string \| null` | `rte_…` ID. |
| `label` | `string \| null` | — |
| `inbound` | `boolean` | Calls to the number are answered here (a route or connection matches it). |
| `outbound_caller_id` | `boolean` | The number can be presented as caller ID on outbound calls. |
| `created` | `string \| null` | When the number was added (null for numbers derived from a connection). |

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

const phoneNumber = await mv.phoneNumbers.create({
	assistant_id: "asst_4Gk2LmN9pQ4rS6tV8wX0yZ",
	label: "Support line (test)",
});
console.log(phoneNumber);
```

## `delete()`

**Release a phone number.** With a live key: releases one of your numbers (when number releases are enabled for the organisation; otherwise 501). The number stops answering calls: its inbound route goes with it. A number bought through the platform goes back to the platform (a MoreVoice Telephony number returns to the stock) and its monthly fee stops: no further month is billed, and the month already billed is not refunded. A number of your own SIP connection is removed from the connection's caller IDs and its exact inbound routes are deleted. Fires `number.released`.

While campaigns present the number as caller ID, caller-ID pools hold it, desk phones present it or other inbound routes match it, the answer is 409 `number_in_use` with `details.uses`; `force=true` detaches it from all of them first (campaigns and phones fall back to the line's default caller ID). Releasing a released number again answers 200. Releasing cannot be undone: the number may go to another customer.

With a test key: releases a sandbox number.

```ts
mv.phoneNumbers.delete(id: PhoneNumbersDeleteData["path"]["id"], query?: NonNullable<PhoneNumbersDeleteData["query"]>, options?: RequestOptions): Promise<PhoneNumbersDeleteResponse>
```

`DELETE /phone_numbers/{id}` · [API reference](https://docs.morevoice.ai/api/operations/phone_numbers_delete/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | yes | A phone number ID (`pn_…`). |
| `query.force` | `string` | no | Live keys: `true` releases the number even while campaigns, caller-ID pools, desk phones or other inbound routes use it, and detaches it from them. Default `false` (409 `number_in_use` with `details.uses`). |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options.headers["Idempotency-Key"]` | `string` | no | A unique key (for example a UUID) that makes this request safe to retry: for 24 hours, a retry with the same key and parameters returns the first response instead of acting twice. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

`DeletedPhoneNumber`:

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | The released number's ID. |
| `object` | `"phone_number"` | Always `phone_number`. |
| `deleted` | `true` | — |

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

const deletedPhoneNumber = await mv.phoneNumbers.delete("pn_7Hk2Lm9Qp");
console.log(deletedPhoneNumber);
```

## `list()`

**List phone numbers.** Numbers bought through the platform, then the numbers of your own SIP connections (their caller IDs and the numbers your inbound routes match exactly). Test-mode keys list sandbox numbers.

```ts
mv.phoneNumbers.list(query?: NonNullable<PhoneNumbersListData["query"]>, options?: RequestOptions): PagedList<PhoneNumbersListResponse["data"][number], NonNullable<PhoneNumbersListData["query"]>>
```

`GET /phone_numbers` · [API reference](https://docs.morevoice.ai/api/operations/phone_numbers_list/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `query.limit` | `integer` | no | How many objects to return, 1–100 (default 20). |
| `query.starting_after` | `string` | no | A cursor (`next_cursor`) or object ID: return the objects after it (older). |
| `query.ending_before` | `string` | no | A cursor or object ID: return the objects before it (newer). |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

A [`PagedList`](https://docs.morevoice.ai/sdk/typescript/pagination/): `await` it for the first page, `for await` it for every item.

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

for await (const phoneNumber of mv.phoneNumbers.list()) {
	console.log(phoneNumber);
}
```

## `listAvailable()`

**List numbers you can buy.** The numbers on offer to your organisation, in number order, with your monthly price (and a setup fee, if any). Buy one with `POST /v1/phone_numbers` and its `id` as `available_id` (or its `number`). Needs a live key, and number purchases enabled for the organisation (otherwise 403 `feature_unavailable`). An empty list means nothing matches right now.

```ts
mv.phoneNumbers.listAvailable(query?: NonNullable<PhoneNumbersListAvailableData["query"]>, options?: RequestOptions): PagedList<PhoneNumbersListAvailableResponse["data"][number], NonNullable<PhoneNumbersListAvailableData["query"]>>
```

`GET /phone_numbers/available` · [API reference](https://docs.morevoice.ai/api/operations/phone_numbers_list_available/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `query.limit` | `integer` | no | How many objects to return, 1–100 (default 20). |
| `query.starting_after` | `string` | no | A cursor (`next_cursor`) or object ID: return the objects after it (older). |
| `query.ending_before` | `string` | no | A cursor or object ID: return the objects before it (newer). |
| `query.country` | `string` | no | ISO 3166-1 alpha-2, e.g. `IL`. |
| `query.area_code` | `string` | no | Only numbers with this area code (`073`, `03`; the leading 0 is optional). |
| `query.contains` | `string` | no | Only numbers whose digits contain this sequence. |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

A [`PagedList`](https://docs.morevoice.ai/sdk/typescript/pagination/): `await` it for the first page, `for await` it for every item.

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

for await (const phoneNumber of mv.phoneNumbers.listAvailable()) {
	console.log(phoneNumber);
}
```

## `retrieve()`

**Retrieve a phone number.** Returns a phone number: its E.164 form, where it comes from (`source`: a number of one of your SIP connections, a number provisioned through the platform, or a test-mode sandbox number), whether it answers inbound calls (and the route that answers them) and whether it can be the caller ID of outbound calls.

```ts
mv.phoneNumbers.retrieve(id: PhoneNumbersRetrieveData["path"]["id"], options?: RequestOptions): Promise<PhoneNumbersRetrieveResponse>
```

`GET /phone_numbers/{id}` · [API reference](https://docs.morevoice.ai/api/operations/phone_numbers_retrieve/)

### Parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | yes | A phone number ID (`pn_…`). |
| `options.headers["MoreVoice-Version"]` | `string` | no | The API version to use for this request. Defaults to the version the API key is pinned to. |
| `options` | `RequestOptions` | no | `idempotencyKey`, extra `headers` and an abort `signal`: see [retries and idempotency](https://docs.morevoice.ai/sdk/typescript/retries/). |

### Returns

`PhoneNumber`:

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | The number's ID (stable). |
| `object` | `"phone_number"` | Always `phone_number`. |
| `livemode` | `boolean` | `true` in live mode, `false` in test mode. |
| `e164` | `string` | The number in E.164. |
| `country` | `string \| null` | ISO 3166-1 alpha-2, when known. |
| `source` | `"provisioned" \| "connection" \| "sandbox"` | `provisioned`: bought through the platform. `connection`: a number of your own SIP connection (its caller IDs and the numbers your inbound routes match). `sandbox`: a test-mode number. |
| `provider` | `string \| null` | The carrier of a provisioned number; null for your own connections. |
| `status` | `string` | `active`; provisioned numbers can also be `pending`, `pending_kyc`, `porting_in`, `suspended`, `releasing` or `released`. |
| `connection_id` | `string \| null` | `conn_…` ID. |
| `inbound_route_id` | `string \| null` | `rte_…` ID. |
| `label` | `string \| null` | — |
| `inbound` | `boolean` | Calls to the number are answered here (a route or connection matches it). |
| `outbound_caller_id` | `boolean` | The number can be presented as caller ID on outbound calls. |
| `created` | `string \| null` | When the number was added (null for numbers derived from a connection). |

### Example

```ts
import MoreVoice from "@morevoice/sdk";

const mv = new MoreVoice(); // MOREVOICE_API_KEY from the environment

const phoneNumber = await mv.phoneNumbers.retrieve("pn_7Hk2Lm9Qp");
console.log(phoneNumber);
```
