Custom domains for AI agents
This page is for an agent that adds a custom domain to a LeafCloud site. It gives the order of the calls, the records to create, how to wait for the domain to go live, and what to do when a check fails. A person still creates the DNS records at their DNS provider, or lets the agent do it only if the person has given it access to that provider.
The commands below are the leaf CLI. Each has an MCP tool with the same effect. The tool names are in the last section.
Rules to follow
Section titled “Rules to follow”- Add and change domains only when the person has asked for it.
- Never remove a domain unless the person asked for that exact domain. The MCP tool
remove_domainneedsconfirmset to the domain name. - Never use an IP address in a record. LeafCloud does not publish IP addresses. Use the hostnames from
records. For the bare domain, use a flattened or ALIAS record. - Keep every
_acme-challengerecord. Renewals need it. - Do not print or log
LEAF_TOKEN.
Sequence
Section titled “Sequence”1. Check the sign-in and the scope
Section titled “1. Check the sign-in and the scope”leaf whoami --jsonAdding, changing and removing domains needs a signed-in session or a manage key:
- A normal signed-in session (
leaf login) can manage domains. It has the same rights as the account owner. - An API key needs the
managescope, and it must cover the whole account, not one site. Create one withleaf keys create --name domains --can manage --expires 90d. Do not add--site: a key limited to one site cannot manage domains. - A
deploykey or areadkey cannot change domains. Reading domains needsreadormanage.
See Use with AI agents and API keys.
2. See what exists
Section titled “2. See what exists”leaf domains --jsonEach line is a domain with its state, its kind (single for a domain for one project, platform for a project-addresses domain) and the projects that use it. If the domain is already listed, do not add it again. Read it with leaf domains show <domain> --json instead.
3. Add the domain
Section titled “3. Add the domain”For a domain for one project:
leaf domains add shop.example.com --project portfolio --jsonFor a project-addresses domain, every project:
leaf domains add example.app --project-addresses --all-projects --jsonFor a project-addresses domain with chosen projects:
leaf domains add example.app --project-addresses --project portfolio --project docs --jsonThe result has records, the DNS records to create. Each record has type, name and value. A root domain or a www pair also has a companion, with its own records. Create the records from both.
Do not add --wait to this command. The records do not exist yet, so the wait polls for 5 minutes and then exits with status 5. Create the records first (step 4), then run step 5.
Use the name and value exactly as the JSON gives them. The note field, when present, is advice for a person, not part of the record.
4. Create the records
Section titled “4. Create the records”Create each record at the DNS provider that hosts the domain. Use the type, name and value from step 3.
- Do not proxy the records. On Cloudflare, every record must be DNS only (grey cloud). A proxied record breaks the certificate.
- For the bare domain, the record is a CNAME at the top of the zone. Many providers cannot do that. If the provider cannot flatten it (ALIAS, ANAME or CNAME flattening), tell the person and use one of the workarounds: move the DNS to Cloudflare, or use
wwwonly with a forward from the bare domain. See DNS providers.
5. Wait for Live
Section titled “5. Wait for Live”leaf domains check shop.example.com --wait --json--wait checks again every 10 seconds, and stops after 5 minutes. Read the exit status:
| Exit status | Meaning | What to do |
|---|---|---|
0 | Every address of the domain is live. | Report the address (https://shop.example.com). Go to step 6 if the domain is a pair. |
1 | A certificate failed, or DNS changed. The message says which, and for which domain. | Read the failure in step 6, fix it, then run the same command again. |
5 | The domain was not live within 5 minutes. | It may still become live. Run leaf domains show and read the checks. Fix what is missing, then wait again. |
Use the same wait after you change the records. Do not wait more than once for the same change without fixing something first.
6. Read a failure
Section titled “6. Read a failure”Run leaf domains show <domain> --json. Look at state, checks and problem. Each check has a name, ok, message and sometimes transient.
| State or check | What it means | What to do |
|---|---|---|
waiting-dns, check ownership fails | The TXT record _leafcloud-verify.<host> is missing, or has a different value. | Create or correct the TXT record with the value from records. |
waiting-dns, check acme fails | The _acme-challenge.<host> CNAME is missing or wrong. | Create it with the value from records. A pair needs one for each host. |
waiting-dns, check routing fails | The record that sends visitors to LeafCloud is missing or points somewhere else. | Create or correct the CNAME with the value from records. For the bare domain, use flattening. |
Any check with "transient": true | The DNS lookup timed out. The record may be correct. | Do nothing. It is retried. Do not change a correct record. |
certificate-failed | The records are right, but the certificate could not be issued. certificate.last_error is a short safe summary. | Read last_error. If the domain has a CAA record, it must allow letsencrypt.org. Fix the cause, then run leaf domains check again. |
dns-changed | A live domain has a record that points somewhere else. problem.record is the record to put back. | Put back the record from problem.record. The domain keeps serving meanwhile. |
getting-certificate | The records are right. The certificate is being issued. | Keep waiting. |
For the full list of states and what the person sees, see Status and checks. For the common causes, see Troubleshooting.
7. Set the root and www addresses (optional)
Section titled “7. Set the root and www addresses (optional)”For a domain for one project, the first address is the primary address and the other one of a pair redirects to it. To change that:
leaf domains primary www.shop.example.com # the address must be liveleaf domains redirect shop.example.com # send visitors to the primaryleaf domains serve shop.example.com # serve the project here tooFor a project-addresses domain, set the root and www addresses:
leaf domains root example.app --serve portfolioleaf domains root example.app --redirect https://www.example.appleaf domains www example.app --offEach of these fails with not ready until the domain is live, and the root can only serve a project that has an address on the domain and a live deploy.
8. Check the limits
Section titled “8. Check the limits”leaf usage --jsonRead domains_limit, domains_used and domains_per_project_limit. A refused add says which limit was reached. Tell the person, and suggest removing a domain, or asking for a higher limit from Settings → Help & feedback in the dashboard. The CLI and MCP tools have no support command. See Limits.
What each refusal means
Section titled “What each refusal means”| Error code | Meaning | What to do |
|---|---|---|
409 hostname_conflict | The name is used by another address or another account. | Choose another name, or remove the other address first. |
409 account_domain_limit_reached | The account has all its project-addresses domains. | Remove one, or ask for more in Settings → Help & feedback. |
409 domain_limit_reached | The project has all its custom domains. | Remove one, or ask for more in Settings → Help & feedback. |
409 label_taken | The label or the project is already on this domain. | Choose another label. |
409 not_ready | The domain or address is not live yet. | Finish step 5 first. |
409 primary_needs_successor | The primary address cannot be changed directly. | Make another live address the primary first. |
422 invalid_route | The route is not primary, serve or redirect. | Use one of those. |
422 invalid_redirect | The redirect loops, or points to itself or is not https://. | Redirect to a different https:// address. |
404 not_listed | The project is not on this domain. | Add the project to the domain first. |
409 not_platform_domain | The command needs a project-addresses domain. | Use the right domain. |
429 rate_limited | The domain was checked within the last 10 seconds. | Wait for Retry-After seconds, or use the CLI or MCP tools, which wait for you. |
MCP tools
Section titled “MCP tools”When the agent uses leaf mcp, these tools do the same things. See Use LeafCloud from AI assistants (MCP) for setup.
| Step | Tool | Arguments |
|---|---|---|
| See what exists | list_domains | none |
| Read one domain | get_domain | domain |
| Add a domain | add_domain | domain, and project, or project_addresses with all_projects or projects |
| Wait for Live | check_domain, then again until state is live | domain |
| Set the root or www address | set_domain_name | domain, name (root or www), mode, and project or redirect_to |
| Set how an address is served | set_domain_route | domain, route (primary, serve or redirect) |
| Change the projects of a project-addresses domain | set_domain_projects | domain, and all_projects, include, exclude or labels |
| Remove a domain | remove_domain | domain, confirm (exactly the domain name) |
check_domain waits for the 10-second limit when it has to, so calling it again is safe. Call it until state is live, or until a failure is shown. Read checks for the reason, as in step 6.