Idempotency

When purchasing a label, you can't make the mistake twice: connection drops or timeouts leave uncertainty about whether the label was purchased. The Idempotency-Key header makes retries safe.

Rules

Example

curl -X POST https://api.shippickup.com/v1/shipments \
  -H "Authorization: Bearer $SHIPPICKUP_API_KEY" \
  -H "Idempotency-Key: order-10452-label-v1" \
  -H "Content-Type: application/json" \
  -d '{
    "rate_id": "<rate_id>",
    "from": {
        "name": "Acme Store",
        "company": "Acme Inc",
        "line1": "350 5th Ave",
        "city": "New York",
        "state": "NY",
        "postal_code": "10001",
        "country": "US",
        "phone": "+12125550100",
        "email": "shipping@example.com"
    },
    "to": {
        "name": "Anna Schmidt",
        "line1": "Unter den Linden 1",
        "city": "Berlin",
        "postal_code": "10117",
        "country": "DE",
        "phone": "+493012345678",
        "email": "anna@example.com"
    },
    "parcel": {
        "weight_g": 800,
        "length_mm": 250,
        "width_mm": 180,
        "height_mm": 80
    },
    "items": [
        {
            "description": "T-shirts",
            "quantity": 2,
            "unit_value_cents": 2500,
            "origin_country": "US"
        }
    ],
    "declared_value_cents": 5000,
    "reference": "ORDER-10452"
}'

What a retry returns

The same order with a 200 status (first creation returns 201) and the flag meta.duplicate: true:

JSON (200)
{
    "data": {
        "id": "4f2a9c1e7b3d48a6c5e0f1d2b3",
        "status": "label_ready"
    },
    "meta": {
        "duplicate": true
    }
}

How to create a key

Create the key from your system's order number: this way, rerunning the script won't purchase a second label. If the numbers are short, add a prefix to reach at least 16 characters. Ready-made clients (PHP, Node.js, Python) have an idempotencyKey function for this.

PHP
<?php
// The same store order always gives the same key: a repeated run will not buy a second label.
$idempotencyKey = 'shop-order-' . $orderId . '-label';   // 16-64 characters: A-Z a-z 0-9 _ -

If the request was interrupted

Do not create a new key. Repeat the same request with the same key: you will get the already created order or it will be created once. If the rate expired (409 rate_expired), request rates again and retry the purchase with the same key.

Другие изменяющие методы

Заголовок Idempotency-Key обязателен и у остальных изменяющих методов: создание счёта на пополнение, добавление и изменение адресов, пакетная отмена, приглашения и смена ролей в команде, создание, изменение вебхуков и ротация секрета. Ключ действует в пределах аккаунта 24 часа: повтор с тем же ключом и тем же запросом возвращает сохранённый успешный ответ (200, meta.duplicate: true, заголовок Idempotent-Replay: true) и ничего не выполняет второй раз; тот же ключ с другим запросом даёт 422 idempotency_key_reused. Ответы с ошибкой не сохраняются, секреты (секрет подписи вебхука) в сохранённый ответ не попадают. Отмена одного заказа и удаление безопасны для повтора сами по себе.