# Tenant

The **Tenant** object is supported by the MiRTA PBX OpenAPI endpoint. This object is managed at system scope and requires a global API key.

## Object Summary

<table id="bkmrk-propertyvalueobjectt"><thead><tr><th>Property</th><th>Value</th></tr></thead><tbody><tr><td>Object</td><td>`tenant`</td></tr><tr><td>Primary path</td><td>`/tenants`</td></tr><tr><td>ID field</td><td>`te_id`</td></tr><tr><td>Label field</td><td>`te_name`</td></tr><tr><td>Primary source table</td><td>`te_tenants`</td></tr><tr><td>Required on create</td><td>`te_name`, `te_code`</td></tr><tr><td>Path aliases</td><td>`/tenants`</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/tenants`</td></tr><tr><td>Get by ID</td><td>`GET https://pbx.example.com/pbx/openapi.php/tenants/OBJECT_ID`</td></tr><tr><td>Create</td><td>`POST https://pbx.example.com/pbx/openapi.php/tenants`</td></tr><tr><td>Update</td><td>`PATCH https://pbx.example.com/pbx/openapi.php/tenants/OBJECT_ID`</td></tr><tr><td>Delete</td><td>`DELETE https://pbx.example.com/pbx/openapi.php/tenants/OBJECT_ID`</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>`te_name`</td></tr><tr><td>`code`</td><td>`te_code`</td></tr><tr><td>`billingcode`</td><td>`te_billingcode`</td></tr><tr><td>`billing_code`</td><td>`te_billingcode`</td></tr><tr><td>`timezone`</td><td>`te_timezone`</td></tr><tr><td>`routing_profile_id`</td><td>`te_rp_id`</td></tr><tr><td>`campaign_routing_profile_id`</td><td>`te_campaign_rp_id`</td></tr><tr><td>`fax_routing_profile_id`</td><td>`te_fax_rp_id`</td></tr></tbody></table>

## Examples

### List Tenants

Returns the tenants visible to the key and scope.

```
curl -H "X-API-Key: GLOBAL_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/tenants"
```

To filter the list with a full or read-only legacy global key, pass `tenant` as a tenant code or name. The selector uses SQL `LIKE`. Use `%` for any sequence of characters or `_` for a single character. Omit `tenant` to list all tenants available to the key.

```
curl --get 'https://pbx.example.com/pbx/openapi.php/tenants' \
  -H 'X-API-Key: GLOBAL_API_KEY' \
  --data-urlencode 'tenant=%Example%'
```

An advanced key needs global permission and access to `listTenants`. Supply one exact code or name within its allowed scope, or omit `tenant` to list that scope.

<table id="bkmrk-tenant-list-errors"><thead><tr><th>HTTP status</th><th>Meaning</th></tr></thead><tbody><tr><td>400</td><td>`invalid_tenant` when the selector is supplied in a form other than a string.</td></tr><tr><td>404</td><td>`tenant_not_found` when the selector has no match.</td></tr><tr><td>409</td><td>`ambiguous_tenant` when the exact selector for an advanced key is ambiguous.</td></tr><tr><td>500</td><td>`database_error` when the legacy tenant lookup or the tenant list query fails.</td></tr><tr><td>503</td><td>`api_key_unavailable` when the advanced key policy or exact tenant lookup is unavailable.</td></tr></tbody></table>

### Get Tenant

Reads one object by its internal ID.

```
curl -H "X-API-Key: GLOBAL_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/tenants/OBJECT_ID"
```

### Create Tenant

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

```
curl -X POST \
  -H "X-API-Key: GLOBAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Docs API Tenant",
  "code": "DOCSAPI",
  "timezone": "Europe/Rome"
}' \
  "https://pbx.example.com/pbx/openapi.php/tenants"
```

### Edit Tenant

Updates only the supplied fields.

```
curl -X PATCH \
  -H "X-API-Key: GLOBAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "routing_profile_id": 3,
  "te_payment_type": "Post Paid"
}' \
  "https://pbx.example.com/pbx/openapi.php/tenants/OBJECT_ID"
```

### Delete Tenant

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

```
curl -X DELETE \
  -H "X-API-Key: GLOBAL_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/tenants/OBJECT_ID"
```

## Tenant Initialization

`POST /tenants` requires a full global API key. It creates the tenant and its related records in one transaction.

```
curl -X POST \
  -H "X-API-Key: GLOBAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Acme",
  "code": "ACME",
  "parking": {"start": 700, "end": 720, "hosted": ""},
  "user_ids": [12],
  "usergroup_ids": [3]
}' \
  "https://pbx.example.com/pbx/openapi.php/tenants"
```

- Global new-item defaults for tenants and parking apply first; explicit request values override them. Tenant settings are copied from the global new-item defaults. `APIKEY` and `READONLYAPIKEY` defaults set to `RANDOM16` become generated 16-character keys. Empty `PARKTIMEOUT`, `MAXCALLDURATION` and `DIALTIMEOUT` receive 120, 7200 and 60.
- One parking lot named after the tenant code is created, by default 700–720 on an automatic server. Use the `parking` object, or `pk_start`/`pk_end`/`pk_hosted`. Asterisk is not reloaded and no server placement is performed.
- `user_ids` and `usergroup_ids` give existing users and groups access to the tenant. The default custom destination restriction mode is applied; `ctselected`, an array of custom type IDs, overrides the configured exceptions.
- Codes need at least two characters without spaces. Duplicate codes return 409 and the tenant license limit is checked before saving.
- The response includes an `initialization` summary. Errors include `rolled_back`: `true` confirms that nothing was saved. HTTP `503 tenant_commit_unknown` returns the `tenant_id`; inspect that tenant before retrying.

These inputs apply only to creation. To change an existing tenant, use the tenant, settings, parking, user and user-group operations.

## Tenant Server Placement

`GET /tenants/TENANT_ID/placement` shows which cluster servers a tenant uses, with a full or read-only global key. `POST` changes it and requires a full global key. Tenant keys cannot inspect or change placement. Use the numeric tenant ID in the path and omit `tenant`.

```
curl -H "X-API-Key: GLOBAL_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/tenants/42/placement"

curl -X POST -H "X-API-Key: GLOBAL_API_KEY" -H "Content-Type: application/json" \
  -d '{"settings":{"preferred_queue_node_id":2},"assignments":{"queues":2}}' \
  "https://pbx.example.com/pbx/openapi.php/tenants/42/placement"
```

GET returns the placement `settings`, the raw stored preferences, the resolved defaults, the current assignment of each service and the server inventory with eligibility and diagnostics. Eligible means registered with a usable peer name; it does not prove that the server is healthy.

<table id="bkmrk-body-objectmeaning-s"><thead><tr><th>Body object</th><th>Meaning</th></tr></thead><tbody><tr><td>`settings`</td><td>Defaults and preferences for new objects: `cronjob_node_id`, `campaign_node_id`, `fax_node_id`, `preferred_queue_node_id`, `preferred_parking_node_id`. Existing objects are not moved.</td></tr><tr><td>`assignments`</td><td>Moves every current object of a service in the tenant: `queues`, `parkinglots`, `conferences`, `campaigns`, `cronjobs`. System jobs and `UPLOADRECORDING` jobs are excluded.</td></tr></tbody></table>

- Values are server IDs, not peer names. Supply at least one field; omitted fields are preserved. `0` clears a preference or explicit host so the object inherits the default or automatic placement again.
- Changing `campaign_node_id` also updates the tenant `DEFAULTCAMPAIGNSERVER` setting.
- Stop running campaigns before changing their placement. A running or pending cron job returns `409 placement_busy`; database lock conflicts also return 409, so reload and retry later.
- Queue changes are notified to every registered server after saving; `queue_refresh` reports which servers accepted the notification. Parking modules are not reloaded, so moving parking lots may need a scheduled restart, as in the web editor.
- The save is a single transaction. Errors include `rolled_back`; `503 placement_commit_unknown` means the outcome is unknown, so inspect GET before retrying.

## 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>