Guides › Developers

How to send email with an API

An email API lets your app send messages such as sign-in codes, receipts and password resets with one HTTPS request, without running a mail server. This guide shows the request in four languages, then the details that make it reliable: verified domains, retries without duplicates, and delivery events.

8 min read · updated November 8, 2026

Before you start

  1. Create an account with an email API provider and verify your sending domain (SPF, DKIM and DMARC; see the setup guide).
  2. Create an API key with permission to send, and keep it on your server, never in browser or mobile app code.
  3. Store the key in an environment variable such as SENDFLUME_API_KEY.

The examples use the Sendflume API (POST https://api.sendflume.com/v1/send). Other providers work the same way with a different address and field names.

cURL

curl -X POST https://api.sendflume.com/v1/send \
  -H "Authorization: Bearer $SENDFLUME_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "to": "[email protected]",
    "from_email": "[email protected]",
    "from_name": "Your Shop",
    "subject": "Order {{order_id}} confirmed",
    "html": "<p>Thanks {{name}}, your order is on its way.</p>",
    "text": "Thanks {{name}}, your order is on its way.",
    "variables": { "name": "Rae", "order_id": "A-1044" },
    "idempotency_key": "order-A-1044"
  }'

Node.js

Node.js 18 and later have fetch built in, so no package is needed:

const res = await fetch('https://api.sendflume.com/v1/send', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.SENDFLUME_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    to: '[email protected]',
    from_email: '[email protected]',
    subject: 'Order {{order_id}} confirmed',
    html: '<p>Thanks {{name}}, your order is on its way.</p>',
    variables: { name: 'Rae', order_id: 'A-1044' },
    idempotency_key: 'order-A-1044',
  }),
});

if (!res.ok) throw new Error(`Send failed: ${res.status} ${await res.text()}`);
const message = await res.json(); // { id, to, subject, status, ... }

Python

With the requests package (pip install requests):

import os
import requests

res = requests.post(
    "https://api.sendflume.com/v1/send",
    headers={"Authorization": f"Bearer {os.environ['SENDFLUME_API_KEY']}"},
    json={
        "to": "[email protected]",
        "from_email": "[email protected]",
        "subject": "Order {{order_id}} confirmed",
        "html": "<p>Thanks {{name}}, your order is on its way.</p>",
        "variables": {"name": "Rae", "order_id": "A-1044"},
        "idempotency_key": "order-A-1044",
    },
    timeout=15,
)
res.raise_for_status()
print(res.json()["id"])

PHP

With PHP's built-in cURL extension:

<?php
$payload = json_encode([
    'to' => '[email protected]',
    'from_email' => '[email protected]',
    'subject' => 'Order {{order_id}} confirmed',
    'html' => '<p>Thanks {{name}}, your order is on its way.</p>',
    'variables' => ['name' => 'Rae', 'order_id' => 'A-1044'],
    'idempotency_key' => 'order-A-1044',
]);

$ch = curl_init('https://api.sendflume.com/v1/send');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 15,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('SENDFLUME_API_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => $payload,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($status !== 201) {
    throw new RuntimeException("Send failed: $status $body");
}

The fields

FieldRequiredWhat it does
toYesOne recipient address
subjectYes, unless the template has oneThe subject line; {{tags}} are filled from variables
html / textOne of them, or a templateThe message. Send both for the best results
templateNoThe name of a template saved in your account, instead of html
variablesNoValues for {{merge_tags}} in the subject and body
from_email / from_nameNoAn address on a verified domain; defaults to noreply@ your first verified domain
reply_toNoWhere replies go
idempotency_keyRecommendedRepeat a request with the same key and it will not send twice
attachmentsNoUp to 10 files, 10 MB in total, base64-encoded

Make it reliable

  • Retry safely. Network calls fail. Retry on timeouts and 5xx responses with a growing delay, and always send the same idempotency_key so a retry never sends a second email.
  • Do not retry 4xx errors without fixing the request: a 400 means a bad field, a 401 a wrong key.
  • Send from a queue, not inside the web request, so a slow email service never slows your app.
  • Listen for delivery events. Webhooks tell you when an email bounces or is marked as spam, so you can flag the address in your app.
  • Keep transactional and marketing email apart. Receipts and codes should never wait behind a newsletter.

Many APIs, Sendflume's included, can also create and check one-time sign-in codes and magic links for you, so you do not have to store codes yourself. Sendflume's codes are single-use, expire after 10 minutes and are limited in attempts. See Sendflume for developers.

Questions

Is an email API better than SMTP?

For new code, usually yes: one HTTPS request, clear error messages, and features such as templates and idempotency. SMTP is best for software that already has email settings. See the SMTP relay guide.

Can I send email from the browser?

No. Anyone could read your API key from the page and send as you. Always call the API from your server.

How do I send to many recipients?

Call the API once per recipient, ideally from a queue. For newsletters to a list, use campaigns, which add unsubscribe links and list checks for you.

Read next

Email that lands in the inbox.

Send campaigns and app email from your own domain, with SPF, DKIM and DMARC set up for you. Free to start.

No credit card needed.