Send delivery updates
Email your customers each time their parcel's status changes, using a webhook and the TypeScript library.
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.
- Install the library
npm install trackified express export TRACKIFIED_API_KEY=live_... - 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" }, ); - Subscribe to status changes
Register your endpoint for
tracking.status_changedonly. 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_SECRETDon't also subscribe to
tracking.deliveredfor emails: you'd get both events for one delivery, andtracking.deliveredis sent again if a delivered parcel is checked again. - 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); - 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
signatureand thebodyit 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.rawkeeps them;express.jsonwould not.The event id. If your server doesn't answer
2xx, we retry up to 5 times, about 30 seconds apart, with the same eventid. Store the ids you've handled so a retry doesn't send a second email.Answer fast. Return
200as 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.