Transactional Emails via API: Idempotency and Signed Webhooks

By Equipo de Replifier · ·Email

The business problem

An online store wants to send order confirmations automatically from its own system: the moment a payment is processed, the customer receives an email with their order summary. The challenge is not the send itself — it is reliability. When there are network hiccups or server failures, the system may retry the same request more than once. Without a protection mechanism, the customer receives the same email two or three times, which is confusing and looks unprofessional.

The team also wants to know when an email was delivered or failed, without having to check a dashboard manually.

How Replifier solves it

Replifier requires an Idempotency-Key header on every transactional send. You choose the value — your order ID, your transaction UUID — and Replifier guarantees that identifier is processed only once. If the system retries with the same key and the same body, Replifier returns the same result without creating a second send. The customer does not receive duplicates, even if the server failed and retried five times.

For delivery notifications, Replifier signs every webhook event with HMAC-SHA256. Your server can verify the signature before processing the event, confirming the notification actually came from Replifier and not from an external request.

Step by step

Before you start: you need a brand configured in Replifier with at least one active email provider.

  1. In the Replifier dashboard, create an API key and assign it only the messages.send scope. Using the minimum scope limits the impact if the key is ever leaked. Store it in a server-side environment variable; never in source code.

  2. From your system, build the POST request to the send endpoint. Include the Idempotency-Key header with your order ID:

curl -X POST "https://api.replifier.com/v1/brands/brd_your_brand/messages" \
  -H "Authorization: Bearer <your_api_key>" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-98765" \
  -d '{
    "to": "customer@example.com",
    "template": "confirm-order",
    "variables": {
      "order_number": "98765",
      "total": "USD 125.00"
    }
  }'
  1. Replifier responds with 202 and an ID starting with txm_. Store that ID in your database alongside the order so you can look it up later. The status field can be queued, sending, accepted, skipped, or failed.

  2. If the server retries the same request (same key, same body), Replifier returns the same txm_ with the Idempotent-Replayed: true header. No additional email is sent.

  3. To receive delivery notifications, create a webhook endpoint from the Replifier dashboard. The secret is shown only once when you create it; store it in a platform secret.

  4. On your server, verify the signature of each event before processing it. The X-Replifier-Signature header has the format sha256=<hex>. Compute HMAC-SHA256(secret, body_as_text) and compare using constant-time equality:

import { createHmac, timingSafeEqual } from 'node:crypto'

function verifySignature(secret, rawBody, header) {
  const expected = 'sha256=' + createHmac('sha256', secret)
    .update(rawBody)
    .digest('hex')
  const a = Buffer.from(expected)
  const b = Buffer.from(header)
  if (a.length !== b.length) return false
  return timingSafeEqual(a, b)
}
  1. Respond with 200 if the signature is valid. Replifier retries events that do not receive a 2xx response.

What you should see when done

  • The first call responds 202 with an id starting with txm_.
  • A retry with the same key returns the same id and the Idempotent-Replayed: true header.
  • In the Replifier dashboard, the send appears in the recipient’s history with its full timeline.
Screenshot of the Replifier dashboard: one recipient's timeline, from send to click.Screenshot of the Replifier dashboard: one recipient's timeline, from send to click.

Common issues

  • Duplicate emails are arriving: the server is generating a different idempotency key on each retry (for example, a fresh UUID every attempt). Use the order ID as the key — it is already unique and stable.
  • 409 idempotency_conflict: the same key was used with a different body. Generate a new key or investigate why the body changes between retries.
  • Webhook signature does not match: the HMAC must be computed over the raw body as text, before parsing it as JSON. Also check that you are using the secret for the correct endpoint.
  • 422 recipient_suppressed: the recipient is on your brand’s suppression list (hard bounce, complaint, or unsubscribe). Replifier does not send to suppressed addresses by design.

To connect it through the API, see the documentation