Skip to content

Connect a domain and wait for SSL

Add a domain to a site, check its DNS and wait for the certificate.

2 min read Updated

Connect a domain you own to a site, wait until its DNS points at the site's server and its SSL certificate is issued, then serve the site on it.

What you need

  • A token with sites:read (View sites) and sites:write (Manage sites).
  • The site's id, and access to the domain's DNS records, wherever they're managed.

1. Connect the domain

POST /v1/sites/{siteId}/domains with the domain. Its DNS is checked at once: if it already points at the server, the answer's status is provisioning; otherwise it's pending_dns, and dnsTarget is the address to point it at.

curl -X POST "https://api.rabbitflare.com/v1/sites/8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d/domains" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
  "hostname": "shop.example.com"
}'

2. Point the domain at the server

Where the domain's DNS is managed, set its A record to dnsTarget, the server's IPv4 address. For shop.example.com, that's the record named shop; for example.com itself, the record at the root (often written @). Remove other A and AAAA records of that name: they would send some visitors elsewhere.

3. Check the DNS

Check it now and then, every minute say: DNS changes can take a while to be seen. Until the domain points at the server, the answer is 422 dns_mismatch, and its message says where it points now. Once it does, the answer is 202, with the domain's status provisioning: it's set up on the server and its certificate issued, usually within a few minutes.

curl -X POST "https://api.rabbitflare.com/v1/sites/8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d/domains/c4d5e6f7-a8b9-4c0d-9e1f-2a3b4c5d6e7f/actions/verify" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN"

4. Wait for the certificate

Read the site's domains until this one's status and sslStatus are both active. When either is failed, lastError says why; once that's fixed, ask for the certificate again.

curl "https://api.rabbitflare.com/v1/sites/8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d/domains" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN"

Or let webhooks tell you: site.domain_verified, site.domains_updated or site.domains_failed.

5. Serve the site on it

Make it the site's primary domain: the site is served there, and its other addresses redirect to it. Doing it once the certificate is issued means no visitor sees a certificate warning in between.

curl -X POST "https://api.rabbitflare.com/v1/sites/8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d/domains/c4d5e6f7-a8b9-4c0d-9e1f-2a3b4c5d6e7f/actions/make-primary" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN"

What can go wrong

CodeStatusWhat it meansWhat to do
dns_mismatch 422 The domain doesn't point to the site's server yet. message says where it points now. Point the domain's A record at the server's IPv4 address, wait a few minutes, then verify again.
conflict 409 This clashes with something that exists, or is already done (for example, a domain already connected to a site). Read message; there may be nothing left to do.
limit_reached 403, 409 A limit is reached: message says which (servers per account, snapshots per server, domains per site, …). Delete something first, or contact support to raise the limit.
not_verified 409 The domain's DNS isn't verified yet. Verify the domain first. A domain waiting for DNS can be made primary once it's verified instead.
busy 409 Another change is still in progress on this server or site, or something else under way stops this one for now (a site backup running on the server, the server's backups being moved): message says what. Wait until it finishes, then try again. A change to the server or site itself is done when its pendingAction is null again; for anything else, message says how long to wait.

The whole script

All five steps in JavaScript (Node.js 18 or later; save it as an .mjs file). It checks the DNS every minute for up to 60 minutes and stops after that; run it again later and it picks up where it was, without connecting the domain twice.

JavaScript
// Connects a domain to a site, waits until its DNS points at the site's server and its SSL certificate is issued,
// then serves the site on it. Run it again at any time: it picks up where it was.
const API = "https://api.rabbitflare.com/v1";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

// Sends a request and gives back its data. Over a rate limit (429) it waits as long as Retry-After says, then sends
// the same request again; any other error is thrown with the API's code.
async function rabbitflare(method, path, body, headers = {}) {
  for (;;) {
    const res = await fetch(API + path, {
      method,
      headers: {
        Authorization: `Bearer ${process.env.RABBITFLARE_TOKEN}`,
        ...(body ? { "Content-Type": "application/json" } : {}),
        ...headers,
      },
      body: body ? JSON.stringify(body) : undefined,
    });
    if (res.status === 429) {
      await sleep((Number(res.headers.get("Retry-After")) || 1) * 1000);
      continue;
    }
    const answer = res.status === 204 ? null : await res.json().catch(() => null);
    if (!res.ok) throw Object.assign(new Error(answer?.error?.message ?? `HTTP ${res.status}`), { code: answer?.error?.code });
    return answer?.data ?? null;
  }
}

const SITE_ID = "8a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d";
const HOSTNAME = "shop.example.com";

// 1. Connect the domain, unless it's connected already. The answer says where its A record must point.
const connected = await rabbitflare("GET", `/sites/${SITE_ID}/domains`);
let domain =
  connected.find((d) => d.hostname === HOSTNAME) ??
  (await rabbitflare("POST", `/sites/${SITE_ID}/domains`, { hostname: HOSTNAME }));

// 2. While its DNS doesn't point at the server, check it every minute: DNS changes can take a while to be seen.
const dnsDeadline = Date.now() + 60 * 60_000;
while (domain.status === "pending_dns") {
  if (Date.now() > dnsDeadline) throw new Error(`${HOSTNAME} doesn't point at ${domain.dnsTarget} yet. Run this again later.`);
  console.log(`Point the A record of ${HOSTNAME} at ${domain.dnsTarget}.`);
  await sleep(60_000);
  try {
    domain = await rabbitflare("POST", `/sites/${SITE_ID}/domains/${domain.id}/actions/verify`);
  } catch (err) {
    if (err.code !== "dns_mismatch") throw err;
    console.log(err.message); // Where the domain points now.
  }
}

// 3. Wait while the domain is set up on the server and its SSL certificate is issued (usually a few minutes).
while (domain.status !== "active" || domain.sslStatus !== "active") {
  if (domain.status === "failed" || domain.sslStatus === "failed") throw new Error(`${HOSTNAME}: ${domain.lastError}`);
  await sleep(10_000);
  const domains = await rabbitflare("GET", `/sites/${SITE_ID}/domains`);
  domain = domains.find((d) => d.id === domain.id);
}

// 4. Serve the site on it: its other addresses redirect there.
if (!domain.isPrimary) await rabbitflare("POST", `/sites/${SITE_ID}/domains/${domain.id}/actions/make-primary`);
console.log(`The site is served at https://${HOSTNAME}`);

Still have a question?

Our team answers support tickets about the API, and other developers answer in the community.

Contact support

Popular

API docs