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
- The header is required in
POST /shipments. Without it, you'll get a 422idempotency_key_requirederror. - Value: 16–64 characters from
A-Za-z0-9_-. - One key corresponds to one order in your account. Repeating with the same key returns the same order and does not charge you twice.
- Always use a new key for a new order. The body of a repeated request is not compared; the response is returned by key.
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"
}'<?php
$body = [
'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',
];
$ch = curl_init('https://api.shippickup.com/v1/shipments');
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('SHIPPICKUP_API_KEY'),
'Idempotency-Key: order-10452-label-v1',
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode($body),
]);
$response = json_decode((string) curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
print_r([$status, $response]);
import os
import requests
body = {
"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",
}
r = requests.post(
"https://api.shippickup.com/v1/shipments",
headers={
"Authorization": "Bearer " + os.environ["SHIPPICKUP_API_KEY"],
"Idempotency-Key": "order-10452-label-v1",
},
json=body,
timeout=30,
)
print(r.status_code, r.json())
const body = {
"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"
};
const res = await fetch("https://api.shippickup.com/v1/shipments", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.SHIPPICKUP_API_KEY}`,
"Idempotency-Key": "order-10452-label-v1",
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
console.log(res.status, await res.json());
What a retry returns
The same order with a 200 status (first creation returns 201) and the flag meta.duplicate: true:
{
"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
// 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. Ответы с ошибкой не сохраняются, секреты (секрет подписи вебхука) в сохранённый ответ не попадают. Отмена одного заказа и удаление безопасны для повтора сами по себе.