Send delivery updates

Email your customers each time their parcel's status changes, using a webhook and the TypeScript library.

Embed
Preview of the card
<a href="https://trackified.net/guides/delivery-updates"><img src="https://trackified.net/guides/_api/public/orgs/e2a7773e-db9e-41cb-bd59-bd248745cac3/spaces/5f05ba65-3f06-41a2-ad68-be7988522567/pages/38008310-d05a-416d-89fc-7bf86d4a1438/social-card.svg?v=3e7ae33eec8b" alt="Send delivery updates" width="600" height="315"></a>

In this guide you build a small Express server that emails a customer when their parcel moves: shipped, out for delivery, delivered. Trackified tells your server about each change with a webhook. You send the email.

Trackified stores customer_email on a tracking but doesn't email anyone itself. Sending is up to you, so you choose the wording and the email provider.

You need Node.js 18 or later and an API key.

  1. Install the library
    npm install trackified express
    export TRACKIFIED_API_KEY=live_...
  2. Add trackings with the customer's email

    When an order ships, add the tracking with your order id and the customer's address. Use the order id as the idempotency key, so a retry never adds the parcel twice.

    import { Trackified } from "trackified";
    
    const client = new Trackified();
    
    await client.trackings.create(
      {
        tracking_number: "1Z999AA10123456784",
        custom_id: "order_1042",
        customer_email: "ada@example.com",
      },
      { idempotencyKey: "order_1042" },
    );
  3. Subscribe to status changes

    Register your endpoint for tracking.status_changed only. It fires once each time the status moves, delivery included.

    const webhook = await client.webhooks.create({
      url: "https://example.com/webhooks/trackified",
      event_types: ["tracking.status_changed"],
    });
    console.log(webhook.secret); // shown once: store it as TRACKIFIED_WEBHOOK_SECRET

    Don't also subscribe to tracking.delivered for emails: you'd get both events for one delivery, and tracking.delivered is sent again if a delivered parcel is checked again.

  4. Handle the webhook

    Verify the signature on the raw body, skip events you've already handled, and email the customer.

    import express from "express";
    import { Trackified } from "trackified";
    
    const app = express();
    const handled = new Set<string>(); // use your database in production
    
    const subjects: Record<string, string> = {
      in_transit: "Your order is on its way",
      out_for_delivery: "Your order is out for delivery",
      available_for_pickup: "Your order is ready to collect",
      delivered: "Your order has been delivered",
    };
    
    async function sendEmail(to: string, subject: string, text: string) {
      // Call your email provider here.
      console.log("email", to, subject, text);
    }
    
    app.post("/webhooks/trackified", express.raw({ type: "application/json" }), async (req, res) => {
      let event;
      try {
        event = await Trackified.webhooks.constructEvent(
          req.body,
          req.header("x-tracking-signature"),
          process.env.TRACKIFIED_WEBHOOK_SECRET!,
        );
      } catch {
        return res.sendStatus(400);
      }
    
      if (event.type !== "tracking.status_changed" || handled.has(event.id)) {
        return res.sendStatus(200);
      }
      handled.add(event.id);
    
      const { tracking, current_status } = event.data;
      const subject = subjects[current_status];
      if (subject && tracking.customer_email) {
        await sendEmail(
          tracking.customer_email,
          subject,
          `Follow your parcel: https://trackified.net/t/${tracking.id}`,
        );
      }
      res.sendStatus(200);
    });
    
    app.listen(3000);
  5. Try it

    Ask for a signed sample event and check your code accepts it:

    curl -X POST https://api.trackified.net/v1/webhooks/wh_.../test \
      -H "Authorization: Bearer $TRACKIFIED_API_KEY"

    The response has a signature and the body it signs; it isn't sent to your endpoint. See Verifying webhooks.

Why each part matters

  • The raw body. The signature covers the exact bytes we sent. express.raw keeps them; express.json would not.

  • The event id. If your server doesn't answer 2xx, we retry up to 5 times, about 30 seconds apart, with the same event id. Store the ids you've handled so a retry doesn't send a second email.

  • Answer fast. Return 200 as soon as you've recorded the event. If sending email is slow, queue it and send it from a worker.

  • The link. https://trackified.net/t/<tracking id> is a public page for that parcel. Brand the tracking page to show your name on it.

The event reference lists every status and event type.

Updated