OpenAI JavaScript SDK
Call Kendr from Node.js or TypeScript with Responses, custom fields, and explicit error handling.
Install the client
npm install openai
Set KENDR_API_KEY in your server environment. Save the next example as hello.mjs and run node hello.mjs with a Node.js version supported by the installed SDK. Never expose the key in a browser bundle.
Make a chat request
import OpenAI from "openai";
import { randomUUID } from "node:crypto";
const client = new OpenAI({
apiKey: process.env.KENDR_API_KEY,
baseURL: "https://api.kendr.org/v1",
timeout: 180_000,
maxRetries: 0,
});
const requestId = randomUUID(); // Persist before sending for recovery.
const response = await client.chat.completions.create({
model: "kendr-intelligent",
messages: [{ role: "user", content: "Explain API keys in two sentences." }],
}, { headers: { "Idempotency-Key": requestId } });
if (response.choices[0].finish_reason !== "stop") throw new Error("Incomplete response");
console.log(response.choices[0].message.content);
Call Responses and read usage
const result = await client.responses.create({
model: "kendr-intelligent", input: "Write a three-step launch checklist.",
}, { headers: { "Idempotency-Key": randomUUID() } });
console.log(result.output_text);
TypeScript does not declare Kendr extensions in upstream types. Narrow the value when reading additional fields:
const receipt = result as typeof result & { kendr_usage?: unknown };
console.log(receipt.kendr_usage);
Send Kendr-specific fields
JavaScript can include extensions directly. In TypeScript, declare the extra fields while retaining upstream request validation:
const request: OpenAI.Responses.ResponseCreateParamsNonStreaming & {
web_search: boolean;
} = {
model: "kendr-intelligent",
input: "Find the latest Python release notes and include sources.",
web_search: true,
};
const answer = await client.responses.create(request, {
headers: { "Idempotency-Key": randomUUID() },
});
console.log(answer.output_text);
This block is TypeScript; remove the type annotation in a .mjs file. For custom tools see tools and connected apps. For complete control over request fields, use fetch.
Handle errors at your server boundary
Catch OpenAI.APIError and inspect error.status; connection failures may have no HTTP status. Return an application error to the UI without exposing credentials. Disable automatic retries until you have implemented idempotency and recovery. Continue with streaming. SDK mechanics are described in the official OpenAI quickstart.