# Add a domain to a service

Source: https://openpanel.sh/docs/api/domains/add-a-domain-to-a-service

A name under opnl.app works at once. Your own domain works once its DNS records are in place: a CNAME to edge.openpanel.sh and a TXT record that proves it is yours (both in the answer's records).

`POST https://openpanel.sh/api/v1/services/{serviceId}/domains`

## Parameters

| Name | In | Required | Description |
|---|---|---|---|
| `serviceId` | path | yes | The service's id. |
| `Idempotency-Key` | header | no | Any unique string up to 255 characters. Sending the same request again with it returns the first answer instead of creating twice. |

## Request body

JSON, `DomainCreate`.

## Responses

| Status | Meaning | Body |
|---|---|---|
| 201 | Add a domain to a service | `application/json`, `Domain` |
| 401 | No valid credential was sent. | `application/problem+json`, `Problem` |
| 403 | The credential may not do this. | `application/problem+json`, `Problem` |
| 404 | Not found, or not visible to this credential. | `application/problem+json`, `Problem` |
| 409 | It clashes with something that exists, such as a name in use. | `application/problem+json`, `Problem` |
| 422 | The input was refused; detail says why. | `application/problem+json`, `Problem` |
| 429 | Too many requests; wait for Retry-After seconds. | `application/problem+json`, `Problem` |

## Schemas

### DnsRecord

```json
{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "enum": [
        "CNAME",
        "TXT"
      ]
    },
    "name": {
      "type": "string"
    },
    "value": {
      "type": "string"
    },
    "purpose": {
      "type": "string"
    }
  },
  "required": [
    "type",
    "name",
    "value",
    "purpose"
  ]
}
```

### Domain

```json
{
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "uuid"
    },
    "serviceId": {
      "type": "string",
      "format": "uuid"
    },
    "projectId": {
      "type": "string",
      "format": "uuid"
    },
    "host": {
      "type": "string"
    },
    "kind": {
      "type": "string",
      "enum": [
        "opnl",
        "custom"
      ]
    },
    "port": {
      "type": "integer"
    },
    "verifyToken": {
      "type": [
        "string",
        "null"
      ]
    },
    "verifiedAt": {
      "anyOf": [
        {
          "type": "string",
          "format": "date-time"
        },
        {
          "type": "null"
        }
      ]
    },
    "certificateSource": {
      "type": "string",
      "enum": [
        "auto",
        "uploaded"
      ],
      "description": "uploaded: the edge serves the certificate you uploaded for this domain."
    },
    "certificateExpiresAt": {
      "anyOf": [
        {
          "type": "string",
          "format": "date-time"
        },
        {
          "type": "null"
        }
      ],
      "description": "When the uploaded certificate expires."
    },
    "dnsZoneId": {
      "anyOf": [
        {
          "type": "string",
          "format": "uuid"
        },
        {
          "type": "null"
        }
      ],
      "description": "The connected Cloudflare zone in which OpenPanel wrote this domain's records; null when you add them yourself."
    },
    "dnsProblem": {
      "description": "Why OpenPanel could not write a record in that zone, such as a name that already has one.",
      "type": [
        "string",
        "null"
      ]
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "records": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/DnsRecord"
      },
      "description": "For your own domain: the DNS records to add."
    }
  },
  "required": [
    "id",
    "serviceId",
    "projectId",
    "host",
    "kind",
    "port",
    "verifyToken",
    "verifiedAt",
    "certificateSource",
    "certificateExpiresAt",
    "dnsZoneId",
    "dnsProblem",
    "createdAt",
    "records"
  ]
}
```

### DomainCreate

```json
{
  "type": "object",
  "properties": {
    "host": {
      "type": "string",
      "maxLength": 253,
      "description": "A name under opnl.app (shop.opnl.app) or your own domain (app.example.com)."
    },
    "port": {
      "description": "Which HTTP port of the service it reaches. Default its first.",
      "type": "integer",
      "minimum": 1,
      "maximum": 65535
    }
  },
  "required": [
    "host"
  ],
  "additionalProperties": false
}
```

### Problem

```json
{
  "type": "object",
  "properties": {
    "type": {
      "type": "string",
      "description": "A link to the error's explanation in the docs."
    },
    "title": {
      "type": "string"
    },
    "status": {
      "type": "integer"
    },
    "code": {
      "type": "string",
      "description": "Stable machine-readable code: not_found, forbidden, invalid, conflict, rate_limited, not_implemented, …"
    },
    "detail": {
      "type": "string",
      "description": "What went wrong, in a sentence written for a person."
    }
  },
  "required": [
    "type",
    "title",
    "status",
    "code",
    "detail"
  ]
}
```