Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

Set up a sending domain

Set up a domain before the project emails real addresses, such as sign-in codes for your product’s customers; Projects explains what happens to email until it verifies. Adding a domain takes the role of an owner or an admin; Roles lists each role’s permissions.

Before you start

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

1. Add the domain

Send the domain to PUT /v1/projects/{id}/sending-domain. Replace example.com with a domain whose DNS records you can change.

const added = await api(
  'PUT',
  `/v1/projects/${project.id}/sending-domain`,
  token,
  { domain: 'example.com' },
);
const { records } = added.sendingDomain;
console.log(added.sendingDomain.status, added.sendingDomain.address); // pending no-reply@example.com

The default address is no-reply@ the domain. To use another address, send address too, such as { domain: 'example.com', address: 'hello@example.com' }. Use the same domain and 1 to 64 letters, digits, or . _ % + - before the @. Changing the address on an already verified domain needs no new verification.

Adding a domain may answer conflict with details.reason set to domain_in_use, or invalid_request.

2. Create the DNS records

Create every record in records at the domain’s DNS provider, with its type, name, and value exactly as given. Verification waits on all of them: three CNAME records that sign the email, and an MX and a TXT record for the subdomain bounces return to.

added.sendingDomain.recommended lists records that help delivery but that nothing waits on, such as a DMARC TXT record when the domain has none. Create them too.

While a new domain is pending, the project’s current sender keeps sending, so changing domains leaves no gap.

Check it worked

Read the domain until it leaves pending. A read rechecks a pending domain if the last check was at least 10 seconds ago.

let state = await api(
  'GET',
  `/v1/projects/${project.id}/sending-domain`,
  token,
);
while (state.sendingDomain.status === 'pending') {
  await new Promise((resolve) => setTimeout(resolve, 5 * 60_000));
  state = await api('GET', `/v1/projects/${project.id}/sending-domain`, token);
}
console.log(state.sendingDomain.status, state.sender?.address); // verified no-reply@example.com

Check that status is verified and sender.address is the address you requested. If the domain verified and the sender hasn’t switched, read again. Agentpost also checks on its own, every 5 minutes for the first hour, then hourly. While the domain is pending, waitingOn lists what is still missing.

A domain that can’t verify, such as one still missing a record after 72 hours, becomes failed, and failure says why and what to do. Send the same PUT again to start over, and create the records it answers with, which may be new. Another workspace can add a failed domain if no project sends from it.

The sending domain reference has every field, and Remove the sending domain stops sending from it.