Skip to content

Create a server and a site

Order a server with WordPress on it and wait until it's ready.

2 min read Updated

Order a server with WordPress on it from your own code, wait until the site is live, and get the administrator's sign-in.

What you need

  • A token with servers:read (View servers), servers:create (Create servers), sites:read (View sites) and sites:files (Files and logins).
  • An account that has agreed to card charges once, in the panel's Billing: a server is paid like one ordered in the panel, from the wallet first, then the default card (see Paying through the API).

1. Choose a plan that's in stock

List the cloud's plans in your region, with their prices and sizes, and what's in stock: a plan can be sold out in a region for a while. Neither needs a scope.

curl "https://api.rabbitflare.com/v1/clouds/digitalocean/plans?region=fra1" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN"
curl "https://api.rabbitflare.com/v1/clouds/digitalocean/availability" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN"

2. Order the server, with its site

POST /v1/servers with the plan, and a first site in site: WordPress is installed on the server as soon as it's ready. Like every order, it needs an Idempotency-Key: sent again with the same key (after a timeout, say), the order is still placed once.

# Keep this key when you send the same request again (after a timeout, say), so it's only done once.
KEY=$(uuidgen)
curl -X POST "https://api.rabbitflare.com/v1/servers" \
  -H "Authorization: Bearer $RABBITFLARE_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $KEY" \
  -d '{
  "name": "Production",
  "cloud": "digitalocean",
  "region": "fra1",
  "planId": "business",
  "site": {
    "name": "Shop",
    "siteTitle": "Example Shop",
    "adminUsername": "editor",
    "adminEmail": "[email protected]"
  }
}'

The answer comes at once, with the server's status provisioning. It's usually ready in about 5 minutes: poll GET /servers/{serverId} every 10 seconds until status is active.

3. Wait for the server, then the site

Read the server until its status is active. The site is listed on the server from the start (GET /v1/servers/{serverId}/sites), as queued: read it the same way until it's active.

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

Or let webhooks tell you: server.ready and site.ready are sent when each is ready.

4. Sign in to WordPress

The WordPress administrator made with the site, with its password. It's a secret: show it to whoever asked, and don't log or store it (see the security checklist).

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

What can go wrong

CodeStatusWhat it meansWhat to do
plan_unavailable 403, 409, 422 The plan can't be had there: it isn't offered on this cloud or in this region, it's sold out there right now, or it isn't open to your account yet. Choose another plan or region. GET /v1/clouds/{cloud}/availability says what's in stock.
charges_not_authorized 403 The account hasn't agreed to card charges yet. In the panel, open Billing and accept the terms once. Then try again.
payment_method_required 402 There's no money in the wallet and no saved card to pay with. Add funds to the wallet or save a card in the panel.
card_declined 402 The card was declined. message gives the bank's reason. Nothing was created. Use another card, or add funds to the wallet.
payment_action_required 402 The bank wants the customer to confirm this payment, which can't happen through the API. Nothing was created. Add funds to the wallet in the panel (wallet payments need no confirmation), or make this purchase in the panel.
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.

After the first answer, a failure is a status: a server whose setup failed is failed, with the reason in lastError, and can be set up again; a site whose install failed can be installed again. A refusal is kept with its Idempotency-Key: once you've fixed the cause, order with a new key.

The whole script

All four steps in JavaScript (Node.js 18 or later; save it as an .mjs file). It takes the cheapest plan in stock, and waits up to 20 minutes for the server and 10 for the site.

JavaScript
// Orders a server with a WordPress site on it, waits until the site is live, and prints where to sign in.
import { randomUUID } from "node:crypto";

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;
  }
}

// Reads a server or a site every 10 seconds until it's active, for up to `minutes`.
async function waitUntilActive(path, minutes) {
  const deadline = Date.now() + minutes * 60_000;
  for (;;) {
    const thing = await rabbitflare("GET", path);
    if (thing.status === "active") return thing;
    if (thing.status === "failed") throw new Error(`${thing.name} failed: ${thing.lastError}`);
    if (Date.now() > deadline) throw new Error(`${thing.name} isn't ready after ${minutes} minutes`);
    await sleep(10_000);
  }
}

const CLOUD = "digitalocean";
const REGION = "fra1";

// 1. The cheapest plan in stock in the region.
const plans = await rabbitflare("GET", `/clouds/${CLOUD}/plans?region=${REGION}`);
const { regions } = await rabbitflare("GET", `/clouds/${CLOUD}/availability`);
const plan = plans
  .filter((p) => regions[REGION]?.includes(p.id))
  .sort((a, b) => a.priceMonthlyCents - b.priceMonthlyCents)[0];
if (!plan) throw new Error(`No plan is in stock in ${REGION} right now: choose another region`);

// 2. The server, with its first site, installed as soon as the server is ready. Sent again with the same
// Idempotency-Key (after a timeout, say), the order is still placed only once.
const idempotencyKey = randomUUID();
const server = await rabbitflare(
  "POST",
  "/servers",
  {
    name: "Production",
    cloud: CLOUD,
    region: REGION,
    planId: plan.id,
    site: { name: "Shop", siteTitle: "Example Shop", adminUsername: "editor", adminEmail: "[email protected]" },
  },
  { "Idempotency-Key": idempotencyKey },
);
console.log(`Ordered ${server.name} on the ${plan.name} plan`);

// 3. Wait for the server (about 5 minutes), then for its site (1 to 2 minutes more).
await waitUntilActive(`/servers/${server.id}`, 20);
const [first] = await rabbitflare("GET", `/servers/${server.id}/sites`);
const site = await waitUntilActive(`/sites/${first.id}`, 10);

// 4. The WordPress administrator, to sign in with. Show the password to whoever asked; don't log it.
const login = await rabbitflare("GET", `/sites/${site.id}/wordpress-login`);
console.log(`${site.url} is live. Sign in at ${site.adminUrl} as ${login.username}, with the password ${login.password}`);

Still have a question?

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

Contact support

Popular

API docs