Napisz klienta API
Każde żądanie ma token w nagłówku Authorization: Bearer. Sandbox to sandbox-api-shipx-pl.easypack24.net, produkcja api-shipx-pl.easypack24.net. Nagłówek X-User-Agent jest opcjonalny i pomaga InPost w diagnozie, gdy zgłaszasz problem.
const API = process.env.SHIPX_URL ?? 'https://sandbox-api-shipx-pl.easypack24.net';const TOKEN = process.env.SHIPX_TOKEN ?? '';const ORG = process.env.SHIPX_ORG ?? '';export async function shipx<T>(method: string, path: string, body?: unknown): Promise<T> { const response = await fetch(`${API}${path}`, { method, headers: { Authorization: `Bearer ${TOKEN}`, 'Content-Type': 'application/json', 'X-User-Agent': 'shop-example', }, body: body === undefined ? undefined : JSON.stringify(body), }); if (!response.ok) { throw new Error(`ShipX ${method} ${path}: ${response.status} ${await response.text()}`); } return response.json() as Promise<T>;}