# Custom pages

Replace the pages the CDN shows to visitors with your own text, set the captcha colors and put hosts in maintenance.

Every domain has nine pages that the CDN serves itself. Here you read them, replace their text with your own, put the default back, and change the `customize` settings: the captcha colors and the maintenance mode. The paths start with `/v2/cdn/custom_pages/{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).

- [Pages](#pages): List the nine pages, read one, and see which are your own.
- [Page text](#page-text): Read, write and remove the text of a page.
- [Colors and maintenance](#captcha-colors-and-maintenance): Set the captcha colors and choose the hosts in maintenance.

The examples on this page use these shell variables:

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

Custom pages are part of the Business and Enterprise plans. A domain on any other plan answers `403 FEATURE_NOT_AVAILABLE` on every call here.

## Pages

### The nine pages

The collection always has the same nine pages. Their ids are fixed:

`5s`, `unusual`, `502`, `504`, `denied`, `oops`, `dwaf`, `maintenance`, `bot-blocked`

A page is never created and never gone. It is either the default page, or a page the domain has a text of its own for. Any other id answers `400 INVALID_REQUEST` and names `page_id`.

### Two ETags, two kinds of write

Every write has to prove it saw the latest state, with the `ETag` of a read sent back in `If-Match`, exactly as it came. There are three different ETags here, and they are not interchangeable:

| You want to | Read first | Then send its `ETag` to |
| --- | --- | --- |
| Write or remove the text of a page | `GET .../pages/{page_id}/content/` | `PUT` or `DELETE .../pages/{page_id}/content/` |
| Delete a page from the collection | `GET .../pages/` or `GET .../pages/{page_id}/` | `DELETE .../pages/{page_id}/` |
| Edit the colors or the maintenance mode | `GET .../settings/customize/` | `PATCH .../settings/customize/` |

A write without `If-Match` answers `428`. A write whose `ETag` is out of date answers `412 REVISION_MISMATCH`: read again and retry. An `If-Match` that is not the `ETag` as it came (a weak one, `*`, a list, no quotes) answers `400 INVALID_PRECONDITION`.

### List the pages

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

```bash
curl "$PAGES/pages/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:0d1e6041d11f...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "items": [
      {
        "id": "5s",
        "metadata": { "state": "resolved", "value": { "is_custom": false, "byte_size": 38 } }
      },
      {
        "id": "502",
        "metadata": { "state": "resolved", "value": { "is_custom": true, "byte_size": 21 } }
      }
    ],
    "page": { "limit": 50, "next_cursor": null, "total": 9 }
  }
}
```

- `metadata.value.is_custom` is `true` when the domain has its own text for the page, and `false` when the page is the default one.
- `metadata.value.byte_size` is the size of the page in bytes. When it is missing, the size is unavailable. It is never zero.
- A `metadata` whose `state` is `unknown` has no `value`. Treat the page as unavailable, never as a default page.
- The list holds no HTML. The text is read through [its own endpoint](#read-the-text).
- `limit` goes from 1 to 100, and is 50 without it. To read the next page, send `data.page.next_cursor` back as `cursor`, until it is `null`. If the collection changes between two pages, the next page answers `409 CURSOR_STALE`. Start again from the first page.
- No other query parameter is accepted.

### Read one page

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

```bash
curl "$PAGES/pages/502/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

The page is under `data`, with the same `id` and `metadata` as in the list.

The `bot-blocked` page has two more fields in `metadata.value`:

| Field | Meaning |
| --- | --- |
| `available` | `true`: the page is ready to be written. `false`: a write of its text is refused, and `reason` says why in one word. Its text can still be read and removed. When the field is missing, the CDN did not say. That is not `false`. |
| `applied` | Always `unknown`. Saving the page is never a confirmation that it is served everywhere. |

### Delete a page

[`DELETE /v2/cdn/custom_pages/{domain_public_id}/pages/{page_id}/` in the API reference](https://docs.diamwall.com/api/#tag/custom-pages/DELETE/v2/cdn/custom_pages/{domain_public_id}/pages/{page_id}/)

Deleting a page removes the domain's own text of it, so the CDN serves the default page again. The page stays in the collection. The request takes no body, and its `If-Match` is the `ETag` of the list or of one page:

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

curl -X DELETE "$PAGES/pages/502/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "If-Match: $ETAG"
```

The answer has an `operation` object with the `id` of the page, and a new `ETag` header for the next write to the collection. It holds no page: read the page to see what it is now.

[Removing the text](#remove-the-text) has the same effect. The two calls differ only in the `ETag` they take.

> **bot-blocked cannot be deleted here**
> A delete of `bot-blocked` answers `400` and names `page_id`. To put its default page back, remove its text instead.

## Page text

### Read the text

[`GET /v2/cdn/custom_pages/{domain_public_id}/pages/{page_id}/content/` in the API reference](https://docs.diamwall.com/api/#tag/custom-pages/GET/v2/cdn/custom_pages/{domain_public_id}/pages/{page_id}/content/)

```bash
curl -i "$PAGES/pages/502/content/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "data": {
    "page": "502",
    "content": "<!doctype html>\n<html>\n  <body>\n    <h1>We will be right back</h1>\n  </body>\n</html>\n",
    "publication": "verified"
  }
}
```

- `data.content` is the whole text of the page as the CDN serves it: the domain's own text when it has one, otherwise the text of the default page. To know which of the two it is, read `is_custom` of [the page](#read-one-page).
- The text always comes as a string inside JSON, never as `text/html`. A request whose `Accept` header allows no JSON answers `406 NOT_ACCEPTABLE`.
- `data.publication` is `verified` or `unverified`. A `verified` answer comes with an `ETag` header. An `unverified` answer is a preview with no `ETag`, so there is nothing to send in `If-Match`: do not write or remove the text from it.
- For the `dwaf` page, a block page setting that cannot be read answers `409 INVALID_STATE`. The text is unavailable. Do not show an empty or default page in its place.

> **Show the text as text**
> The text is HTML or plain text that someone may have written by hand. Show it in an editor or escaped. Never insert it into a page as markup, and never open it as a document.

### Write the text

[`PUT /v2/cdn/custom_pages/{domain_public_id}/pages/{page_id}/content/` in the API reference](https://docs.diamwall.com/api/#tag/custom-pages/PUT/v2/cdn/custom_pages/{domain_public_id}/pages/{page_id}/content/)

The body is the text itself, not JSON. Read the text first to get its `ETag`, then send the new text:

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

**From a file**

```bash
curl -X PUT "$PAGES/pages/502/content/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: text/html" \
  -H "If-Match: $ETAG" \
  --data-binary @502.html
```

**Inline**

```bash
curl -X PUT "$PAGES/pages/502/content/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: text/html; charset=utf-8" \
  -H "If-Match: $ETAG" \
  --data-binary '<!doctype html>
<html>
  <body>
    <h1>We will be right back</h1>
  </body>
</html>'
```

Use `--data-binary`, so `curl` sends the file byte for byte. The text is stored exactly as you send it: nothing is trimmed, rewritten or added.

| Rule | If you break it |
| --- | --- |
| `Content-Type` is `text/html` or `text/plain`, with no charset or with `charset=utf-8` | `415 UNSUPPORTED_MEDIA_TYPE`. This includes `application/json`. |
| The body is not empty | `400 INVALID_REQUEST` |
| The text is valid UTF-8 with no NUL character | `400 INVALID_REQUEST`, with `content` in `fields` |
| At most 1048576 bytes, or 65536 bytes for the `dwaf` page | `413 PAYLOAD_TOO_LARGE` |

The answer is `200` with `revision`, `consistency` and `publication`, and nothing of the page. Read the text again before your next write, and use the `ETag` of that read.

> **Two pages have rules of their own**
> - `5s` must hold exactly one `::CAPTCHABOX::` marker, or exactly one empty element whose id is `root`, and not both. A marker inside a comment or a script does not count. A text that breaks the rule answers `400 INVALID_REQUEST`.
> - `bot-blocked` only takes a write when its `metadata.value.available` is `true`. Otherwise the write is refused with `FILE_SERVER_V2_ERROR`.

### Remove the text

[`DELETE /v2/cdn/custom_pages/{domain_public_id}/pages/{page_id}/content/` in the API reference](https://docs.diamwall.com/api/#tag/custom-pages/DELETE/v2/cdn/custom_pages/{domain_public_id}/pages/{page_id}/content/)

Removing the text puts the default page back. The request takes no body, and its `If-Match` is the `ETag` of a read of the text:

```bash
curl -X DELETE "$PAGES/pages/502/content/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "If-Match: $ETAG"
```

For `bot-blocked` this is the only way to put the default page back. It does not depend on `available`.

## Captcha colors and maintenance

### Read the settings

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

```bash
curl -i "$PAGES/settings/customize/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:66db0466f581...",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "captcha_colors": {
      "state": "resolved",
      "value": { "mclrdw": "0c4a56", "sclrdw": "2ea8c1" }
    },
    "maintenance_declared": {
      "state": "resolved",
      "value": { "all": false, "hosts": ["www.example.com", "shop.example.com"] }
    },
    "maintenance_runtime": {
      "state": "resolved",
      "value": { "all": false, "hosts": [] }
    }
  }
}
```

| Field | What it holds |
| --- | --- |
| `captcha_colors` | `mclrdw`, the main color, and `sclrdw`, the secondary one. Each is six hexadecimal digits, without `#`. |
| `maintenance_declared` | The maintenance mode as it is set: `all` and `hosts`. This is what an edit writes. |
| `maintenance_runtime` | Read-only. The maintenance mode as the CDN read it. It does not confirm that every location applies it. |

How to read `maintenance_declared.value`:

| `all` | `hosts` | Meaning |
| --- | --- | --- |
| `true` | empty | Every host of the domain is in maintenance. |
| `false` | a list | Only the listed hosts are in maintenance. |
| `false` | empty | No host is in maintenance. |

The answer does not list the hosts a domain has. Take them from its [DNS records](https://docs.diamwall.com/products/dns/#list-the-records).

> **unknown is not off**
> - A field whose `state` is `unknown` has no `value`. It is unavailable. Never read it as `false`, as maintenance that is off, or as a default color.
> - When the whole group cannot 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.

### Change the settings

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

The body holds `captcha_colors`, `maintenance` or both, and no other field. A field you leave out keeps its value. Read the settings first to get the `ETag`:

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

**Colors**

```bash
curl -X PATCH "$PAGES/settings/customize/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "captcha_colors": { "mclrdw": "112233", "sclrdw": "e94560" } }'
```

**Some hosts**

```bash
curl -X PATCH "$PAGES/settings/customize/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{
    "maintenance": {
      "all": false,
      "hosts": ["www.example.com", "shop.example.com"]
    }
  }'
```

**Every host**

```bash
curl -X PATCH "$PAGES/settings/customize/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "maintenance": { "all": true, "hosts": [] } }'
```

**Maintenance off**

```bash
curl -X PATCH "$PAGES/settings/customize/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "maintenance": { "all": false, "hosts": [] } }'
```

The answer is the settings as they are after the edit, in the same form as the read, with a new `ETag` header for your next edit.

The rules of the body:

- `captcha_colors` has at least one of `mclrdw` and `sclrdw`. Each is exactly six hexadecimal digits, without `#`. Upper and lower case are both accepted, and the answer shows the color in lower case.
- `maintenance` always has both `all` and `hosts`. `all` is a JSON boolean, not `"true"` or `1`.
- `hosts` is a list of at most 1000 hostnames of this domain, each 1 to 253 characters, none repeated. It is empty when `all` is `true`.
- The body is at most 32768 bytes.
- An empty body, `{}`, answers `400 NO_CHANGES`.

> **The list of hosts is replaced whole**
> The `hosts` you send takes the place of the list the domain has. To add or remove one host, read the settings, change the list from `maintenance_declared.value.hosts`, and send all of it with the `ETag` of that read.

A color edit answers `409 INVALID_STATE` when the domain has no usable pair of colors to change. Nothing is written. This goes with a `captcha_colors` whose `state` is `unknown`: do not offer color editing until the read answers a pair.

## When a write is not confirmed

Some answers to a write do not tell you whether it was made. Do not send the request again blindly after any of them. Read the page (its item and its text) or the settings again, compare with what you meant to write, and only then decide. Each comes with a `request_id` to quote to support.

| Status | Code | What it means |
| --- | --- | --- |
| `503` | `WRITE_VERIFICATION_FAILED` | Something was written, and reading it back did not confirm it. It is not a success and not a refusal. |
| `503` | `PUBLICATION_FAILED` | Settings only. The change could not be published and verified. A rollback is attempted, with no promise that it worked. |
| varies | `FILE_SERVER_V2_ERROR` | An answer this API has no code of its own for. The write may have been refused, or made. |

`503 RUNTIME_UNAVAILABLE` on a write of a text is different: the text could not be checked right now and the page was not changed. Nothing in your request is wrong. Wait, then send the same request again with the same `If-Match`.

## Limits and permissions

- **Plan.** Business and Enterprise only. Any other plan answers `403 FEATURE_NOT_AVAILABLE`, with `details.feature` (`custom_pages`) and `details.plan`. The owner of the domain gets the same answer.
- **Credits.** Each domain has a budget of 3000 credits a minute, shared by everyone who calls the API for that domain. Every call here costs 8 credits. A request that is refused before it reaches the CDN, such as a wrong `page_id`, a missing `If-Match` or a text that is too large, spends none.
- **Out of credits.** The call answers `429 TOO_MANY_REQUESTS`, with the wait in the `Retry-After` header.
- **Size.** A text holds at most 1048576 bytes, or 65536 bytes for the `dwaf` page. A settings body holds at most 32768 bytes and 1000 hosts.
- **Permissions.** They are separate, and one does not include another:

| Action | Permission on the domain |
| --- | --- |
| Read the pages, a text or the settings | `pages.view` |
| Write the text of a page | `pages.create` |
| Change the colors or the maintenance mode | `pages.edit` |
| Delete a page or remove its text | `pages.delete` |

`pages.edit` alone does not allow writing a text, and `pages.create` alone does not allow removing one.

## Errors you are likely to see

| Status | Code | What happened |
| --- | --- | --- |
| `400` | `INVALID_REQUEST` | A field is wrong: a `page_id` that is not one of the nine, a text that is empty or not valid UTF-8, a color, a host. The answer names it in `fields`. |
| `400` | `NO_CHANGES` | A settings edit was sent with `{}`. |
| `400` | `INVALID_PRECONDITION` | `If-Match` is not the `ETag` exactly as it came. |
| `403` | `FEATURE_NOT_AVAILABLE` | The plan of the domain has no custom pages. |
| `404` | `CUSTOM_PAGE_NOT_FOUND`, `RESOURCE_ITEM_NOT_FOUND` | The CDN does not know that page. Nothing was read or written. |
| `404` | `SETTINGS_GROUP_NOT_FOUND` | The settings are unavailable for this domain. This does not mean that maintenance is off. |
| `406` | `NOT_ACCEPTABLE` | The `Accept` header allows no JSON. Send `Accept: application/json`, or no `Accept` header. |
| `409` | `CURSOR_STALE` | The collection changed while you were paging. Start from the first page. |
| `409` | `INVALID_STATE` | The change was refused for the state the settings or the domain are in, or the text of the `dwaf` page cannot be read. Nothing was written. |
| `412` | `REVISION_MISMATCH` | What you are writing changed since your read. Read again and retry. |
| `413` | `PAYLOAD_TOO_LARGE` | The text is above the size limit of the page. |
| `415` | `UNSUPPORTED_MEDIA_TYPE` | A text was sent as something other than `text/html` or `text/plain` in UTF-8, or a settings body was not sent as JSON. |
| `428` | `PRECONDITION_REQUIRED` | The `If-Match` header is missing. |
| `429` | `TOO_MANY_REQUESTS` | The domain is out of credits for now. |
| `503` | `RUNTIME_UNAVAILABLE` | The CDN cannot check a text right now. The page was not changed. |
| `503` | `WRITE_VERIFICATION_FAILED`, `PUBLICATION_FAILED` | The write is not confirmed. See [When a write is not confirmed](#when-a-write-is-not-confirmed). |

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 Custom pages 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/custom-pages) for every endpoint, with parameters and responses.
