Direct mail for growth teams, with an API underneath. Read the docs →

Shopify postcard automation: a thank-you card after the first order

Shopify postcard automation takes one webhook. Subscribe to Shopify’s orders/paid webhook, check the signature, and if it’s the customer’s first order, send a postcard to the order’s shipping address with one Mailr API call. A printed thank-you with a code for the next order is a direct ask for the second purchase.

Shopify direct mail automation with one webhook

A Next.js route that verifies Shopify’s HMAC signature, maps the shipping address to Mailr’s address fields and sends the tmpl_thank_you postcard. It needs SHOPIFY_WEBHOOK_SECRET and MAILR_API_KEY.

// app/api/shopify-orders/route.js (Next.js App Router), subscribed to the orders/paid webhook.
import crypto from "node:crypto";

async function sendPostcard(body) {
  const res = await fetch("https://api.mailr.dev/v1/postcards", {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.MAILR_API_KEY}`, "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
  const postcard = await res.json();
  if (!res.ok) throw new Error(postcard.error.message);
  return postcard;
}

// Shopify signs each webhook: base64 HMAC-SHA256 of the raw body with your app's secret.
function verified(rawBody, hmacHeader) {
  const digest = crypto.createHmac("sha256", process.env.SHOPIFY_WEBHOOK_SECRET).update(rawBody, "utf8").digest("base64");
  const a = Buffer.from(digest), b = Buffer.from(hmacHeader ?? "");
  return a.length === b.length && crypto.timingSafeEqual(a, b);
}

export async function POST(req) {
  const rawBody = await req.text();
  if (!verified(rawBody, req.headers.get("x-shopify-hmac-sha256"))) {
    return new Response("Bad signature", { status: 401 });
  }
  const order = JSON.parse(rawBody);
  const ship = order.shipping_address;

  // Your own record decides "first order" and "already mailed", so a retry never sends twice.
  if (ship?.address1 && (await isFirstOrder(order.customer.id)) && !(await alreadyMailed(order.customer.id))) {
    const postcard = await sendPostcard({
      size: "6x9",
      template: "tmpl_thank_you",
      to: {
        name: `${ship.first_name} ${ship.last_name}`.trim(),
        line1: ship.address1,
        ...(ship.address2 ? { line2: ship.address2 } : {}),
        city: ship.city,
        state: ship.province_code,
        zip: ship.zip,
      },
      from: { name: "Acme Outdoor", line1: "1 Example Way", city: "Phoenix", state: "AZ", zip: "85004" },
      merge: { first_name: ship.first_name, promo_code: "TRAIL15" },
    });
    await rememberMailed(order.customer.id, postcard.id);
  }
  return new Response("ok");
}

Three helpers are yours to write against your own database: isFirstOrder, alreadyMailed and rememberMailed. Keeping that record yourself is what stops a webhook retry from mailing the same customer twice.

We ran this handler against the Mailr API: a correctly signed order produced one postcard, and a bad signature got a 401 without sending anything.

Address fields, Shopify to Mailr

Shopify shipping_addressMailr to
first_name + last_namename
address1 / address2line1 / line2
citycity
province_codestate (two letters)
zipzip

Automated thank-you cards: printed, not handwritten

Some services send handwritten-style cards written by robots with pens. Mailr prints full-color postcards instead, so the whole card carries your design and photos. If a handwritten look matters most, those services fit better.

Two more postcards worth automating

Win-back

A nightly job that finds customers whose last order is 60 or more days old and sends the 6x11 win-back postcard once per customer. Skip anyone who ordered again on their own.

Abandoned cart direct mail

When a checkout stays open for 48 hours and you have the shipping address, send the abandoned cart postcard with the product name and a short cart link.

What it costs

Test keys are free while you build. Mailed in a batch, a 6x9 thank-you card is $0.89 with postage included; see pricing and the 6x9 postcard page.