Get API key
Guides / Labels & dispatch

Labels & dispatch

In live mode, POST /v1/shipments books with the carrier synchronously. This page explains how to check for a label source, download the PDF, wait for carrier-side generation, and distinguish test-mode behavior.

When the label_pdf_url is populated

In live mode the create call runs quote → persist → dispatch synchronously. When dispatch returns successfully, dispatch.status becomes "dispatched". When a direct source exists, dispatch.label_pdf_url contains a directly-downloadable URL (a carrier-native presigned OSS URL, or — for carriers whose label we store ourselves — a time-limited signed link). Partner-channel shipments can have no URL and use the authenticated endpoint below instead.

A failed live carrier booking returns 422; read the top-level message and sysCode. After a successful 201, GET /v1/shipments/{id} exposes label_available, label_pdf_url and the flat dispatch_status field. Test mode skips the carrier and returns dispatch: null; do not poll a test shipment waiting for a real label.

Check the source and wait for label generation

If the 201 response already contains dispatch.label_pdf_url, download it directly. Otherwise check shipment detail: label_available: true means a source exists. If label_pdf_url is still null, use GET /v1/shipments/{shipment_id}/label with Authorization. A partner channel may still be generating the PDF; the endpoint returns 503 with Retry-After, so wait that many seconds before retrying. If a live booking succeeded but no source has appeared, check detail every 1s for up to 30s; do not decide whether to poll from an assumed dispatch-status value.

If the label is still unavailable after 30s, or the create call timed out with an unclear outcome, contact support to confirm the carrier-side state. Do not cancel the order or switch to another service_tier yourself, which risks duplicate bookings. When retrying the original create request, keep the same idempotency key; GET queries themselves do not require that header.

bash
# For a successfully created live shipment; check the detail response.
TN="IM2605288471293048"
for attempt in $(seq 1 30); do
  resp=$(curl -fsS "https://imileexpress.com/api/v1/shipments/$TN" \
    -H "Authorization: Bearer mk_live_***")
  if [[ $? -ne 0 ]]; then break; fi
  url=$(echo "$resp" | jq -r '.data.label_pdf_url // empty')
  dispatch_state=$(echo "$resp" | jq -r '.data.dispatch_status // empty')
  label_source=$(echo "$resp" | jq -r '.data.label_available')
  live_mode=$(echo "$resp" | jq -r '.data.livemode')
  if [[ "$live_mode" == "false" ]]; then echo "Test mode: no real label"; break; fi
  if [[ "$label_source" == "true" ]]; then
    if [[ -n "$url" ]]; then
      echo "Direct PDF URL is ready; use the download example below."
    else
      echo "Use GET /api/v1/shipments/$TN/label with Authorization."
      echo "If it returns 503, wait for Retry-After before retrying."
    fi
    break
  fi
  if [[ "$dispatch_state" == "failed" || "$dispatch_state" == "skipped_no_creds" || "$dispatch_state" == "skipped_internal" ]]; then break; fi
  sleep 1
done

Fetching the PDF

label_pdf_url is a directly-downloadable URL (a carrier-native presigned OSS URL, or — for carriers whose label we store ourselves — a time-limited signed link) — fetch it directly, no Authorization header needed. Best practice: re-upload to your own object storage right after download. The URL has an expiry either way; once it lapses you need to fetch a fresh one from /v1/shipments/{id}.

bash
# Direct download — no auth header.
curl -fL "$url" -o "label-$TN.pdf"

# Or pipe through to your storage:
curl -fL "$url" | aws s3 cp - "s3://my-labels/$TN.pdf"

Partner-carrier-channel shipments: label_pdf_url stays null

Some shipments are booked through a partner carrier channel (e.g. certain third-party delivery arrangements). For these, label_pdf_url stays permanently null — this is not a temporary label-generation delay; there is simply no presigned OSS URL for these shipments to hand out. Check the new label_available boolean instead (present on both GET /v1/shipments and GET /v1/shipments/{id}) to know whether a label SOURCE exists at all — but label_available:true is not a guarantee the bytes are downloadable at this exact instant: for a partner-channel shipment, the authenticated endpoint below may briefly return 503 with a Retry-After header right after dispatch while the carrier finishes generating the PDF. Poll again after that many seconds — it is not a permanent failure.

Authenticated download endpoint

GET /v1/shipments/{shipment_id}/label works for every carrier and channel, including the partner-channel shipments above. Unlike label_pdf_url, this endpoint requires Authorization: Bearer and the response body is raw PDF bytes (not the JSON envelope). Poll it alongside label_available using the same pattern described above for label_pdf_url (every 1s, up to 30s); for a partner-channel shipment, a call shortly after dispatch may return 503 with a Retry-After header while the label is still generating (not a permanent error) — poll again after that many seconds.

bash
curl -fL "https://imileexpress.com/api/v1/shipments/$TN/label" \
  -H "Authorization: Bearer mk_live_***" \
  -o "label-$TN.pdf"

Failure semantics: 404 means the shipment doesn't exist / isn't yours / isn't in this mode, or has no label at all; 502 means the label exists but the download failed — retry shortly; 503 (partner-channel shipments only) means the label has not finished generating yet — also a retry-shortly condition, never a permanent failure.

Which number do I use?

A shipment carries up to three numbers — don't mix them up:

FieldLooks likeUse it for
tracking_numberIM-2026-...iMile's canonical number — use it against our API (GET /api/v1/shipments/{id}) and our buyer-facing tracking page.
waybill_numberYT...First-leg carrier waybill (e.g. Yun Express YT…), assigned at dispatch.
last_mile_tracking_number9214… (USPS)Final-mile carrier number (e.g. USPS). Once present, THIS is the number to push to eBay / Amazon and show the buyer — the delivering carrier's own tracker recognises it. Null until the last-mile leg is assigned.

Practical rule: prefer last_mile_tracking_number for marketplaces once it's non-null; fall back to waybill_number while it's still null.

Buyer-facing tracking page

Want to show buyers the progress? The public tracking page is at https://imileexpress.com/track?no={tracking_number} (pass tracking_number, not waybill_number).

Test-mode behaviour

When you create with an mk_test_* key, the dispatch step is skipped entirely: no carrier API call, no wallet deduction, no label_pdf_url. The response shows dispatch = null.

Want to test the PDF-download flow in staging? Mock a 200-byte PDF (e.g. https://www.example-files.com/test.pdf) and stub your fetcher. We don't yet have a sandbox carrier that returns a fake label_pdf_url; coming in the next sprint.