社区示例需要复核可用2026-09-23
TypeSafe REST API
依赖较少的服务端 REST 接入示例,包含 Bearer 认证、有限指数退避、请求超时、响应校验和人工复核。
服务端环境变量
TYPESAFE_API_KEY准备事项
- 支持 fetch 和 AbortSignal.timeout 的服务端运行环境
- TypeSafe API Key
- 当前版本的 API Schema
接入步骤
- 1从服务端环境变量读取 API Key。
- 2创建类型化决策请求体。
- 3调用 POST /v1/systemone,并设置十秒的单次请求超时。
- 4只重试临时 HTTP 错误,并限制重试次数。
- 5拒绝非成功响应和结构异常的数据。
- 6把低置信度结果交给人工复核。
服务端示例
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 });生产检查
- 复制这份社区维护示例前,请核对在线 OpenAPI Schema。
- 增加请求标识和指标,但不要记录私密输入。
- 遵守提供方的速率限制和 Retry-After 指引。
- 下游操作可能产生副作用时增加幂等保护。
限制与核实说明
这份 REST 示例由 Type Safe 目录维护;在针对固定的在线 API 版本完成实测前,状态为需要复核。
来源
- TypeSafe AI JavaScript SDKTypeSafe AI · 2026-09-23打开来源
- TypeSafe API referenceTypeSafe AI · 2026-09-23打开来源
这份指南由 Type Safe 独立整理。使用前请核对提供方的当前文档、版本和条款。