@gatelit/sdk
v0.1.0-beta.3
Published
TypeScript client for the Gatelit AI gateway — chat, stream, and structured output across any LLM provider
Maintainers
Readme
@gatelit/sdk
TypeScript client for the Gatelit AI gateway — chat, streaming, tool calling, and structured output across any LLM provider through a single API.
import { GatelitClient } from "@gatelit/sdk"
const client = new GatelitClient({
gatewayUrl: "https://gateway.gatelit.dev",
getToken: async () => ({ token: "glk_...", scheme: "GatelitKey" }),
})
const response = await client.chat({
model: "openai/gpt-4o",
messages: [{ role: "user", content: "Hello" }],
})
console.log(response.content)Features
- Universal client — one API for OpenAI, Anthropic, Google, Mistral, and more
- Streaming — async generator-based token-by-token output
- Tool calling — send
tools/toolChoice, receivetoolCallsin responses - Structured output — typed JSON responses with
chatJson<T>() - Multi-turn conversations — automatic history management with
Conversation - Retry — configurable exponential backoff for transient failures
- Zero dependencies — only the Web Crypto API (Node 18+, browser, edge)
- Dual CJS/ESM — works everywhere: Node.js, Deno, Bun, Cloudflare Workers, browsers
- Lightweight sign subpath — import
@gatelit/sdk/signforexchangeToken()without the client
Install
npm install @gatelit/sdk
# or
pnpm add @gatelit/sdk
# or
yarn add @gatelit/sdkQuick start
import { GatelitClient } from "@gatelit/sdk"
const client = new GatelitClient({
gatewayUrl: "https://gateway.gatelit.dev",
getToken: async () => {
// Option A: long-lived service key (backend only)
return { token: process.env.GATELIT_SERVICE_KEY!, scheme: "GatelitKey" }
// Option B: short-lived signed token (browser apps)
// const res = await fetch("/api/ai-token")
// const { token } = await res.json()
// return { token, scheme: "GatelitSigned" }
},
})
// Chat
const res = await client.chat({
model: "openai/gpt-4o",
messages: [{ role: "user", content: "What is the capital of France?" }],
})
console.log(res.content, res.usage)
// Streaming
for await (const chunk of client.stream({
model: "anthropic/claude-3-5-sonnet-20241022",
messages: [{ role: "user", content: "Tell me a story" }],
})) {
process.stdout.write(chunk.delta)
}
// Tool calling
const toolRes = await client.chat({
model: "openai/gpt-4o",
messages: [{ role: "user", content: "What's the weather in Boston?" }],
tools: [{
type: "function",
function: {
name: "get_weather",
description: "Get current weather",
parameters: {
type: "object",
properties: { location: { type: "string" } },
},
},
}],
})
if (toolRes.toolCalls) {
console.log(toolRes.toolCalls[0].function.name) // "get_weather"
}
// Typed JSON
const data = await client.chatJson<{ name: string; age: number }>({
model: "openai/gpt-4o",
messages: [{ role: "user", content: "Alice is 30" }],
outputSchema: { mode: "json_object" },
})
console.log(data.name, data.age)
// Retry on transient failures
const resilient = new GatelitClient({
gatewayUrl: "https://gateway.gatelit.dev",
getToken: async () => ({ token: "glk_...", scheme: "GatelitKey" }),
maxRetries: 3,
retryDelay: 1000,
})Documentation
Full docs at docs.gatelit.dev/sdk/typescript/overview.
License
MIT
