Community exampleNeeds reviewAvailable2026-09-23
TypeSafe REST API
A dependency-light server-side REST example with bearer authentication, bounded exponential backoff, request timeout, response validation, and human review.
Server environment variables
TYPESAFE_API_KEYPrerequisites
- A server runtime with fetch and AbortSignal.timeout
- A TypeSafe API key
- Access to the current API schema
Implementation steps
- 1Load the API key from the server environment.
- 2Create a typed-decision request body.
- 3Send POST /v1/systemone with a ten-second attempt timeout.
- 4Retry only transient HTTP failures and cap retries.
- 5Reject non-success responses and malformed payloads.
- 6Send low-confidence outcomes to human review.
Server example
const API_URL = 'https://api.typesafe.ai/v1/systemone';
const MAX_RETRIES = 2;
const apiKey = process.env.TYPESAFE_API_KEY;
if (!apiKey) throw new Error('TYPESAFE_API_KEY is required');
const requestBody = {
model: 'jev-latest',
state: { ticket: 'I was charged twice. Please fix this ASAP.' },
questions: {
route: {
type: 'choice',
instructions: 'Choose the support queue.',
criteria: { billing: null, technical: null, other: null },
},
},
};
const retryable = (status) =>
status === 408 || status === 429 || status >= 500;
let response;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt += 1) {
response = await fetch(API_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
signal: AbortSignal.timeout(10_000),
});
if (response.ok || !retryable(response.status) || attempt === MAX_RETRIES) {
break;
}
await new Promise((resolve) => setTimeout(resolve, 500 * 2 ** attempt));
}
if (!response?.ok) {
throw new Error(`TypeSafe request failed with HTTP ${response?.status}`);
}
const payload = await response.json();
const decision = payload?.answers?.route;
if (
typeof decision?.choice !== 'string' ||
!Number.isFinite(decision?.confidence) ||
decision.confidence < 0 ||
decision.confidence > 1
) {
throw new Error('Unexpected decision response');
}
const action = decision.confidence < 0.8 ? 'human_review' : decision.choice;
console.log({ action, confidence: decision.confidence, model: payload.model });Production checks
- Review the live OpenAPI schema before copying this community-maintained example.
- Add request identifiers and metrics without logging private input.
- Respect provider rate limits and Retry-After guidance.
- Use idempotency safeguards around downstream side effects.
Limitations and verification notes
This REST sample is maintained by Type Safe and is marked needs review until it is run against a pinned live API revision.
Sources
- TypeSafe AI JavaScript SDKTypeSafe AI · 2026-09-23Open source
- TypeSafe API referenceTypeSafe AI · 2026-09-23Open source
This guide is independently maintained by Type Safe. Check the provider’s current documentation, versions, and terms before use.