sendpetrelgateway

Integration guide

From an API key to your first delivered message, and what to have in place before real traffic. Pair it with the API reference.

  1. Get an API key

    Keys are issued per integration — write to hello@sendpetrel.com with your application's name, the domain you will send from and the kinds of mail (transactional, marketing). You receive a tenant and a key such as edg_yourapp_…, shown once.

    Store it as a secret in your application's environment (never in the repository or the browser):

    SENDPETREL_API_KEY=edg_yourapp_…
    SENDPETREL_BASE_URL=https://api.sendpetrel.com

    Check that it works:

    curl -s https://api.sendpetrel.com/v1/usage \
      -H "Authorization: Bearer $SENDPETREL_API_KEY"
  2. Register your sending domain

    Use a domain (or subdomain) you control, for example mail.yourapp.com or yourapp.com. Mail sent from your own domain builds your own reputation, so this is the right choice for production.

    No domain of your own yet? We can set up a managed sending subdomain such as yourapp.sendpetrel.com: you register it in this step as usual, and we publish its DNS records for you, so you can skip step 3. Its reputation is shared with the gateway, so treat it as a way to start, not a long-term home. The bare sendpetrel.com domain is not available for integrations.
    curl -s https://api.sendpetrel.com/v1/sending-domains \
      -H "Authorization: Bearer $SENDPETREL_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{"domain": "yourapp.com"}'

    Keep the returned domain_id. The domain starts as pending.

  3. Publish the DNS records

    curl -s https://api.sendpetrel.com/v1/sending-domains/$DOMAIN_ID/dns-records \
      -H "Authorization: Bearer $SENDPETREL_API_KEY"

    Add every record it lists at your DNS provider. They look like this (values are specific to your domain):

    PurposeTypeNameValue
    OwnershipTXT_edg-verification.yourapp.comedg-verification=…
    SPFTXTyourapp.cominclude include:spf.sendpetrel.com in your one SPF record
    DKIMTXTpostal-xxxxxx._domainkey.yourapp.comv=DKIM1; … p=…
    Return pathCNAMEpsrp.yourapp.comrp.sendpetrel.com
    DMARC (recommended)TXT_dmarc.yourapp.comv=DMARC1; p=none; …
    If you use Cloudflare, set the CNAME to DNS only (grey cloud). Keep a single SPF record per name — merge the include into an existing one rather than adding a second.
  4. Verify the domain

    curl -s -X POST https://api.sendpetrel.com/v1/sending-domains/$DOMAIN_ID/verification-checks \
      -H "Authorization: Bearer $SENDPETREL_API_KEY"

    Each record comes back pass, fail or missing. DNS can take a few minutes to propagate; checks are rate-limited, so wait between attempts. When all required records pass, the domain is reviewed and switched to verified, after which it can send.

  5. Send your first message

    Use a stable Idempotency-Key per logical email (for example password-reset-{user_id}-{token_id}): if your request times out, retry with the same key and body — the gateway returns the original result instead of sending twice.

    # Python (requests)
    import os, requests
    r = requests.post(
        os.environ["SENDPETREL_BASE_URL"] + "/v1/messages",
        headers={"Authorization": "Bearer " + os.environ["SENDPETREL_API_KEY"],
                 "Idempotency-Key": f"welcome-{user.id}"},
        json={"from": "YourApp <hello@yourapp.com>", "to": [user.email],
              "subject": "Welcome to YourApp", "text": text_body, "html": html_body,
              "tags": {"kind": "welcome"}},
        timeout=15)
    r.raise_for_status()
    message_id = r.json()["message_id"]
    // Node.js (fetch)
    const res = await fetch(`${process.env.SENDPETREL_BASE_URL}/v1/messages`, {
      method: "POST",
      headers: { Authorization: `Bearer ${process.env.SENDPETREL_API_KEY}`,
                 "Content-Type": "application/json",
                 "Idempotency-Key": `receipt-${order.id}` },
      body: JSON.stringify({ from: "YourApp <orders@yourapp.com>", to: [order.email],
                             subject: `Receipt for order ${order.id}`, text, html }),
    });
    if (res.status !== 202) throw new Error(`sendpetrel ${res.status}`);
    const { message_id } = await res.json();
  6. Track delivery

    Store the message_id with your own record. Read GET /v1/messages/{message_id} for the current state, or poll GET /v1/events with its cursor to sync outcomes for all messages: delivered, deferred (retried automatically), bounced, failed, complaints and unsubscribes.

  7. Handle errors

    ResponseWhat to do
    400 / 422Fix the request; the problem document names the field. 422 domain_not_verified means the from domain is not verified yet; suppressed recipients are reported per recipient. Do not retry unchanged.
    401 / 403Wrong, revoked or expired key, or a key without the scope the route needs.
    409Same Idempotency-Key with a different body, or the first request is still in progress.
    429Rate limit or daily quota reached; wait for Retry-After.
    5xx / timeoutRetry with backoff using the same Idempotency-Key.
  8. Respect opt-outs

    Bounced and complaining addresses are suppressed for you; check GET /v1/suppressions before re-adding anyone.

    Send newsletters and promotions with "category": "marketing" (and an unsubscribe_topic if you have several lists). For marketing mail the gateway adds the List-Unsubscribe and List-Unsubscribe-Post headers itself, which make mail apps show a one-click Unsubscribe button; each recipient gets their own link, so a marketing message is addressed to one recipient. Mirror opt-outs into your app with GET /v1/unsubscribes.

    Marketing mail must also carry a visible unsubscribe link and your postal address (CAN-SPAM). Until the gateway can add them for you, include both in your message footer.
  9. Before real traffic

    • All required DNS records pass and the domain is verified; DMARC is published.
    • Every send uses a deterministic Idempotency-Key and a 10–15 s timeout.
    • Errors are handled as above, and message_ids are stored.
    • Marketing mail uses category: marketing; consent is recorded in your app.
    • Start small: tens of messages a day to engaged recipients, then roughly double weekly while bounces stay under 2% and complaints under 0.1%.