TypeScript / Node.js

Integrate Ingate with TypeScript and Node.js applications using the OpenAI SDK or raw fetch.

Using the OpenAI SDK

bash
npm install openai
tsapp.ts
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.ingateai.com",
  apiKey: "sk-ingate-your-key",
  defaultHeaders: { "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

tsstream.ts
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. This works in any JavaScript runtime: Node.js, Deno, Bun, Cloudflare Workers, or browsers:

tsfetch.ts
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": "sk-ingate-your-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

tsingate-client.ts
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-Provider": provider,
  };
  if (options?.translate) {
    headers["X-Ingate-Translate"] = "true";
  }

  return new OpenAI({
    baseURL: process.env.INGATE_URL ?? "https://api.ingateai.com",
    apiKey: process.env.INGATE_KEY ?? "",
    defaultHeaders: headers,
  });
}

// Usage
const openai = createIngateClient("openai");
const claude = createIngateClient("anthropic", { translate: true });

Next.js Route Handler

tsapp/api/chat/route.ts
import OpenAI from "openai";
import { NextResponse } from "next/server";

const client = new OpenAI({
  baseURL: process.env.INGATE_URL,
  apiKey: process.env.INGATE_KEY,
  defaultHeaders: { "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,
  });
}