# Domains

List the domains a key can reach, register a new Enterprise domain, confirm its setup, copy settings from another domain and delete it.

A domain is where everything else in the API starts: its `public_id` is part of the path of every DNS and CDN call. This page covers the whole life of a domain, from registering it to deleting it.

- [List](#list-your-domains): The domains this key can reach, with their public ids.
- [Register](#register-a-domain): Add an Enterprise domain to your account.
- [Complete setup](#complete-the-setup): Confirm the name servers, so the domain goes active.
- [Copy settings](#copy-settings-from-another-domain): Bring the settings and rules of another domain.
- [Delete](#delete-a-domain): Remove a domain, with its name as confirmation.

The examples on this page use these shell variables:

```bash
export DIAMWALL_API_KEY="your key"
export API="https://api.diamwall.com/public-api"
```

## Who can do what

| Action | What the key needs |
| --- | --- |
| List | Any active key. A restricted key only sees its own domains. |
| Register | A partner account with Enterprise capacity, and a key with domain creation enabled. |
| Complete setup | A key with domain creation enabled, on an account that owns the domain. |
| Copy settings | A key with domain creation enabled. Both domains are Enterprise, have the same owner and are assigned to the key. |
| Delete | A key with domain deletion enabled, with the domain assigned to that key, on the account that owns it. |

A key that lacks the permission answers `403`. See [API keys](https://docs.diamwall.com/guides/api-keys/) and [IP and domain restrictions](https://docs.diamwall.com/guides/restrictions/).

## The status of a domain

Register and complete setup both answer with the domain's `public_id` and its `status`, as a number:

| `status` | Meaning |
| --- | --- |
| `1` | Active |
| `2` | Config |
| `3` | Moved |
| `4` | Suspended |
| `5` | Terminated |
| `6` | Blocked |
| `7` | Setup: registered, waiting for its name servers to be confirmed |

A new domain starts at `7` and becomes `1` when its setup is complete.

## List your domains

[`GET /public-api/domains/` in the API reference](https://docs.diamwall.com/api/#tag/domains/GET/public-api/domains/)

```bash
curl "$API/domains/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

The answer is a paginated list, sorted by name. Each entry has the `name` and the `public_id` of a domain. Add `?name=example.com` to find one domain, and `?page=2` for the next page.

## Register a domain

[`POST /public-api/domains/` in the API reference](https://docs.diamwall.com/api/#tag/domains/POST/public-api/domains/)

Send the name in `domain`, at most 100 characters:

```bash
curl -X POST "$API/domains/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "domain": "example.com" }'
```

A new domain answers `201`:

```json
{ "public_id": "acb66b15-da87-422a-8774-825ae2a8cb8c", "status": 7 }
```

The domain belongs to your account and is assigned to the key that created it. Keep the `public_id`: every other call for this domain needs it.

> **Safe to repeat**
> If the request is cut off, or answers `502`, send the same request again with the same key. It picks the creation up where it stopped and answers `200` with the current status, instead of creating a second domain.

## Complete the setup

[`POST /public-api/domains/{domain_public_id}/complete-setup/` in the API reference](https://docs.diamwall.com/api/#tag/domains/POST/public-api/domains/{domain_public_id}/complete-setup/)

A registered domain waits in status `7` until both of its name servers are confirmed. Once the domain is delegated to them, ask for the check:

```bash
curl -X POST "$API/domains/acb66b15-da87-422a-8774-825ae2a8cb8c/complete-setup/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY"
```

The request needs no body. An empty JSON object, `{}`, is also accepted. When both name servers are confirmed the answer is `200` with `"status": 1`, and the domain is active.

Repeating the request runs the check again, so it is safe to retry while the delegation spreads.

## Copy settings from another domain

[`POST /public-api/domains/{domain_public_id}/copy-settings/` in the API reference](https://docs.diamwall.com/api/#tag/domains/POST/public-api/domains/{domain_public_id}/copy-settings/)

To set up a new domain like one you already have, copy the settings and rules across. The domain in the path receives them. The domain in `source_domain_public_id` is read and never changed.

```bash
curl -X POST "$API/domains/acb66b15-da87-422a-8774-825ae2a8cb8c/copy-settings/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "source_domain_public_id": "684393bc-679d-4779-a802-12cab3769191" }'
```

| Copied | Not copied |
| --- | --- |
| Network, compression, cache and TLS settings | Origins |
| Cache rules, redirect rules, request transforms and response transforms | DNS |
| Firewall rules and firewall access rules | Certificates and mTLS |
| WAF rule exceptions | Credentials |

The copy runs group by group, in this order: `network`, `compression`, `cache`, `tls`, `cache-rules`, `redirect-rules`, `request-transforms`, `response-transforms`, `firewall-rules`, `firewall-access-rules`, `waf-rule-exceptions`. A complete copy answers `200` with one entry for each group, `applied` when something changed and `unchanged` when the target already matched:

```json
{
  "groups": [
    { "group": "network", "status": "applied" },
    { "group": "compression", "status": "unchanged" }
  ]
}
```

Repeating the request is safe. It creates no duplicates and deletes nothing: rules the target already has are reused, and they keep their order.

> **Credits**
> The copy reads the source and writes the target through the same API as everything else, so it spends credits on both domains.

### When the copy is incomplete

A copy that could not finish answers `COPY_INCOMPLETE`, with the status of the failure that stopped it: `400`, `409`, `412`, `429` with a `Retry-After` header, `502` or `503`. The body says how far it got:

```json
{
  "message": "COPY_INCOMPLETE",
  "groups": [
    { "group": "network", "status": "applied" },
    { "group": "compression", "status": "unchanged" },
    { "group": "cache", "status": "unchanged" },
    { "group": "tls", "status": "failed", "code": "COPY_TLS_PROFILE_UNSUPPORTED" }
  ],
  "not_run": ["cache-rules", "redirect-rules", "request-transforms", "response-transforms", "firewall-rules", "firewall-access-rules", "waf-rule-exceptions"]
}
```

- `groups` lists what ran. A failed group has a `code`.
- `not_run` lists the groups that were not reached. Fix the cause and send the same request again: the groups already applied come back as `unchanged`.
- A rule that cannot be copied is listed in `omitted` of its group, with a `reference` (the public id of the rule, or its position in the source list, counting from 0) and a `code`. The other rules of that group are still copied, and later groups still run.

| `omitted` code | Meaning |
| --- | --- |
| `COPY_RULE_CITES_SOURCE` | The rule refers to the source domain itself. |
| `COPY_RULE_NOT_PORTABLE` | The rule cannot be moved to another domain. |
| `COPY_WAF_RULE_UNKNOWN` | The exception is for a WAF rule the target does not have. |

## Delete a domain

[`DELETE /public-api/domains/{domain_public_id}/` in the API reference](https://docs.diamwall.com/api/#tag/domains/DELETE/public-api/domains/{domain_public_id}/)

A delete needs the `X-Confirm-Domain` header with the name of the domain, exactly as it is stored, upper and lower case included:

```bash
curl -X DELETE "$API/domains/acb66b15-da87-422a-8774-825ae2a8cb8c/" \
  -H "Authorization: Api-Key $DIAMWALL_API_KEY" \
  -H "X-Confirm-Domain: example.com"
```

A deleted domain answers `204` with no body.

> **Before you delete**
> - A domain with a subscription cannot be deleted. Cancel the subscription first.
> - A domain that is still in setup must have its setup completed before it can be deleted.
> - A missing or different `X-Confirm-Domain` answers `400 DOMAIN_CONFIRMATION_REQUIRED` and nothing is deleted.

## Errors you are likely to see

The code is in the `message` field of the answer.

| Status | Code | Where | What happened |
| --- | --- | --- | --- |
| `400` | `DOMAIN_INVALID_FORMAT`, `DOMAIN_INVALID_CHARACTERS`, `DOMAIN_TOO_SHORT` | Register | The name is not a valid domain name. |
| `400` | `DOMAIN_INVALID_OR_UNSUPPORTED_TLD` | Register | The ending of the name is not valid or not supported. |
| `400` | `DOMAIN_SUBDOMAIN_NOT_ALLOWED`, `DOMAIN_USE_ROOT` | Register | A subdomain was sent. Register the root domain. |
| `400` | `DOMAIN_CONFIRMATION_REQUIRED` | Delete | `X-Confirm-Domain` is missing or does not match the stored name. |
| `403` | `API_KEY_ENDPOINT_FORBIDDEN` | All | The key is not allowed to call this operation. |
| `403` | `API_KEY_ACCESS_DENIED` | Setup, delete | The key cannot act on this domain. |
| `404` | `DOMAIN_NOT_FOUND` | Setup, delete | No domain has that public id. |
| `409` | `DOMAIN_ALREADY_REGISTERED`, `DOMAIN_ALREADY_ACTIVE` | Register | The domain already exists. |
| `409` | `DOMAIN_PARENT_ALREADY_ACTIVE`, `DOMAIN_DESCENDANT_ALREADY_ACTIVE` | Register, delete | A domain above or below this one is already active. |
| `409` | `PARTNER_ENTERPRISE_QUOTA_EXCEEDED` | Register | The account has no Enterprise capacity left. |
| `409` | `DOMAIN_SETUP_NOT_ALLOWED` | Register, setup | The setup cannot be confirmed for this domain in its current state. |
| `409` | `DOMAIN_SETUP_REQUIRED` | Delete | The setup is still pending. Complete it first. |
| `409` | `DOMAIN_HAS_SUBSCRIPTIONS` | Delete | The domain has a subscription. |
| `409` | `COPY_SOURCE_IS_TARGET` | Copy settings | The source and the target are the same domain. |
| any | `COPY_INCOMPLETE` | Copy settings | The copy stopped part of the way. The body says which groups ran. |
| `409` | `DOMAIN_BUSY` | All writes | Another operation is running on the domain. Try again shortly. |
| `502` | `DOMAIN_CREATION_UNCONFIRMED`, `DOMAIN_SETUP_UNCONFIRMED` | Register, setup | The result could not be confirmed. Repeat the same request. |
| `502` | `DOMAIN_TERMINATION_UNCONFIRMED` | Delete | The delete could not be confirmed. List your domains to see whether it is gone. |

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