
# Custom domains

`agc domain add example.com` asks a person to approve serving the app at `example.com` and answers with two DNS records to set. Once they approve and the records resolve, Agent Cloud verifies the domain, issues it a certificate and routes it to the live release.

Success: `agc domain` shows `state` `active`, and `https://example.com` serves the same release as the app's Agent Cloud address, which keeps working. To move that `<label>.ac0.app` address itself, see [Changing an app's address](#changing-an-apps-address).

## Ask your agent

```text
Serve camp-bookings at camps.example.test as well as its Agent Cloud address.
Tell me exactly which DNS records to add.
```

You get an approval question, in the session or as a console link, listing the two records. Set them at your DNS provider; Agent Cloud does the rest.

## Steps

```bash
agc domain add camps.example.test
```

It exits 10 with `domain.approval_required`, a `decision` and the two records ([envelope](#errors)). Put the decision to the person as [approving in a session](/docs/ship/approving-in-a-session-vs-the-console.md) describes. The person sets both records at the domain's DNS provider: the TXT `_agc.<domain>` = `agc-app=<app id>`, and a CNAME from `<domain>` to the app's address, **DNS only, not proxied**. At a zone apex, use the provider's CNAME flattening, ALIAS or ANAME. Any CAA records must allow `letsencrypt.org`. Then follow it with `agc domain` and `agc status`. To stop serving it, `agc domain remove camps.example.test` asks a person the same way.

## How it works

| `state` | `step` | What's happening |
|---|---|---|
| `approval` | waiting for a person to approve it | Nothing is checked or routed yet |
| `pending` | waiting for its DNS records (see reason and records) | Checked every minute; `reason` says what's missing |
| `verified` | DNS is right; its certificate is being issued | Routed to the live release with its own certificate |
| `active` | serving the app with its own certificate | `https://<domain>` proved it serves the live version |

**Verification.** The TXT must say exactly `agc-app=<app id>`, which proves whoever runs the zone means this app. The domain's A and AAAA answers, as sets, must equal the app address's. A proxied record resolves to the proxy and stays `pending`: the certificate comes from Let's Encrypt over HTTP-01, and Agent Cloud terminates TLS itself.

**Ownership.** Waiting entries hold nothing, so several apps can ask for the same name. The first app whose TXT verifies gets it, and every other app's waiting entry is dropped, as is any entry not verified within 7 days.

**Both hosts serve.** Adding, verifying or removing a domain never redirects, moves or restarts the app. Every release re-applies the domain routes last, and an address move keeps them. To make the domain the main address, 301 the Agent Cloud address's pages to it yourself, but leave webhook and OAuth callback paths answering there.

**Sign-in on the domain.** [Agent Cloud sign-in](/docs/build/choosing-a-sign-in-shape.md#agent-cloud-sign-in-shape-2) returns to the app's Agent Cloud address unless the app passes the host its page was requested on. Without that, a sign-in started on the domain comes back and finds no state cookie:

```ts
const host = new URL(c.req.url).host; // the app's address or one of its active domains
return c.redirect(`${AUTH_URL}/authorize?state=${state}&host=${encodeURIComponent(host)}`);
```

A host that isn't the address or an active domain at that moment gets a 400 page and nothing starts. `/token` works the same from either host. An app with its own sign-in buttons passes the same `host` in its `/stytch/google` and `/stytch/email` calls; a bad one answers 400 `auth.app_host_invalid`.

**Who decides.** Only an owner of the app's organization approves, and can also add, approve or remove domains in the app's Settings, "Domains", which shows the records with copy buttons.

## Changing an app's address

An app's address, like `sales-call-feed.ac0.app`, can move to another label under `ac0.app`. A person moves it under the app's Settings, "Move the address". `agc address <label>` only answers with that console link, label filled in. Success: the app answers at the new address, the old one redirects to the same path, and `agc status` shows the new `prod.url`.

An app still at a former `<label>.agent.cloud` address moves to `ac0.app` the same way, and the old address redirects.

The app's name, namespace, repo, database, receipts and `[app] name` don't change.

```bash
agc address summer-camps
```

```json
{
  "ok": true,
  "product": "agent.cloud",
  "command": "address",
  "data": {
    "host": "camp-bookings.ac0.app",
    "old_hosts": [],
    "requested": "summer-camps",
    "change_url": "https://agent.cloud/console/apps/camp-bookings/settings?address=summer-camps#address",
    "note": "Give this link to the person; they move the address there, and the old one keeps redirecting. agc changed nothing."
  },
  "next": [{ "cmd": "agc status", "why": "the app’s address, once the person has moved it" }]
}
```

`old_hosts` are former addresses that redirect to `host`. A label is 3 to 40 lowercase letters, digits and hyphens, starting with a letter and ending with a letter or digit. It can be the app's own name or one of its former addresses, so an address can move back. A label holding `-dom-` followed by 12 hex digits is refused with a 400 "The address didn't move" page, because custom-domain routes use that shape; nothing changes. When the person moves it:

| Step | What happens |
|---|---|
| Submit | The app's owner, or an owner of its organization, submits the label in the console. No agent credential can change an address |
| Claim | Under a lock, Agent Cloud checks the label is free, makes it the address, and adds the old one to the former addresses, which stay held: no other app can take an address an app has, or had and still redirects from |
| Route | The live release's route moves to the new address, with the same gate and origin check, and every former address gets a permanent redirect to the same path and query, with `Cache-Control: no-store` so moving back can't loop a browser. No deployment is created, stopped or restarted, and no release is made |
| Prove | The move counts once the new address serves the live version. If it doesn't within 90 seconds, or Kubernetes refuses a step, every route is put back and the operation fails as `address.failed` |

| After a move | |
|---|---|
| Sign-in | Returns people to the new address, but sessions belong to a host, so everyone signs in once more |
| Webhooks and OAuth callbacks | Update them at the provider: many senders don't follow the old address's redirect |
| Targets | An app on a platform target at a flat address under `ac0.app` or the former `agent.cloud` moves the same way, after Agent Cloud has written and confirmed the new address's DNS record (`address.dns` if it can't) |

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md). The console refuses an address move, with a message, when:

- the address belongs to another app or is reserved;
- a release or another change is in progress;
- the app has no live release yet ("Ship it first");
- the app runs on an organization's target.

Exit 10 from an agent's `agc domain add` or `remove` is the normal path, not a failure:

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "domain add",
  "data": {
    "domain": "camps.example.test",
    "state": "approval",
    "records": [
      { "type": "TXT", "name": "_agc.camps.example.test", "value": "agc-app=<app id>" },
      { "type": "CNAME", "name": "camps.example.test", "value": "camp-bookings.ac0.app", "proxied": false }
    ],
    "step": "waiting for a person to approve it",
    "op": "op_…",
    "decision": { "id": "…", "word": "…", "in_session": true, "summary": "…", "reason": "…" }
  },
  "error": {
    "code": "domain.approval_required",
    "message": "A person must approve serving the app at camps.example.test: …",
    "fix": "Ask with your harness's question tool when available: decision.summary word for word, …",
    "retryable": false,
    "docs": "agc help domains"
  },
  "next": [
    { "cmd": "agc approve <id> --word <word>", "why": "only after the person answers this decision" },
    { "cmd": "agc domain", "why": "each domain’s step, once the person has decided" }
  ]
}
```

(Abbreviated: records also carry a `why`; `data` also has `console_url`, `note` and `approval`.)

| Code | Exit | Cause | Fix |
|---|---|---|---|
| `domain.bad_name` | 2 | Not a domain name | A name like `example.com` or `app.example.com` |
| `domain.taken` | 1 | Already this app's, or another app's | `agc domain`; or remove it from the other app |
| `domain.too_many` | 1 | The app has five | Remove one first |
| `domain.unclaimed` | 1 | No organization has claimed the app | Ship its first release, then ask |
| `domain.edge` | 1 | The app is behind the [edge gate](/docs/build/the-edge-gate.md) | Use Agent Cloud sign-in, or keep the address |
| `domain.target` | 1 | The app's runtime isn't ready for domains | Keep the address until its runtime is upgraded |

## Limits

Five domains per app. A domain's approval expires after 24 hours, and the entry goes with it; a domain whose DNS isn't set within 7 days is dropped.

## Agent notes

- Add domains with `agc domain add`, never in `agentcloud.toml`: they're platform state, not part of a snapshot.
- An address move happens only in the console, through the `change_url` link.
- `agc domain` only reads the stored state; it doesn't trigger a check.
- `agc address` never changes anything, even with a valid, free label. Don't report the address as moved until `agc status` shows it.

## Related

- [Agent Cloud sign-in (shape 2)](/docs/build/choosing-a-sign-in-shape.md#agent-cloud-sign-in-shape-2)
- [Approvals](/docs/concepts/approvals.md)
- [Approving in a session vs the console](/docs/ship/approving-in-a-session-vs-the-console.md)
- [Deleting and restoring an app](/docs/ship/deleting-and-restoring-an-app.md)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "build/custom-domains: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
