JavaScript & TypeScript SDK integrations
DVARA is an AI governance platform with an OpenAI-compatible data plane. Every JavaScript / TypeScript SDK below works by changing baseURL — Policy-as-Code, audit, PII redaction, and cost attribution all kick in on the first request. Examples assume DVARA is running at http://localhost:8080 and you've created a tenant + 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.