# Network

Manage the origin servers of a domain, its gzip and Brotli compression and the largest upload it accepts.

The Network endpoints control where the network sends the traffic of a domain (its origins), how it compresses the answers, and how large an upload can be. Everything works on one domain at a time, and the paths start with `/v2/cdn/network/{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).

- [Origins](#origins): List, create, edit and delete the servers behind each record.
- [Origins by hostname](#origins-by-hostname): Read each hostname with the servers it is served from.
- [Compression](#compression): Turn gzip and Brotli on or off and set their level.
- [Upload size](#upload-size): Read and set the largest body a visitor may upload.

The examples on this page use these shell variables:

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

## Origins

An origin is a server that one DNS record of the domain is served from. It has two fields you can write:

| Field | What it holds |
| --- | --- |
| `record` | The DNS record the origin serves: `@` for the domain itself, `*` for every name under it, or a name relative to the domain such as `www` or `api.dev`. A wildcard is only allowed as the whole first label, as in `*.dev`. |
| `address` | Where the network sends the requests of that record: an IPv4 address, an IPv6 address or a hostname. No port, no scheme and no path. |

A read also answers the `id` of the origin, the `hostname` the record is served under, a read-only `port`, and `mutable`, `state` and `reason`.

> **The port is never sent**
> The port follows the SSL mode of the domain. You cannot set it: a body with `port` is refused with `400 INVALID_REQUEST`. When a read has no `port`, treat it as unavailable and assume no default.

### Writes need the latest revision

Every read of the origins answers an `ETag` header. It is the revision of the whole collection, not of one origin. Each create, edit and delete sends it back in `If-Match`, exactly as it came, quotes included.

- Without `If-Match` the write answers `428`.
- If the origins changed since your read, the write answers `412 REVISION_MISMATCH`. Read again and retry.
- Every successful write answers a new `ETag`. Use it for your next write.

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

A domain with no origin still answers an `ETag`. That is the one you send to create the first origin.

### List the origins

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

```bash
curl "$NETWORK/origins/?limit=50" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:70283828807995825335015f10a1fbef9d4fb9cc1102272189e5a613b7ad7044",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "items": [
      {
        "id": "o1AbCdEfGhIjKlMnOpQrStUv",
        "record": "@",
        "address": "192.0.2.20",
        "hostname": "example.com",
        "mutable": true,
        "state": "verified",
        "port": 443
      }
    ],
    "page": { "limit": 50, "next_cursor": null, "total": 1 }
  }
}
```

- `limit` goes from 1 to 100. Without it a page holds 50 origins.
- To read the next page, send `data.page.next_cursor` back as `cursor`, until it is `null`.
- `data.page.total` is the number of origins of the domain, on every page together.
- If the origins change between two pages, the next page answers `409 CURSOR_STALE`. Start again from the first page, without `cursor`.
- An empty `data.items` means the domain has no origin.
- No other query parameter is accepted.

> **Some origins are read-only**
> An origin with `mutable: false` is shown with `state: "unknown"` and a `reason`, usually in an answer with `consistency: "degraded"`. You can read it, but an edit or a delete of it answers `409 AMBIGUOUS_RESOURCE_STATE` and writes nothing. Do not send that request again. The other origins of the domain are edited as usual.

### Read one origin

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

```bash
curl "$NETWORK/origins/o1AbCdEfGhIjKlMnOpQrStUv/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

The answer has the origin under `data`, in the same form as an item of the list. Its `ETag` is also the revision of the whole collection, so you can use it for an edit or a delete. An id that no origin has answers `404 RESOURCE_ITEM_NOT_FOUND`: the origin may have been deleted, so read the list again.

### Add an origin

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

The body is exactly `address` and `record`. Send the `ETag` of a read of the list in `If-Match`.

**IPv4, the domain itself**

```bash
curl -X POST "$NETWORK/origins/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "address": "192.0.2.20", "record": "@" }'
```

**Hostname**

```bash
curl -X POST "$NETWORK/origins/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "address": "origin.example.net", "record": "www" }'
```

**IPv6, wildcard**

```bash
curl -X POST "$NETWORK/origins/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "address": "2001:db8::20", "record": "*" }'
```

The answer is `201` with the new origin under `data`, and a new `ETag` header. `operation.id` is the id of the new origin: use it to read, edit or delete it. The other origins of the domain are not part of the answer.

```json
{
  "revision": "sha256:16019284eee5c8cdb06e6b9fa9da4b9ae40853f411fb7adfb77fc79b5e197975",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "id": "o1AbCdEfGhIjKlMnOpQrStUv",
    "record": "@",
    "address": "192.0.2.20",
    "hostname": "example.com",
    "mutable": true,
    "state": "verified",
    "port": 443
  },
  "operation": { "type": "create", "warning_count": 0, "id": "o1AbCdEfGhIjKlMnOpQrStUv" }
}
```

Both fields are strings and are taken exactly as sent: nothing is trimmed or put in lower case. A field in the wrong form answers `400 INVALID_REQUEST` and names it in `fields`. A record that does not exist, an address the CDN does not serve or a duplicate is refused with `400 INVALID_REQUEST` or `409 RESOURCE_CONFLICT`, and nothing is written.

### Change an origin

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

Send `address`, `record` or both. A field you leave out keeps its value.

```bash
curl -X PATCH "$NETWORK/origins/o1AbCdEfGhIjKlMnOpQrStUv/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $ETAG" \
  -d '{ "address": "192.0.2.21" }'
```

The answer is the origin as it is after the edit, under `data`, with a new `ETag`. An empty body, `{}`, answers `400 NO_CHANGES`.

### Delete an origin

[`DELETE /v2/cdn/network/{domain_public_id}/origins/{origin_id}/` in the API reference](https://docs.diamwall.com/api/#tag/network/DELETE/v2/cdn/network/{domain_public_id}/origins/{origin_id}/)

The request takes no body.

```bash
curl -X DELETE "$NETWORK/origins/o1AbCdEfGhIjKlMnOpQrStUv/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "If-Match: $ETAG"
```

The answer is `200` with no `data`. `operation.id` is the id the origin had, and the `ETag` header is the new revision. Read the list to see the origins that are left.

> **When a write is not confirmed**
> After `502 FILE_SERVER_OUTCOME_UNKNOWN`, `503 PUBLICATION_FAILED`, `500 UNEXPECTED_ERROR` or any `FILE_SERVER_V2_ERROR`, the write may have been made. Read the origins again before you try anything else. Never repeat the request blindly.

## Origins by hostname

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

`GET .../settings/origins/` answers the same origins grouped by hostname: one entry for each hostname the domain serves through the network, with its `record` and its `servers`.

```bash
curl "$NETWORK/settings/origins/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

```json
{
  "revision": "sha256:cbd91b272fb630a299f3621539c12313b9cdd0806e4cc2adc061dbfccdd21a72",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "origins": {
      "state": "resolved",
      "value": [
        {
          "hostname": "www.example.com",
          "record": "www",
          "servers": [
            { "address": "origin.example.net", "port": 443 },
            { "address": "192.0.2.21", "port": 443 }
          ]
        }
      ]
    }
  }
}
```

- This view is read-only and its answer has no `ETag`. To write an origin, use the [origins](#origins) endpoints and the `ETag` of the list.
- An empty `value` with `state: "resolved"` means the domain has no origin. A `state` of `unknown` never means that.

## Compression

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

A domain can compress its answers with gzip and with Brotli. Each compressor has two settings:

| Compressor | `enabled` | `level` |
| --- | --- | --- |
| `gzip` | `true` or `false` | A whole number from 1 to 6 |
| `brotli` | `true` or `false` | A whole number from 1 to 4 |

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

```json
{
  "revision": "sha256:4ba85940c78e38cd60ef7947c1089c71e9aec060866efeab604b4fded72403fe",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "gzip": {
      "enabled": { "state": "resolved", "value": true },
      "level": { "state": "resolved", "value": 4 }
    },
    "brotli": {
      "enabled": { "state": "resolved", "value": true },
      "level": { "state": "resolved", "value": 3 }
    }
  }
}
```

Each of the four settings comes back as `{ "state": "resolved", "value": ... }`. A `state` of `unknown` has no `value`: the CDN could not tell. Treat it as unavailable, never as `false`, `0` or a default. A compressor or a setting that is missing from the answer is also unavailable, not off.

A read can show a stored level above the range an edit accepts. It is shown as it is, but you cannot set it again.

The `ETag` header of this answer is the revision of the compression settings. Keep it for the edit.

### Change gzip or Brotli

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

The body holds exactly one compressor, `gzip` or `brotli`, with `enabled`, `level` or both. Whatever you leave out keeps its value, and a missing `enabled` is never read as `false`.

**Set the gzip level**

```bash
curl -X PATCH "$NETWORK/settings/compression/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $COMPRESSION_ETAG" \
  -d '{ "gzip": { "level": 6 } }'
```

**Turn Brotli off**

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

**Turn gzip on with a level**

```bash
curl -X PATCH "$NETWORK/settings/compression/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $COMPRESSION_ETAG" \
  -d '{ "gzip": { "enabled": true, "level": 5 } }'
```

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

- To change both compressors, send two requests. The second takes the `ETag` the first answered.
- A body with both compressors answers `400 INVALID_REQUEST` and names both in `fields`.
- `enabled` must be a JSON boolean, not `"true"`, `1` or `null`.
- An empty body, `{}`, answers `400 NO_CHANGES`.

## Upload size

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

`max_upload_size` is the largest body a visitor may upload through the domain.

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

```json
{
  "revision": "sha256:b7062b3baa86351b2eef85fb959bff47e468a032f5ec9942da064ae22015951c",
  "consistency": "verified",
  "publication": { "status": "verified", "rollout": "unknown" },
  "data": {
    "max_upload_size": { "state": "resolved", "value": "200M" }
  }
}
```

A domain can hold a size outside the three that an edit accepts, such as `1024m` or `0`. The read answers it as it is. You cannot set such a size again: the only change is a move to `200M`, `500M` or `1G`.

### Set the upload size

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

The body is exactly `max_upload_size`, as a string. Send the `ETag` of the read in `If-Match`.

```bash
curl -X PATCH "$NETWORK/settings/network/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -H "If-Match: $NETWORK_ETAG" \
  -d '{ "max_upload_size": "500M" }'
```

| Value | Size | Lowest plan that allows it |
| --- | --- | --- |
| `200M` | 200 MB | Pro |
| `500M` | 500 MB | Business |
| `1G` | 1 GB | Enterprise |

The values are also accepted in lower case (`200m`, `500m`, `1g`). Any other size, a number of bytes included, answers `400 INVALID_REQUEST`.

A size above what the plan of the domain allows answers `400 PLAN_LIMIT_EXCEEDED`, and nothing changes:

```json
{
  "message": "PLAN_LIMIT_EXCEEDED",
  "fields": ["max_upload_size"],
  "details": { "plan": "pro", "feature": "network_upload" }
}
```

Sending the same request again answers the same until the plan changes.

## When a setting cannot be read

Any of the three settings reads can answer `200` with nothing to show:

```json
{
  "revision": null,
  "consistency": "degraded",
  "publication": { "status": "verified", "rollout": "unknown" },
  "state": "unknown"
}
```

The CDN could not read the settings. There is no `data` and no `ETag`, so there is nothing to send in `If-Match`. Treat the settings as unavailable, not as empty or off, and do not write until a read answers an `ETag`.

In every answer, `publication.rollout` is always `unknown`: the answer does not say whether the change has reached every point of presence.

## Limits and permissions

- **Credits.** Each domain has a budget of 3000 credits a minute, shared by everyone who calls the API for that domain. Listing the origins or reading one costs 6 credits. Every other Network call costs 8. A request the API refuses by itself, such as a `400`, a `403` or a `428`, spends none.
- **Out of credits.** The call answers `429 TOO_MANY_REQUESTS`, with the wait in `details.retry_after_seconds`.
- **Origins per domain.** A domain holds at most 1024 origin servers. This is not a plan limit. A create above it is refused with `400 INVALID_REQUEST`.
- **Upload size.** The largest size depends on the plan: 200 MB on Pro, 500 MB on Business, 1 GB on Enterprise.
- **Permissions.** Each area takes a different permission on the domain:

| Area | Read | Write |
| --- | --- | --- |
| Origins, origins by hostname | `dns.view` | `dns.create` to add, `dns.edit` to change, `dns.delete` to delete |
| Compression | `speed.view` | `speed.edit` |
| Upload size | `network.view` | `network.edit` |

Origins take the `dns` permissions because an origin belongs to a DNS record. The `network` permissions alone do not give access to the origins: those calls answer `403`.

## Errors you are likely to see

| Status | Code | What happened |
| --- | --- | --- |
| `400` | `INVALID_REQUEST` | A field is wrong, and the answer names it in `fields`. With no `fields`: the body is missing, is not a JSON object, or has a field that is not accepted. With a `request_id`: the CDN refused the value. |
| `400` | `NO_CHANGES` | An edit was sent with an empty body. |
| `400` | `INVALID_PRECONDITION` | The `If-Match` header is not in the form of the `ETag` that a read answers. |
| `400` | `PLAN_LIMIT_EXCEEDED` | The upload size is above what the plan of the domain allows. |
| `404` | `RESOURCE_ITEM_NOT_FOUND` | No origin of the domain has that `id`. |
| `404` | `SETTINGS_GROUP_NOT_FOUND` | The CDN does not have these settings for the domain. |
| `406` | `NOT_ACCEPTABLE` | The `Accept` header of the request does not allow JSON. |
| `409` | `CURSOR_STALE` | The origins changed while you were paging. Start from the first page. |
| `409` | `RESOURCE_CONFLICT` | The CDN refused the origin as things are, for example a duplicate. Nothing was written. |
| `409` | `AMBIGUOUS_RESOURCE_STATE` | The origin is read-only (`mutable: false`). Do not send the request again. |
| `409` | `DOMAIN_BUSY` | Something else is changing the domain. Wait a few seconds and send the same request again, with the same `If-Match`. |
| `409` | `INVALID_STATE` | The CDN refused a settings change for the state it found. The change was not made. Read the settings again. |
| `412` | `REVISION_MISMATCH` | What you are writing changed since your read. Read again and retry. |
| `428` | `PRECONDITION_REQUIRED` | The `If-Match` header is missing. |
| `429` | `TOO_MANY_REQUESTS` | The domain is out of credits for now. |
| `503` | `PUBLICATION_FAILED` | The CDN could not publish and verify the change. Read the current state before you try again. |

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 Network 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/network) for every endpoint, with parameters and responses.
