Handle exceptions and returns

Find parcels with problems, check them again, and react when one is sent back.

Embed
Preview of the card
<a href="https://trackified.net/guides/exceptions-and-returns"><img src="https://trackified.net/guides/_api/public/orgs/e2a7773e-db9e-41cb-bd59-bd248745cac3/spaces/5f05ba65-3f06-41a2-ad68-be7988522567/pages/f6dae862-76e9-4d74-a095-b970b884ae79/social-card.svg?v=3e7ae33eec8b" alt="Handle exceptions and returns" width="600" height="315"></a>

A parcel is in exception when the carrier reports a problem: a failed delivery, a damaged label, a customs hold. It's returned when it's on its way back to you. Both need a person to look at them, so it helps to catch them early.

Exceptions stop scheduled checks

Once a parcel is delivered, exception or returned, we stop checking it on a schedule. A problem can clear later without a new scheduled check noticing, so check it again yourself with retrack.

Get told about problems

Subscribe an endpoint to tracking.exception and tracking.returned:

import { Trackified } from "trackified";

const client = new Trackified();

const webhook = await client.webhooks.create({
  url: "https://example.com/webhooks/trackified-problems",
  event_types: ["tracking.exception", "tracking.returned"],
});
console.log(webhook.secret); // shown once

A tracking.exception comes in two shapes:

  • The carrier reported a problem. data is the tracking with status: "exception". Its newest event, data.events[0], says what went wrong.

  • We couldn't reach the carrier. data is {tracking_id, error}, with no tracking. Nothing is known to be wrong with the parcel; our check failed, and we try again in 15 minutes. The tracking's exception field holds the same error until a check succeeds.

Tell them apart before you alert anyone:

import { Trackified } from "trackified";

declare const rawBody: string;
declare const header: string;

const event = await Trackified.webhooks.constructEvent(rawBody, header, process.env.TRACKIFIED_WEBHOOK_SECRET!);

if (event.type === "tracking.exception") {
  const data = event.data as unknown as Record<string, unknown>;
  if ("tracking_id" in data) {
    console.log("check failed, retrying soon:", data.tracking_id, data.error);
  } else {
    const tracking = Trackified.webhooks.trackingOf(event);
    console.log("carrier problem:", tracking.custom_id, tracking.events[0]?.description);
  }
}
if (event.type === "tracking.returned") {
  const tracking = Trackified.webhooks.trackingOf(event);
  console.log("on its way back:", tracking.custom_id);
}

Review them each morning

List every parcel in exception and ask for a fresh check on each:

import { Trackified } from "trackified";

const client = new Trackified();

for await (const t of client.trackings.iterate({ status: "exception" })) {
  console.log(t.custom_id, t.tracking_number, t.events[0]?.description);
  await client.trackings.retrack(t.id); // checked again within a minute
}

retrack queues a check within about a minute and puts the parcel back on the schedule. If the problem has cleared, the status moves on and you get a tracking.status_changed event.

From the terminal, the same list is:

trackified list --status exception --all

Returned parcels

A parcel moves to returned once, and tracking.returned fires then. Use it to start your refund or restock flow. Stop the tracking once you've handled it, so it no longer shows in your lists:

import { Trackified } from "trackified";

declare const trackingId: string;
const client = new Trackified();

await client.trackings.delete(trackingId);

Stopping a tracking doesn't give back its place in your monthly quota.

Updated