JavaScript / TypeScript SDK
Use the Sematryx REST API from any JavaScript or TypeScript environment.
npm package coming soon. Until then, call the REST API directly using
fetch or any HTTP client — the examples below show exactly how.Submit an optimization job and poll for the result.
Optimize via REST API
// No SDK yet — use fetch() directly
const API_KEY = "smtrx_...";
const BASE = "https://api.sematryx.com";
async function optimize(expression, bounds, maxEvaluations = 1000) {
// Start optimization
const startResp = await fetch(`${BASE}/v1/optimize`, {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
objective_function: expression,
bounds,
max_evaluations: maxEvaluations,
strategy: "auto",
}),
});
if (!startResp.ok) throw new Error(`HTTP ${startResp.status}`);
const { operation_id } = await startResp.json();
// Poll until done
while (true) {
const resultResp = await fetch(
`${BASE}/v1/optimize/result/${operation_id}`,
{ headers: { Authorization: `Bearer ${API_KEY}` } }
);
const result = await resultResp.json();
if (result.status === "completed" || result.status === "failed") {
return result;
}
await new Promise((r) => setTimeout(r, 1000));
}
}
// Usage
const result = await optimize("x[0]**2 + x[1]**2", [[-5, 5], [-5, 5]]);
console.log("Optimal value:", result.optimal_value);
console.log("Solution:", result.optimal_solution);