JavaScript & TypeScript SDK integrations
You have a JavaScript or TypeScript service calling an LLM, and it needs Policy-as-Code, audit, PII redaction and cost attribution in front of it before it can ship. Every SDK below reaches that by changing baseURL, and governance applies from the first request. Examples assume DVARA is running at http://localhost:8080 and that you have created a workspace and an API key via the Quickstart.
OpenAI JavaScript
Install:
npm install openai
Basic chat completion:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "http://localhost:8080/v1",
apiKey: "your-dvara-api-key",
});
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain microservices in one paragraph." },
],
});
console.log(response.choices[0].message.content);
Streaming:
const stream = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Write a poem about code." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
Structured output:
import { zodResponseFormat } from "openai/helpers/zod";
import { z } from "zod";
const Planet = z.object({
name: z.string(),
diameter_km: z.number(),
has_rings: z.boolean(),
});
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Tell me about Saturn." }],
response_format: zodResponseFormat(Planet, "planet"),
});
const planet = JSON.parse(response.choices[0].message.content!);
console.log(`${planet.name}: ${planet.diameter_km} km, rings: ${planet.has_rings}`);
Tool calls:
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "What's the weather in London?" }],
tools: [
{
type: "function",
function: {
name: "get_weather",
description: "Get weather for a city",
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
},
],
});
const toolCall = response.choices[0].message.tool_calls?.[0];
if (toolCall) {
console.log(`Call: ${toolCall.function.name}(${toolCall.function.arguments})`);
}
Environment variable approach:
export OPENAI_BASE_URL="http://localhost:8080/v1"
export OPENAI_API_KEY="your-dvara-api-key"
Anthropic JavaScript
Same approach as Python — use the OpenAI SDK with Claude model names. DVARA translates internally.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "http://localhost:8080/v1",
apiKey: "your-dvara-api-key",
});
const response = await client.chat.completions.create({
model: "claude-sonnet-4-20250514",
messages: [{ role: "user", content: "Hello from TypeScript!" }],
});
Vercel AI SDK
Install:
npm install ai @ai-sdk/openai
Usage:
import { createOpenAI } from "@ai-sdk/openai";
import { generateText, streamText } from "ai";
const dvara = createOpenAI({
baseURL: "http://localhost:8080/v1",
apiKey: "your-dvara-api-key",
});
// Non-streaming
const { text } = await generateText({
model: dvara("gpt-4o"),
prompt: "Explain REST APIs briefly.",
});
// Streaming
const result = streamText({
model: dvara("gpt-4o"),
prompt: "Write a short story about a robot.",
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
Using Claude via the Vercel AI SDK:
const response = await generateText({
model: dvara("claude-sonnet-4-20250514"),
prompt: "Compare TypeScript to Rust in three sentences.",
});
Next steps
- Looking for other languages? See Python or Java.
- Need distributed tracing across SDKs? See DVARA headers.
- Migrating from direct provider calls or another gateway? See the Migration guide.
What a rejection looks like
Putting DVARA in front of your provider changes the failure cases more than the success ones: a
call can now be refused by a budget cap, a policy, a guardrail or PII enforcement. Every refusal uses
the same envelope, so one handler covers all of them — the shape, the full code list and the
trace_id contract are documented once in Error handling.
{
"error": {
"message": "No provider configured for model: nope/does-not-exist. Check GET /v1/models for available models.",
"type": "invalid_request_error",
"code": "no_provider",
"trace_id": "043793a6ac754c9c9d1bc5d4f525f827"
}
}
import OpenAI from 'openai';
try {
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{role: 'user', content: 'Summarise this contract.'}],
});
console.log(response.choices[0].message.content);
} catch (err) {
if (err instanceof OpenAI.APIError) {
const body = (err.error ?? {}) as {code?: string; message?: string; trace_id?: string};
if (err.status === 402) console.error(`Budget stopped this call: ${body.message}`);
else if (err.status === 403) console.error(`Governance refused this call: ${body.code}`);
else throw err;
console.error(`Quote this trace id to support: ${body.trace_id}`);
} else {
throw err;
}
}