# Phone Book

The **Phone Book** object is supported by the MiRTA PBX OpenAPI endpoint. This object is tenant-scoped. Tenant API keys must include `tenant=CANISTRACCI`; writes require a writable API key.

## Object Summary

<table id="bkmrk-propertyvalueobjectp"><thead><tr><th>Property</th><th>Value</th></tr></thead><tbody><tr><td>Object</td><td>`phonebook`</td></tr><tr><td>Primary path</td><td>`/phonebooks`</td></tr><tr><td>ID field</td><td>`pb_id`</td></tr><tr><td>Label field</td><td>`pb_name`</td></tr><tr><td>Primary source table</td><td>`pb_phonebooks`</td></tr><tr><td>Required on create</td><td>`pb_name`</td></tr><tr><td>Path aliases</td><td>`/phonebook`, `/phonebooks`, `/phone_book`, `/phone_books`</td></tr></tbody></table>

## Endpoint Patterns

<table id="bkmrk-actionexample-patter"><thead><tr><th>Action</th><th>Example pattern</th></tr></thead><tbody><tr><td>List</td><td>`GET https://pbx.example.com/pbx/openapi.php/phonebooks?tenant=CANISTRACCI`</td></tr><tr><td>Get by ID</td><td>`GET https://pbx.example.com/pbx/openapi.php/phonebooks/OBJECT_ID?tenant=CANISTRACCI`</td></tr><tr><td>Create</td><td>`POST https://pbx.example.com/pbx/openapi.php/phonebooks?tenant=CANISTRACCI`</td></tr><tr><td>Update</td><td>`PATCH https://pbx.example.com/pbx/openapi.php/phonebooks/OBJECT_ID?tenant=CANISTRACCI`</td></tr><tr><td>Delete</td><td>`DELETE https://pbx.example.com/pbx/openapi.php/phonebooks/OBJECT_ID?tenant=CANISTRACCI`</td></tr></tbody></table>

## Accepted Field Aliases

<table id="bkmrk-request-fieldsource-"><thead><tr><th>Request field</th><th>Source field</th></tr></thead><tbody><tr><td>`name`</td><td>`pb_name`</td></tr><tr><td>`include_extensions`</td><td>`pb_includeext`</td></tr><tr><td>`includeext`</td><td>`pb_includeext`</td></tr><tr><td>`include_short_numbers`</td><td>`pb_includeshortnum`</td></tr><tr><td>`includeshortnum`</td><td>`pb_includeshortnum`</td></tr><tr><td>`layout`</td><td>`pl_phonebooklayouts`</td></tr><tr><td>`items`</td><td>`pl_phonebooklayouts`</td></tr></tbody></table>

## Important Notes

- The layout field replaces the phone book field layout. Values can be phone book item codes such as NAME, PHONE1, EMAIL, and ROUTING, or pi\_phonebookitems IDs.
- When layout is omitted on create, the API creates the default layout NAME, PHONE1, PHONE2, EMAIL, ROUTING.
- Deleting a phone book removes its layout rows, entries, entry details, and phone assignment rows.

## Examples

### List Phone Books

Returns the phone books visible to the key and scope.

```
curl -H "X-API-Key: TENANT_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/phonebooks?tenant=CANISTRACCI"
```

### Get Phone Book

Reads one object by its internal ID.

```
curl -H "X-API-Key: TENANT_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/phonebooks/OBJECT_ID?tenant=CANISTRACCI"
```

### Create Phone Book

Creates a new object. Use the short aliases shown above or the source field names.

```
curl -X POST \
  -H "X-API-Key: TENANT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Docs API Phone Book",
  "include_extensions": "no",
  "include_short_numbers": "no",
  "layout": [
    "NAME",
    "PHONE1",
    "PHONE2",
    "EMAIL",
    "ROUTING"
  ]
}' \
  "https://pbx.example.com/pbx/openapi.php/phonebooks?tenant=CANISTRACCI"
```

### Edit Phone Book

Updates only the supplied fields.

```
curl -X PATCH \
  -H "X-API-Key: TENANT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Docs API Phone Book Updated",
  "layout": [
    "NAME",
    "PHONE1",
    "EMAIL",
    "ROUTING"
  ]
}' \
  "https://pbx.example.com/pbx/openapi.php/phonebooks/OBJECT_ID?tenant=CANISTRACCI"
```

### Delete Phone Book

Deletes the object. Check references before deleting configuration used by routing or reporting.

```
curl -X DELETE \
  -H "X-API-Key: TENANT_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/phonebooks/OBJECT_ID?tenant=CANISTRACCI"
```

### Replace Phone Book Layout

Replaces the pl\_phonebooklayouts rows for the selected phone book.

```
curl -X PATCH \
  -H "X-API-Key: TENANT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "layout": [
    "NAME",
    "PHONE1",
    "EMAIL",
    "ROUTING"
  ]
}' \
  "https://pbx.example.com/pbx/openapi.php/phonebooks/OBJECT_ID?tenant=CANISTRACCI"
```

## Common Errors

<table id="bkmrk-errormeaningmissing_"><thead><tr><th>Error</th><th>Meaning</th></tr></thead><tbody><tr><td>`missing_api_key`</td><td>No API key was supplied in the query string, `X-API-Key`, or bearer token.</td></tr><tr><td>`invalid_api_key`</td><td>The supplied key does not match the tenant or global API key.</td></tr><tr><td>`tenant_required`</td><td>A tenant code is required for tenant-scoped writes or tenant-key reads.</td></tr><tr><td>`read_only_api_key`</td><td>The key can read data but cannot create, update, or delete objects.</td></tr><tr><td>`missing_required_field`</td><td>A required create field is missing.</td></tr></tbody></table>

## Download, bulk add, and clear one phone book

Use `/phonebooks/{id}/entries` to work with all stored contacts in one selected tenant phone book. Replace `{id}` with the phone book ID and supply the tenant code or name through `tenant`. A global API key must also select a tenant for these operations.

<table id="bkmrk-methodoperationapi-k"><thead><tr><th>Method</th><th>Operation</th><th>API key</th></tr></thead><tbody><tr><td>GET</td><td>Download every stored entry and its field values as JSON, without pagination.</td><td>Full or read-only</td></tr><tr><td>POST</td><td>Append 1–500 entries in one request.</td><td>Full</td></tr><tr><td>DELETE</td><td>Delete all entries and their details from the selected phone book.</td><td>Full</td></tr></tbody></table>

The download covers database contacts. Automatically included extensions and short numbers are not synthesized into the export. Deleting entries preserves the phone book definition, its layout, and its phone assignments. Other phone books and tenants are unchanged. To delete the phone book itself, use the separate `DELETE /phonebooks/{id}` operation.

### Download all contacts

```
curl -H "X-API-Key: TENANT_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/phonebooks/8/entries?tenant=CANISTRACCI" \
  -o phonebook.json
```

The response includes the phone book ID and name, a contact count, and an `entries` array. Each contact has its entry ID, a `values` object keyed by phone book field codes, and the stored detail records. An empty phone book returns `"count": 0` and `"entries": []`.

```
{
  "phonebook_id": 8,
  "name": "Customers",
  "count": 1,
  "entries": [{
    "id": 101,
    "values": {"NAME": "Alice Smith", "PHONE1": "00123456789"},
    "details": [
      {"id": 201, "item_id": 1, "code": "NAME", "value": "Alice Smith"},
      {"id": 202, "item_id": 2, "code": "PHONE1", "value": "00123456789"}
    ]
  }]
}
```

The IDs in these examples are illustrative. Existing field codes and item IDs can be inspected with `GET /phonebooks/8?tenant=CANISTRACCI` in the `related.available_items` data.

### Add several contacts

Send an object containing an `entries` array. Each entry contains a `values` object. Use configured field codes such as `NAME`, `PHONE1`, `PHONE2`, and `EMAIL`; field codes are case-insensitive. Values can be strings, numbers, or null. Keep phone numbers as strings to preserve leading zeros and plus signs. Null and empty values are omitted, and each contact must contain at least one non-empty value. Unknown or duplicate field codes are rejected.

```
curl -X POST \
  -H "X-API-Key: TENANT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"entries":[
    {"values":{"NAME":"Alice Smith","PHONE1":"00123456789"}},
    {"values":{"NAME":"Bob Jones","PHONE1":"+390212345678","EMAIL":"bob@example.com"}}
  ]}' \
  "https://pbx.example.com/pbx/openapi.php/phonebooks/8/entries?tenant=CANISTRACCI"
```

A successful request returns HTTP 201 with IDs in request order:

```
{"phonebook_id":8,"created":2,"ids":[101,102]}
```

Missing database fields use the applicable New Item Defaults, as with other creation APIs. Tenant and phone book ownership always come from the authenticated scope and URL. Send larger imports in batches of at most 500 contacts. POST appends contacts; repeating a successful request creates additional entries.

To reuse a download as input, build a new object containing `entries` with only each contact's `values`. Do not submit exported IDs, detail records, or the download's name/count fields.

### Clear the selected phone book

```
curl -X DELETE -H "X-API-Key: TENANT_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/phonebooks/8/entries?tenant=CANISTRACCI"
```

```
{"phonebook_id":8,"deleted":true,"deleted_entries":2,"deleted_details":5}
```

No body is required. Clearing an already empty phone book succeeds with zero counts.

### Transactions and failures

Each bulk POST or DELETE uses one checked database transaction. The phone book, entry, and detail tables must use InnoDB. If a contact cannot be inserted, the complete batch is rolled back, including entries inserted earlier in the request. Database truncation warnings also cause a rollback. A clear request followed by an import is two separate transactions.

Errors use the usual `error.code` and `error.message` response. Database failures include an error number and affected column when available, without exposing SQL or contact values. Transaction failures also include `error.rolled_back` (true if rollback completed, false if it could not be confirmed, null if no transaction started) and `error.entry_index` (the one-based entry position when available). Validation before a transaction can return the standard error shape without those additional fields.

```
{"error":{"code":"phonebook_bulk_failed","message":"Database error 1406 (column pd_pi_value) while saving entry 2 details. The transaction was rolled back.","rolled_back":true,"entry_index":2}}
```

If activity logging fails after the database commit, the response remains successful and includes a `warning`; the contacts have already been saved or deleted.

### Alternative query-string routing

For installations that do not accept path information, use the same methods with:

```
https://pbx.example.com/pbx/openapi.php?object=phonebook&action=entries&id=8&tenant=CANISTRACCI
```