Import trackings in bulk

Load a backlog of orders in batches, without adding a parcel twice.

Embed
Preview of the card
<a href="https://trackified.net/guides/bulk-import"><img src="https://trackified.net/guides/_api/public/orgs/e2a7773e-db9e-41cb-bd59-bd248745cac3/spaces/5f05ba65-3f06-41a2-ad68-be7988522567/pages/b05e9dab-c88f-41d4-a866-551dfa03125d/social-card.svg?v=3e7ae33eec8b" alt="Import trackings in bulk" width="600" height="315"></a>

Use POST /v1/trackings/bulk to add many trackings in one request. This guide loads a list of orders in batches sized for your plan, and makes it safe to run again after a failure.

Bulk creates are not idempotent

The bulk endpoint ignores Idempotency-Key. If a bulk request times out and you send it again, the same parcels can be added twice. This guide turns off automatic retries and checks what landed before it resends.

Batch size

Each request may carry up to your plan's batch size:

Plan

Rows per request

Free

5

Starter

40

Pro

40

Scale

100

A larger batch fails with 400 bad_request. GET /v1/account/plan returns your size as bulk_limit.

The whole batch is checked against your monthly quota first. If it doesn't fit, the request fails with 402 quota_exceeded and adds nothing.

The import script

Give every row a custom_id, such as your order id. That's how the script finds rows that already landed.

import { Trackified, type TrackingCreateParams } from "trackified";

const client = new Trackified();
const BATCH = 40; // your plan's bulk_limit

const orders: TrackingCreateParams[] = [
  { tracking_number: "1Z999AA10123456784", custom_id: "order_1042", customer_email: "ada@example.com" },
  { tracking_number: "9434650110837017316201", carrier: "usps", custom_id: "order_1043" },
  // ...
];

async function alreadyAdded(customId: string): Promise<boolean> {
  const page = await client.trackings.list({ custom_id: customId, limit: 1 });
  return page.data.length > 0;
}

for (let i = 0; i < orders.length; i += BATCH) {
  const batch = orders.slice(i, i + BATCH);
  let result;
  try {
    // No automatic retries: a retried bulk request can add the same rows twice.
    result = await client.trackings.createBulk(batch, { maxRetries: 0 });
  } catch (err) {
    console.error(`batch at ${i} failed; run the script again to resume`, err);
    break;
  }
  result.data.forEach((row, j) => {
    if (!row.ok) console.error(batch[j]!.custom_id, row.error);
  });
}

The response is 207 with one result per row, in the same order as your rows: {ok: true, tracking} or {ok: false, error}. One bad row doesn't stop the others.

Running it again safely

Before you run the script a second time, drop the rows that are already there:

import { type TrackingCreateParams } from "trackified";

declare const orders: TrackingCreateParams[];
declare function alreadyAdded(customId: string): Promise<boolean>;

const remaining: TrackingCreateParams[] = [];
for (const order of orders) {
  if (!(await alreadyAdded(order.custom_id!))) remaining.push(order);
}

That costs one request per row, so run it only after a failure, not on every import. Mind your rate limit: 60 requests a minute on Free.

One at a time instead

For a few hundred rows, or when you can't risk a duplicate, trackings.create with an idempotency key is simpler. A retry with the same key returns the first tracking:

import { Trackified, type TrackingCreateParams } from "trackified";

declare const orders: TrackingCreateParams[];
const client = new Trackified();

for (const order of orders) {
  await client.trackings.create(order, { idempotencyKey: order.custom_id });
}

See Pagination, retries and idempotency for how keys work.

Updated