Common Scenarios
Ready-made approaches for typical tasks. Examples use clients in PHP, Node.js, and Python (single file, downloadable in the “Ready Solutions” section).
Clients: PHP · Node.js · Python
Bulk Label Purchase
Order cycle: rate, selection of the cheapest option, purchase with an idempotent key from the order number. The script can be safely run repeatedly: already purchased orders will return as duplicates, not purchased again. When 402 occurs, the balance is depleted; when 429 occurs, the limit is exceeded.
<?php
require __DIR__ . '/shippickup.php';
$api = new ShipPickup(); // key from the SHIPPICKUP_API_KEY environment variable
foreach ($orders as $id => $order) { // $id is your order number, $order is the API body: from, to, parcel, items, declared_value_cents
$request = ['from' => $order['from'], 'to' => $order['to'], 'parcel' => $order['parcel'], 'declared_value_cents' => $order['declared_value_cents']];
try {
$rate = $api->rates($request)['data'][0]; // sorted by price, the first is the cheapest
$shipment = $api->create($order + ['rate_id' => $rate['rate_id']], ShipPickup::idempotencyKey('order-' . $id));
echo $id, ' ', $shipment['data']['tracking_number'], "\n";
} catch (ShipPickupException $e) {
if ($e->status === 402) { break; } // top up the balance and run again, finished orders will not be repeated
if ($e->status === 429) { sleep(60); } // limit reached: wait and re-run
fwrite(STDERR, $id . ' ' . $e->errorCode . ': ' . $e->getMessage() . "\n");
}
}import time
from shippickup import ShipPickup, ShipPickupError
api = ShipPickup() # key from the SHIPPICKUP_API_KEY environment variable
for order_id, order in orders.items(): # order_id is your order number, order is the API body: from, to, parcel, items, declared_value_cents
request = {k: order[k] for k in ("from", "to", "parcel", "declared_value_cents")}
try:
rate = api.rates(request)["data"][0] # sorted by price, the first is the cheapest
shipment = api.create({**order, "rate_id": rate["rate_id"]}, ShipPickup.idempotency_key("order-" + str(order_id)))
print(order_id, shipment["data"]["tracking_number"])
except ShipPickupError as e:
if e.status == 402:
break # top up the balance and run again, finished orders will not be repeated
if e.status == 429:
time.sleep(60) # limit reached: wait and re-run
print(order_id, e.code, e)import { ShipPickup, ShipPickupError } from "./shippickup.js";
const api = new ShipPickup(); // key from the SHIPPICKUP_API_KEY environment variable
for (const [id, order] of Object.entries(orders)) { // id is your order number, order is the API body: from, to, parcel, items, declared_value_cents
const { from, to, parcel, declared_value_cents } = order;
try {
const rate = (await api.rates({ from, to, parcel, declared_value_cents })).data[0]; // the cheapest
const shipment = await api.create({ ...order, rate_id: rate.rate_id }, ShipPickup.idempotencyKey(`order-${id}`));
console.log(id, shipment.data.tracking_number);
} catch (e) {
if (!(e instanceof ShipPickupError)) throw e;
if (e.status === 402) break; // top up the balance and run again, finished orders will not be repeated
if (e.status === 429) await new Promise((r) => setTimeout(r, 60000)); // limit reached: wait and re-run
console.error(id, e.code, e.message);
}
}Rate Recalculation
rate_id is valid for 15 minutes. If more time has passed between calculation and purchase or the parcel has changed, a 409 rate_expired will be returned. Request rates again and repeat the purchase with the same idempotency key; the label will be purchased exactly once.
<?php
// rate_id lives 15 minutes and works only for the same parcel and route. On 409 rate_expired ask for rates again.
try {
$shipment = $api->create($order + ['rate_id' => $rateId], $key);
} catch (ShipPickupException $e) {
if ($e->errorCode !== 'rate_expired') { throw $e; }
$rate = $api->rates($request)['data'][0];
$shipment = $api->create($order + ['rate_id' => $rate['rate_id']], $key); // the same key: still one order
}# rate_id lives 15 minutes and works only for the same parcel and route. On 409 rate_expired ask for rates again.
try:
shipment = api.create({**order, "rate_id": rate_id}, key)
except ShipPickupError as e:
if e.code != "rate_expired":
raise
rate = api.rates(request)["data"][0]
shipment = api.create({**order, "rate_id": rate["rate_id"]}, key) # the same key: still one order// rate_id lives 15 minutes and works only for the same parcel and route. On 409 rate_expired ask for rates again.
let shipment;
try {
shipment = await api.create({ ...order, rate_id: rateId }, key);
} catch (e) {
if (!(e instanceof ShipPickupError) || e.code !== "rate_expired") throw e;
const rate = (await api.rates(request)).data[0];
shipment = await api.create({ ...order, rate_id: rate.rate_id }, key); // the same key: still one order
}Order Cancellation
Funds for a cancelled order are returned to the balance, the amount is in meta.refund_cents. If cancellation is no longer possible, a 409 with a reason will be returned.
<?php
$result = $api->cancel($orderId, 'Customer cancelled the order');
echo $result['meta']['refund_cents'], " cents returned to the balance\n"; // a repeated cancel is safe: meta.already_cancelled = trueresult = api.cancel(order_id, "Customer cancelled the order")
print(result["meta"]["refund_cents"], "cents returned to the balance") # a repeated cancel is safe: meta.already_cancelled = trueconst result = await api.cancel(orderId, "Customer cancelled the order");
console.log(result.meta.refund_cents, "cents returned to the balance"); // a repeated cancel is safe: meta.already_cancelled = trueStatuses Without Webhooks
If webhooks are not suitable, poll GET /shipments/{id}/tracking for open orders no more than once every few hours and remember the request limit.
Courier Pickup
The procedure is: GET /shipments/{id}/pickup_slots, select a time slot, POST /shipments/{id}/pickup with slot_id. The pickup cost is indicated in meta.fee_cents of the first response. Pickup can be cancelled using the pickup/cancel method.