Node.js¶
Works in Node 18+ (built-in fetch) and modern browsers.
Minimal client¶
```js // cnw.js const ENDPOINT = process.env.CNW_ENDPOINT || "https://api.cutweaver.io"; const API_KEY = process.env.CNW_API_KEY;
export async function solve(payload) {
const res = await fetch(${ENDPOINT}/v1/solve, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(API_KEY ? { "X-API-Key": API_KEY } : {}),
},
body: JSON.stringify(payload),
});
const data = await res.json(); if (!data.ok) { const err = new Error(data.error?.message ?? "CutWeaver error"); err.code = data.error?.code; err.status = res.status; throw err; } return data.result; } ```
Usage¶
```js import { solve } from "./cnw.js";
const result = await solve({ sheets: [{ width: 3210, height: 2250 }], pieces: [ { width: 800, height: 600, quantity: 4 }, { width: 1200, height: 900, quantity: 2 }, ], params: { kerf: 4 }, strategy: "greedy", });
console.log(Utilization: ${(result.metrics.totalUtilization * 100).toFixed(1)}%);
for (const sheet of result.sheets) {
console.log(Sheet ${sheet.index}: ${sheet.placements.length} pieces);
}
```
Retry on 429 / 5xx¶
js
async function solveWithRetry(payload, { maxAttempts = 3 } = {}) {
let lastErr;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await solve(payload);
} catch (e) {
lastErr = e;
if (e.status !== 429 && e.status < 500) throw e;
await new Promise(r => setTimeout(r, 2 ** attempt * 500));
}
}
throw lastErr;
}