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");
    }
}

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
}

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 = true

Statuses 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.