JavaScript SDK
Official JavaScript and TypeScript client for the LatentKit /v1 API.
Official ESM package for Node 18+ and other runtimes with fetch, ReadableStream, and AbortController.
Install
npm install @latentkit/sdkQuickstart
import { LatentKit } from '@latentkit/sdk';
const client = new LatentKit({
apiKey: process.env.LATENTKIT_API_KEY!,
});
const response = await client.chat.create({
messages: [{ role: 'user', content: 'Say hello from LatentKit.' }],
max_tokens: 100,
response_profile: 'balanced',
});
console.log(response.content);Client options
| Option | Description |
|---|---|
apiKey | Required API key |
baseUrl | Defaults to https://ai.latentkit.com (normalized to /v1) |
headers | Extra request headers |
fetch | Custom fetch implementation |
timeoutMs | Default 120000 |
Route-based requests
Do not pass model, provider, route, or policy. The SDK rejects route-control keys, including inside extra_body. The assigned route selects the provider/model at runtime.
Errors
import { LatentKit, LatentKitApiError } from '@latentkit/sdk';
try {
await client.chat.create({
messages: [{ role: 'user', content: 'hello' }],
});
} catch (error) {
if (error instanceof LatentKitApiError) {
console.error({
status: error.status,
code: error.code,
request_id: error.request_id,
body: error.body,
});
}
}Return safe errors to your frontend. Do not forward raw provider messages if they may contain internal configuration details.
Streaming
for await (const event of client.chat.stream({
messages: [{ role: 'user', content: 'Count from one to five.' }],
})) {
if (event.event === 'error') throw new Error(JSON.stringify(event.data));
if (event.isDone) break;
console.log(event.data);
}Supported resources
client.chat.create/client.chat.streamclient.completions.create/client.completions.streamclient.vision.create/client.vision.streamclient.embeddings.createclient.image.generateclient.speech.create,client.transcription.create,client.translation.createclient.video.generateclient.queue.createclient.agents.sessions.create
See also Python SDK and REST API overview.
Framework notes
- Next.js: keep the client in a server-only module and call it from Route Handlers or Server Actions.
- React/Vite: browser code should call your backend or serverless function; it should not import the SDK with a raw key.
- Express/Fastify/Nest: create one shared client factory, validate incoming request bodies, and centralize LatentKit error logging.