// Madely quickstart (TypeScript, Node 22+ with built-in fetch). Sandbox: nothing is printed, shipped or charged. // Run: MADELY_BASE=https://api.madely.ai node --experimental-strip-types quickstart.ts (or: npx tsx quickstart.ts) const BASE: string = process.env.MADELY_BASE ?? "https://api.madely.ai"; interface ApiError { code: string; message: string; hint?: string; retryable: boolean; retry_after?: number | null } async function call(method: string, path: string, body?: unknown, key?: string, idem?: string): Promise { const headers: Record = { Accept: "application/json" }; if (body !== undefined) headers["Content-Type"] = "application/json"; if (key) headers["Authorization"] = `Bearer ${key}`; if (idem) headers["Idempotency-Key"] = idem; const res = await fetch(BASE + path, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) }); const json: any = await res.json(); if (!res.ok) { const e: ApiError = json.error; // Read hint, fix param, retry only if retryable (wait retry_after seconds when present). throw new Error(`${res.status} ${e.code}: ${e.message} | hint: ${e.hint} | retryable=${e.retryable}`); } return json as T; } async function main(): Promise { const product = (await call("GET", "/v1/products?q=mug&limit=1")).products[0]; // 1 discover + find console.log("product", product.product_id, `$${product.price_usd}`); const key: string = (await call("POST", "/v1/sandbox/keys")).api_key; // 2 sandbox key, no signup const spec = await call("GET", "/v1/products/K04/print-spec"); // 3 print spec console.log("canvas", spec.canvas_px, "dpi", spec.dpi); const check = await call("POST", "/v1/print-spec/validate", { product_id: "K04", width_px: 2550, height_px: 1155, format: "png", has_transparency: false }); // 4 validate console.log("valid", check.valid); const quote = await call("POST", "/v1/quotes", { items: [{ product_id: "K04", quantity: 24 }], ship_to: { country: "US", zip: "80202" } }); // 5 quote await call("POST", "/v1/payment-methods", { payment_method_id: "pm_card_visa" }, key); // sandbox test card const id = `madely-smoketest-ts-${process.pid}`; const order = await call("POST", "/v1/orders", { // 6 test order quote_id: quote.quote_id, external_id: id, submit: true, payment: { method: "card_on_file" }, ship_to: { name: "Test Buyer", address1: "1 Test St", city: "Denver", state: "CO", zip: "80202", country: "US" }, items: [{ product_id: "K04", quantity: 24, artwork: [{ placement: "wrap", url: "https://example.com/art/mug.png", width_px: 2550, height_px: 1155 }] }], }, key, id); const status = await call("GET", `/v1/orders/${order.order_id}`, undefined, key); // 7 status console.log("order", order.order_id, status.status, `total=$${status.total_usd}`); } main().catch((e: Error) => { console.error(e.message); process.exit(1); });