# Devices

Source: https://docs.qvoice.net/api/devices/

Create a user's devices and update them over the QVOICE Platform REST API, including **SIP URI**
devices that send the user's calls to a SIP address on another platform.

---

## Authentication

Every request needs an access token and the account it acts on. Setting `sip.route` and the update
endpoint require an **account admin** user:

| Header | Value |
| --- | --- |
| `Authorization` | `Bearer <access_token>` from `POST https://{portalURL}:9443/api/v2/login` |
| `X-Account-ID` | ID of the account the user and device belong to |
| `Content-Type` | `application/json` |

---

## Create a device for a user

`POST https://{portalURL}:9443/api/v2/config/users/{user_id}/devices`

The device is created owned by `{user_id}`. SIP credentials (username, password, realm) are always
generated by QVOICE Platform; they cannot be sent in the request.

### Request body

| Field | Required | Description |
| --- | --- | --- |
| `name` | Yes | Device name. |
| `device_type` | No | `sip_device` (default), `softphone`, `cellphone`, `smartphone`, `landline`, `fax`, `sip_uri`, `ata`, `mobile` or `application`. Any other value falls back to `sip_device`. |
| `media.audio.codecs` | No | Audio codecs, e.g. `["PCMA", "PCMU"]`. Default: `["PCMU", "PCMA"]`. |
| `media.webrtc` | No | `true` for a WebRTC device. Default: `false`. |
| `sip.invite_format` | No | How calls to the device are addressed: `contact` (default — the device's registration), `username`, `npan`, `1npan`, `e164` or `route`. |
| `sip.route` | With `route` | SIP URI the call is sent to (`sip:` or `sips:`, e.g. `sip:user@host`). Required when `sip.invite_format` is `route`. Sending a `sip.route` without `sip.invite_format` sets the format to `route`. Account admins only. |

### Example: SIP URI device

```
curl -X POST "https://{portalURL}:9443/api/v2/config/users/{user_id}/devices" \
  -H "Authorization: Bearer $TOKEN" \
  -H "X-Account-ID: $ACCOUNT_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "external-app",
    "device_type": "sip_uri",
    "sip": {
      "invite_format": "route",
      "route": "sip:27870000000@sipgw.example.com"
    },
    "media": { "audio": { "codecs": ["PCMA", "PCMU"] } }
  }'
```

### Responses

| Code | Meaning |
| --- | --- |
| `201` | Created. Body: `{"id": "<device_id>"}`. |
| `400` | The `sip` block is invalid. The body names the field, e.g. `{"sip.route": {"required": {"message": "Field is required when sip.invite_format is \"route\""}}}`. |
| `403` | A user who is not an account admin sent `sip.route`. |
| `500` | The device could not be created. |

**Bulk creation:**

`POST /api/v2/config/users/{user_id}/devices/bulk` takes an array of the same objects and
answers `201` when all were created, or `207` with `{"ids": [...], "errors": [...]}` when some
failed. The bulk endpoint does not support the `sip` block: it is ignored and every device is
created with `invite_format` `contact`. Create SIP URI devices one at a time.

---

## Update a device

`PATCH https://{portalURL}:9443/api/v2/config/devices/{device_id}`

Account admins only. Changes how calls to the device are addressed; everything else on the device,
including its SIP credentials, is kept.

### Request body

| Field | Description |
| --- | --- |
| `sip.invite_format` | Same values as on creation. Setting `route` needs a route, either in this request or already on the device. |
| `sip.route` | SIP URI (`sip:` or `sips:`). Only valid with `sip.invite_format` `route`; sending it alone sets the format to `route`. |

Only these fields can be updated through this endpoint. A request with any other field is rejected
with `400` rather than partially applied.

### Example: point an existing device at a SIP URI

```
curl -X PATCH "https://{portalURL}:9443/api/v2/config/devices/{device_id}" \
  -H "Authorization: Bearer $TOKEN" \
  -H "X-Account-ID: $ACCOUNT_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "sip": {
      "invite_format": "route",
      "route": "sip:27870000000@sipgw.example.com"
    }
  }'
```

### Responses

| Code | Meaning |
| --- | --- |
| `200` | Updated. Body: the device as stored. |
| `400` | Unknown field, missing `sip`, or invalid `sip` values; the body says which. |
| `401` | The user is not an account admin. |
| `404` | The device does not exist in the account given by `X-Account-ID`. |

---

## Read and delete

| Method | Endpoint | Description |
| --- | --- | --- |
| `GET` | `/api/v2/config/devices/{device_id}` | One device. |
| `GET` | `/api/v2/config/devices` | Every device of the account, keyed by device ID. Add `?include_docs=false` for the summary only. |
| `DELETE` | `/api/v2/config/devices/{device_id}` | Removes the device. |
