# mTLS

Upload the CA that client certificates are verified against, switch mTLS on and say which hostnames and paths ask for a certificate.

mTLS makes a domain ask its visitors for a client certificate. This part of the API holds the CA those certificates are verified against, the switch that turns mTLS on, and the rules that say where a certificate is required or optional. The paths start with `/v2/cdn/mtls/{domain_public_id}/`, where the id is the public id of the domain, taken from [the domains list](https://docs.diamwall.com/products/domains/#list-your-domains).

- [CA](#the-ca): Upload, read, replace and delete the CA of the domain.
- [Settings](#settings): Switch mTLS on or off and set the verification depth.
- [Rules](#rules): Choose the hostnames and paths that ask for a certificate.

The examples on this page use these shell variables:

```bash
export DIAMWALL_API_KEY="your key"
export MTLS="https://api.diamwall.com/v2/cdn/mtls/your-domain-public-id"
```

## The order of calls

The three parts depend on each other, so the order matters:

| Step | Call | Why in this order |
| --- | --- | --- |
| 1 | `POST .../mtls-ca/` | Creating the CA does not switch mTLS on, and mTLS cannot be switched on without a CA. |
| 2 | `PATCH .../settings/mtls/` with `"enabled": true` | Needs a CA that is valid now. |
| 3 | `POST .../mtls-rules/` | A rule write with mTLS off answers `409 INVALID_STATE`. |

To remove everything, go the other way: switch mTLS off, then delete the CA. The CA is deleted only with mTLS switched off.

### Writes need the latest revision

Every write takes an `If-Match` header with the `ETag` of a read. There are three separate revisions, one for each part:

| You write | Read this first for the `ETag` |
| --- | --- |
| The CA | `GET .../mtls-ca/` or `GET .../mtls-ca/current/` |
| The settings | `GET .../settings/mtls/` |
| A rule | `GET .../mtls-rules/` or `GET .../mtls-rules/{rule_id}/` |

The `ETag` is one value in quotes, `"sha256:..."`. Send it back exactly as it came, quotes included. Each write answers with a new `ETag` header to use on your next write of the same part. The examples below read it like this:

```bash
ETAG=$(curl -s -o /dev/null -D - "$MTLS/mtls-ca/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  | tr -d '\r' | awk 'tolower($1) == "etag:" { print $2 }')
```

Without `If-Match` the write answers `428`. If the part changed since your read, it answers `412 REVISION_MISMATCH`: read again and retry.

## The CA

A domain has one CA or none. Its id is always `current`.

### Read the CA

[`GET /v2/cdn/mtls/{domain_public_id}/mtls-ca/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/GET/v2/cdn/mtls/{domain_public_id}/mtls-ca/)

```bash
curl "$MTLS/mtls-ca/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:f5d8dacf16ec...",
  "consistency": "verified",
  "data": {
    "state": "resolved",
    "items": [
      {
        "id": "current",
        "present": true,
        "subject": "CN=Example Private CA",
        "issuer": "CN=Example Private CA",
        "not_before": "2026-10-10T00:00:00.000Z",
        "not_after": "2027-10-10T00:00:00.000Z",
        "fingerprint_sha256": "00:01:02:03:04:05:06:07:...",
        "status": "active"
      }
    ],
    "page": { "limit": 50, "next_cursor": null, "total": 1 }
  }
}
```

- An empty `data.items` is a domain with no CA. The `ETag` header is there for an empty list too: it is what you send to create the first CA.
- No read ever answers the PEM. You get the public metadata of the certificate only.
- A CA can carry a `state` and a `reason` of its own, for example with `"status": "expired"`. Show that CA as unavailable, never as a healthy one.
- If `data.state` is there and is not `resolved`, the answer has no `items`, no `page` and no `ETag`. The CA is unavailable. Do not read that as a domain with no CA.

`GET .../mtls-ca/current/` answers the same CA under `data`. On a domain with no CA it answers `404 RESOURCE_ITEM_NOT_FOUND`, which is not an error to retry.

### Upload a CA

[`POST /v2/cdn/mtls/{domain_public_id}/mtls-ca/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/POST/v2/cdn/mtls/{domain_public_id}/mtls-ca/)

The body has exactly one field, `certificate`: the certificate of the CA in PEM, as a string. This example builds the JSON from a file with `jq`, so the line breaks of the PEM are escaped for you:

```bash
jq -n --rawfile certificate ca.pem '{ certificate: $certificate }' \
  | curl -X POST "$MTLS/mtls-ca/" \
      -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
      -H "Content-Type: application/json" \
      -H "If-Match: $ETAG" \
      -d @-
```

The answer is `201` with the metadata of the new CA under `data`, and a new `ETag` header.

| Rule | What happens otherwise |
| --- | --- |
| `certificate` is a string that is not empty, at most 1 MiB | `400 INVALID_REQUEST` with `fields: ["certificate"]` |
| The whole body is at most 3 MiB | `413 PAYLOAD_TOO_LARGE` |
| Exactly one certificate, marked as a CA (`CA:TRUE`) and valid now | `400 INVALID_CA` |
| The domain has no CA yet | `409 ALREADY_EXISTS`. Replace the CA instead. |

> **Never send the private key**
> No field takes the private key of the CA. Send the certificate only. The certificate is passed on to the CDN and is never answered back, by this request or by any read.

### Replace the CA

[`PATCH /v2/cdn/mtls/{domain_public_id}/mtls-ca/{ca_id}/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/PATCH/v2/cdn/mtls/{domain_public_id}/mtls-ca/{ca_id}/)

The body and its rules are the same as for an upload. `ca_id` is always `current`.

```bash
jq -n --rawfile certificate new-ca.pem '{ certificate: $certificate }' \
  | curl -X PATCH "$MTLS/mtls-ca/current/" \
      -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
      -H "Content-Type: application/json" \
      -H "If-Match: $ETAG" \
      -d @-
```

Replacing the CA does not switch mTLS on or off. Client certificates are verified against the new CA from then on. On a domain with no CA the request answers `404 RESOURCE_ITEM_NOT_FOUND`: upload one instead.

### Delete the CA

[`DELETE /v2/cdn/mtls/{domain_public_id}/mtls-ca/{ca_id}/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/DELETE/v2/cdn/mtls/{domain_public_id}/mtls-ca/{ca_id}/)

Switch mTLS off first. With mTLS on, the delete answers `409 INVALID_STATE` and nothing is deleted.

```bash
curl -X DELETE "$MTLS/mtls-ca/current/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "If-Match: $ETAG"
```

The request takes no body. The answer is `200` with no `data`, and `operation.id` is the id the CA had.

## Settings

### Read the settings

[`GET /v2/cdn/mtls/{domain_public_id}/settings/mtls/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/GET/v2/cdn/mtls/{domain_public_id}/settings/mtls/)

```bash
curl "$MTLS/settings/mtls/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:5a1211b2fd83...",
  "consistency": "verified",
  "data": {
    "configuration": {
      "state": "resolved",
      "value": { "enabled": true, "verify_depth": 2 }
    },
    "ca_certificate": {
      "state": "resolved",
      "value": {
        "present": true,
        "subject": "CN=Example Private CA",
        "not_after": "2027-10-10T00:00:00.000Z",
        "status": "active"
      }
    }
  }
}
```

- `configuration.value` holds `enabled`, whether mTLS is on, and `verify_depth`, how deep the chain of a client certificate is verified.
- `ca_certificate.value` holds the same public metadata of the CA as the CA endpoints.
- Each of the two has a `state`, and a `value` only when the `state` is `resolved`. A `state` of `unknown` means the CDN could not tell. Treat it as unavailable, never as mTLS off or as a domain with no CA.
- To know whether the domain has a CA at all, read `mtls-ca/`. The rules are not part of this answer.

When the whole group could not be read, the answer has `"state": "unknown"`, a `revision` of `null`, no `data` and no `ETag`. There is nothing to send in `If-Match`, so do not write.

### Switch mTLS on or off

[`PATCH /v2/cdn/mtls/{domain_public_id}/settings/mtls/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/PATCH/v2/cdn/mtls/{domain_public_id}/settings/mtls/)

Read `settings/mtls/` to get its `ETag`, then send at least one of the two fields. A field you leave out keeps its value.

| Field | Allowed values |
| --- | --- |
| `enabled` | `true` or `false`, as a JSON boolean |
| `verify_depth` | A whole number from 1 to 10 |

**Switch on**

```bash
curl -X PATCH "$MTLS/settings/mtls/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $SETTINGS_ETAG" \
  -d '{ "enabled": true }'
```

**Switch off**

```bash
curl -X PATCH "$MTLS/settings/mtls/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $SETTINGS_ETAG" \
  -d '{ "enabled": false }'
```

**Verification depth**

```bash
curl -X PATCH "$MTLS/settings/mtls/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $SETTINGS_ETAG" \
  -d '{ "verify_depth": 3 }'
```

Values are not converted: `"true"`, `1` or `"2"` answer `400 INVALID_REQUEST` with the field in `fields`. An empty object answers `400 NO_CHANGES`. The answer is the settings after the edit, in the same form as the read, with a new `ETag` header.

Switching on is refused with a `409` when the domain is not ready, and nothing is written:

| Code | What to do |
| --- | --- |
| `MISSING_CA` | The domain has no CA. Upload one, read the settings again for a new `ETag`, then send the edit again. |
| `INVALID_CA` | The CA is expired, not yet valid, or not a CA. Replace it, then send the edit again. |
| `RUNTIME_UNAVAILABLE` | mTLS cannot be used for this domain right now. Nothing in the request can fix it. Do not repeat it, and quote the `request_id` to support. |

## Rules

A rule says on which hostname and path a client certificate is required, or optional. A domain holds at most 100 rules.

| Field | Rules | When left out on create |
| --- | --- | --- |
| `name` | Required on create. 1 to 128 characters: letters, digits, spaces, `_`, `.` and `-`. Unique in the domain. Never trimmed. | |
| `hostname` | The domain, a name under it, or a wildcard of either. 1 to 253 characters. | The domain itself |
| `path` | Begins with `/`, at most 2048 characters, with no `//` and no `.` or `..` segment. | `/` |
| `action` | `require` or `optional` | `require` |
| `description` | One line, at most 1024 bytes. An empty string clears it. | |

### List the rules

[`GET /v2/cdn/mtls/{domain_public_id}/mtls-rules/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/GET/v2/cdn/mtls/{domain_public_id}/mtls-rules/)

```bash
curl "$MTLS/mtls-rules/?limit=50" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:8d30477b8fc2...",
  "data": {
    "state": "resolved",
    "items": [
      {
        "id": "1122334455667788",
        "name": "Require client certificate",
        "hostname": "api.example.com",
        "path": "/private",
        "action": "require",
        "description": "Clients of the private API"
      }
    ],
    "page": { "limit": 50, "next_cursor": null, "total": 1 }
  }
}
```

- `limit` goes from 1 to 100. Without it a page holds 50 rules.
- To read the next page, send `data.page.next_cursor` back as `cursor`, until it is `null`.
- If a rule changes between two pages, the next page answers `409 CURSOR_STALE`. Start again from the first page.
- If `data.state` is there and is not `resolved`, the answer has no `items`, no `page` and no `ETag`. The rules are unavailable. Do not read that as a domain with no rule.

`GET .../mtls-rules/{rule_id}/` answers one rule under `data`, in the same form.

### Add a rule

[`POST /v2/cdn/mtls/{domain_public_id}/mtls-rules/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/POST/v2/cdn/mtls/{domain_public_id}/mtls-rules/)

Read the rules first to get the `ETag`. It is there for an empty list too.

**Require on a path**

```bash
curl -X POST "$MTLS/mtls-rules/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $RULES_ETAG" \
  -d '{
    "name": "Require client certificate",
    "description": "Clients of the private API",
    "hostname": "api.example.com",
    "path": "/private",
    "action": "require"
  }'
```

**Optional on a wildcard**

```bash
curl -X POST "$MTLS/mtls-rules/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $RULES_ETAG" \
  -d '{
    "name": "Optional under the domain",
    "hostname": "*.example.com",
    "action": "optional"
  }'
```

**Name only**

```bash
curl -X POST "$MTLS/mtls-rules/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $RULES_ETAG" \
  -d '{ "name": "Admin area" }'
```

The answer is `201` with the new rule under `data`, defaults filled in, and a new `ETag` header. The `id` of the rule is also in `operation.id`.

> **What is refused**
> - A `hostname` of another domain answers `400 INVALID_REQUEST` with `fields: ["hostname"]`.
> - A hostname or path the CDN does not accept, such as a wildcard in the wrong place, answers `400 INVALID_RULE`.
> - A name another rule already has answers `409 DUPLICATE_NAME`.
> - A rule beyond the 100th answers `409 QUOTA_EXCEEDED`. Delete a rule first.

### Change a rule

[`PATCH /v2/cdn/mtls/{domain_public_id}/mtls-rules/{rule_id}/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/PATCH/v2/cdn/mtls/{domain_public_id}/mtls-rules/{rule_id}/)

Send only the fields that change. A field you leave out keeps its value, and the `id` of the rule stays the same.

```bash
curl -X PATCH "$MTLS/mtls-rules/1122334455667788/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $RULES_ETAG" \
  -d '{ "action": "optional" }'
```

An empty body answers `400 NO_CHANGES`. To clear the description, send `"description": ""`.

### Delete a rule

[`DELETE /v2/cdn/mtls/{domain_public_id}/mtls-rules/{rule_id}/` in the API reference](https://docs.diamwall.com/api/#tag/mtls/DELETE/v2/cdn/mtls/{domain_public_id}/mtls-rules/{rule_id}/)

```bash
curl -X DELETE "$MTLS/mtls-rules/1122334455667788/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "If-Match: $RULES_ETAG"
```

The request takes no body. The answer is `200` with no `data`, and `operation.id` is the id the rule had.

## When a write gets no clear answer

Writes here take no `Idempotency-Key`. After `500 UNEXPECTED_ERROR`, `502 FILE_SERVER_OUTCOME_UNKNOWN` or `503 READBACK_MISMATCH` the write may have been made. Read the part again before any other attempt, and never repeat the request blindly. After `502 FILE_SERVER_NO_ANSWER` nothing changed.

## Limits and permissions

- **Credits.** Each domain has a budget of 3000 credits a minute, shared by everyone who calls the API for that domain. Every mTLS call costs 8 credits. A request refused with `400`, `403`, `413` or `428` before it reaches the CDN costs none.
- **Out of credits.** The call answers `429 TOO_MANY_REQUESTS`, with the wait in `details.retry_after_seconds`.
- **Plan.** Every plan has mTLS today. A domain whose plan had no mTLS would answer `403 FEATURE_NOT_AVAILABLE`, with `details.feature` and `details.plan`.
- **Rule limit.** 100 rules per domain, on every plan.
- **Permissions.** mTLS uses the SSL permissions of the domain:

| Action | Permission |
| --- | --- |
| Any read | `ssl.view` |
| Upload or replace the CA, edit the settings, edit a rule | `ssl.edit` |
| Create a rule | `ssl.create` |
| Delete the CA or a rule | `ssl.delete` |

## Errors you are likely to see

| Status | Code | What happened |
| --- | --- | --- |
| `400` | `INVALID_REQUEST` | A field, a query parameter or an id is wrong. The answer names it in `fields` when it can. |
| `400` | `NO_CHANGES` | An edit was sent with nothing to change. |
| `400` | `INVALID_CA` | The certificate is not exactly one CA certificate that is valid now. |
| `400` | `INVALID_RULE` | The CDN does not accept the hostname or the path of the rule. |
| `400` | `INVALID_PRECONDITION` | The `If-Match` header is not in the form of an `ETag`. |
| `404` | `RESOURCE_ITEM_NOT_FOUND` | The domain has no CA, or no rule has that `id`. |
| `404` | `SETTINGS_GROUP_NOT_FOUND` | The CDN does not have the mTLS settings of the domain. This is not mTLS off. |
| `409` | `ALREADY_EXISTS` | The domain already has a CA. Replace it instead. |
| `409` | `MISSING_CA` | mTLS cannot be switched on: the domain has no CA. |
| `409` | `INVALID_CA` | mTLS cannot be switched on: the CA is not valid now. |
| `409` | `INVALID_STATE` | The CA cannot be deleted with mTLS on, or a rule cannot be written with mTLS off. |
| `409` | `DUPLICATE_NAME` | Another rule of the domain has that name. |
| `409` | `QUOTA_EXCEEDED` | The domain already has 100 rules. |
| `409` | `RUNTIME_UNAVAILABLE` | mTLS cannot be used for this domain right now. Quote the `request_id` to support. |
| `409` | `CURSOR_STALE` | The list changed while you were paging. Start from the first page. |
| `412` | `REVISION_MISMATCH` | The part changed since your read. Read again and retry. |
| `413` | `PAYLOAD_TOO_LARGE` | The body of a CA upload is larger than 3 MiB. |
| `428` | `PRECONDITION_REQUIRED` | The `If-Match` header is missing. |
| `503` | `READBACK_MISMATCH` | The write may have been made. Read again before trying anything else. |

The errors that come from the key itself, such as `API_KEY_IP_NOT_ALLOWED`, are on the [Errors](https://docs.diamwall.com/reference/errors/) page.

## All mTLS endpoints

Each row opens the endpoint in the API reference, with every parameter, rule and response.

See the [API reference](https://docs.diamwall.com/api/#tag/mtls) for every endpoint, with parameters and responses.
