Häufige Szenarien
Fertige Lösungen für typische Aufgaben. Beispiele verwenden Clients in PHP, Node.js und Python (eine Datei, zum Download im Bereich „Fertige Lösungen“).
Clients: PHP · Node.js · Python
Massenkauf von Labels
Auftragszyklus: Tarif, Auswahl des günstigsten, Kauf mit idempotentem Schlüssel aus der Auftragsnummer. Das Skript kann sicher erneut ausgeführt werden: Bereits gekaufte Aufträge werden als Wiederholung zurückgegeben und nicht erneut gekauft. Bei 402 ist das Guthaben erschöpft, bei 429 ist das Limit erreicht.
<?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);
}
}Tarifrechner
rate_id ist 15 Minuten gültig. Wenn zwischen Berechnung und Kauf mehr Zeit vergangen ist oder sich das Paket geändert hat, erhalten Sie 409 rate_expired. Fordern Sie die Tarife erneut an und wiederholen Sie den Kauf mit demselben Idempotenzschlüssel, das Label wird genau einmal gekauft.
<?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
}Auftrag stornieren
Das Geld für einen stornierten Auftrag wird dem Guthaben gutgeschrieben, Betrag in meta.refund_cents. Wenn eine Stornierung nicht mehr möglich ist, erhalten Sie 409 mit Grund.
<?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 = trueStatus ohne Webhooks
Wenn Webhooks nicht passen, fragen Sie GET /shipments/{id}/tracking für offene Aufträge nicht öfter als alle paar Stunden ab und beachten Sie das Anfrage-Limit.
Abholung durch Kurier
Vorgehen: GET /shipments/{id}/pickup_slots, Zeitfenster wählen, POST /shipments/{id}/pickup mit slot_id. Die Abholgebühr steht in meta.fee_cents der ersten Antwort. Abholung kann mit Methode pickup/cancel storniert werden.