TypeScript / Node.js
Integrate Ingate with TypeScript and Node.js applications using the OpenAI SDK or raw fetch.
Configure the provider credential in Ingate for API-key injection. SetINGATE_KEY privately on your server to a key issued by your gateway. The SDK sends it in X-Ingate-Key. The apiKey: "unused"value satisfies the SDK; Ingate replaces upstream auth with the provider credential. If setting INGATE_URL, include /v1, for examplehttp://localhost:7100/v1. Streaming snippets reuse the authenticated client.
For an OpenAI provider configured with auth_passthrough: true, useapiKey: process.env.OPENAI_API_KEY! instead of "unused". Keep X-Ingate-Key: upstream Bearer auth does not authenticate the gateway.
Using the OpenAI SDK
npm install openaiimport OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.ingateai.com/v1",
apiKey: "unused",
defaultHeaders: { "X-Ingate-Key": process.env.INGATE_KEY!, "X-Ingate-Provider": "openai" },
});
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is an AI gateway?" },
],
});
console.log(response.choices[0].message.content);Streaming
const stream = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Tell me a story" }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}Using fetch()
No SDK required. Run this on your backend; do not expose gateway keys in browser bundles:
const response = await fetch(
"https://api.ingateai.com/v1/chat/completions",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Ingate-Provider": "openai",
"X-Ingate-Key": process.env.INGATE_KEY!,
},
body: JSON.stringify({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello" }],
}),
}
);
const data = await response.json();
console.log(data.choices[0].message.content);
// Ingate headers
console.log("Request ID:", response.headers.get("X-Ingate-Request-Id"));Provider Helper
import OpenAI from "openai";
type Provider = "openai" | "anthropic" | "azure-gpt4" | string;
export function createIngateClient(
provider: Provider,
options?: { translate?: boolean }
): OpenAI {
const headers: Record<string, string> = {
"X-Ingate-Key": process.env.INGATE_KEY!,
"X-Ingate-Provider": provider,
};
if (options?.translate) {
headers["X-Ingate-Translate"] = "true";
}
return new OpenAI({
baseURL: process.env.INGATE_URL ?? "https://api.ingateai.com/v1",
apiKey: "unused",
defaultHeaders: headers,
});
}
// Usage
const openai = createIngateClient("openai");
const claude = createIngateClient("anthropic", { translate: true });Next.js Route Handler
import OpenAI from "openai";
import { NextResponse } from "next/server";
const client = new OpenAI({
baseURL: process.env.INGATE_URL ?? "https://api.ingateai.com/v1",
apiKey: "unused",
defaultHeaders: { "X-Ingate-Key": process.env.INGATE_KEY!, "X-Ingate-Provider": "openai" },
});
export async function POST(req: Request) {
const { message } = await req.json();
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: message }],
});
return NextResponse.json({
reply: response.choices[0].message.content,
});
}