Skip to content

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.

  • 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_domain needs confirm set 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-challenge record. Renewals need it.
  • Do not print or log LEAF_TOKEN.
TerminalTerminal windowbash
leaf whoami --json

Adding, 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 manage scope, and it must cover the whole account, not one site. Create one with leaf keys create --name domains --can manage --expires 90d. Do not add --site: a key limited to one site cannot manage domains.
  • A deploy key or a read key cannot change domains. Reading domains needs read or manage.

See Use with AI agents and API keys.

TerminalTerminal windowbash
leaf domains --json

Each 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.

For a domain for one project:

TerminalTerminal windowbash
leaf domains add shop.example.com --project portfolio --json

For a project-addresses domain, every project:

TerminalTerminal windowbash
leaf domains add example.app --project-addresses --all-projects --json

For a project-addresses domain with chosen projects:

TerminalTerminal windowbash
leaf domains add example.app --project-addresses --project portfolio --project docs --json

The 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.

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 www only with a forward from the bare domain. See DNS providers.
TerminalTerminal windowbash
leaf domains check shop.example.com --wait --json

--wait checks again every 10 seconds, and stops after 5 minutes. Read the exit status:

Exit statusMeaningWhat to do
0Every address of the domain is live.Report the address (https://shop.example.com). Go to step 6 if the domain is a pair.
1A 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.
5The 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.

Run leaf domains show <domain> --json. Look at state, checks and problem. Each check has a name, ok, message and sometimes transient.

State or checkWhat it meansWhat to do
waiting-dns, check ownership failsThe 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 failsThe _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 failsThe 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": trueThe DNS lookup timed out. The record may be correct.Do nothing. It is retried. Do not change a correct record.
certificate-failedThe 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-changedA 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-certificateThe 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:

TerminalTerminal windowbash
leaf domains primary www.shop.example.com # the address must be live
leaf domains redirect shop.example.com # send visitors to the primary
leaf domains serve shop.example.com # serve the project here too

For a project-addresses domain, set the root and www addresses:

TerminalTerminal windowbash
leaf domains root example.app --serve portfolio
leaf domains root example.app --redirect https://www.example.app
leaf domains www example.app --off

Each 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.

TerminalTerminal windowbash
leaf usage --json

Read 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.

Error codeMeaningWhat to do
409 hostname_conflictThe name is used by another address or another account.Choose another name, or remove the other address first.
409 account_domain_limit_reachedThe account has all its project-addresses domains.Remove one, or ask for more in Settings → Help & feedback.
409 domain_limit_reachedThe project has all its custom domains.Remove one, or ask for more in Settings → Help & feedback.
409 label_takenThe label or the project is already on this domain.Choose another label.
409 not_readyThe domain or address is not live yet.Finish step 5 first.
409 primary_needs_successorThe primary address cannot be changed directly.Make another live address the primary first.
422 invalid_routeThe route is not primary, serve or redirect.Use one of those.
422 invalid_redirectThe redirect loops, or points to itself or is not https://.Redirect to a different https:// address.
404 not_listedThe project is not on this domain.Add the project to the domain first.
409 not_platform_domainThe command needs a project-addresses domain.Use the right domain.
429 rate_limitedThe domain was checked within the last 10 seconds.Wait for Retry-After seconds, or use the CLI or MCP tools, which wait for you.

When the agent uses leaf mcp, these tools do the same things. See Use LeafCloud from AI assistants (MCP) for setup.

StepToolArguments
See what existslist_domainsnone
Read one domainget_domaindomain
Add a domainadd_domaindomain, and project, or project_addresses with all_projects or projects
Wait for Livecheck_domain, then again until state is livedomain
Set the root or www addressset_domain_namedomain, name (root or www), mode, and project or redirect_to
Set how an address is servedset_domain_routedomain, route (primary, serve or redirect)
Change the projects of a project-addresses domainset_domain_projectsdomain, and all_projects, include, exclude or labels
Remove a domainremove_domaindomain, 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.