Grok API

Grok API with TypeScript and Node.js

View as Markdown

Updated:

A server-side Grok 4.5 integration for TypeScript and Node.js using the OpenAI package. The examples use the Responses API, typed events, streaming, and explicit error handling.

Install the TypeScript SDK

Install the OpenAI package in your Node.js project and keep the lockfile in version control. Load the API key from a server environment variable rather than a source file.

npm
npm install openai
.env
GROK_API_DEV_KEY=sk-lg-YOUR_API_KEY

The baseURL value already ends with /v1. The SDK adds /responses for the method call.

Send the first request

The OpenAI SDK returns a typed response object. Use output_text for the collected answer and usage when you need token accounting.

TypeScript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.GROK_API_DEV_KEY,
  baseURL: "https://api.llm-gate.tech/v1",
  timeout: 3_600_000,
  maxRetries: 2,
});

const response = await client.responses.create({
  model: "grok-4.5",
  input: "Explain the event loop in two sentences.",
});

console.log(response.output_text);
console.log(response.usage);

Top-level await works in ESM projects. In CommonJS or an older build setup, place the call inside an async function.

Keep the key on the server

Do not create this client in a React Client Component or expose GROK_API_DEV_KEY through a public environment variable. Call Grok from a server route, server action, worker, or separate backend.

Next.js route
// app/api/grok/route.ts
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.GROK_API_DEV_KEY,
  baseURL: "https://api.llm-gate.tech/v1",
});

export async function POST(request: Request) {
  const { input } = await request.json();
  const response = await client.responses.create({
    model: "grok-4.5",
    input,
  });

  return Response.json({ text: response.output_text });
}

Return only the data the browser needs. Logs should not contain the API key or full private prompts.

Stream output in Node.js

Set stream: true and iterate over the returned async stream. Text arrives in response.output_text.delta events; response.completed contains the final response and usage.

TypeScript streaming
const stream = await client.responses.create({
  model: "grok-4.5",
  input: "Write a short TypeScript example.",
  stream: true,
});

for await (const event of stream) {
  if (event.type === "response.output_text.delta") {
    process.stdout.write(event.delta);
  }
}

When a browser disconnects from your endpoint, stop the upstream request if your runtime supports cancellation.

Handle errors and retries

OpenAI.APIError exposes the HTTP status and error name. Retry 429 and temporary 5xx with a limit, but fix 400, 401, 403, or 404 before sending the request again.

TypeScript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.GROK_API_DEV_KEY,
  baseURL: "https://api.llm-gate.tech/v1",
  maxRetries: 2,
  timeout: 3_600_000,
});
  • Set a timeout that is shorter than the timeout of your reverse proxy.
  • Limit simultaneous requests with a queue or semaphore.
  • Do not log GROK_API_DEV_KEY or complete private input.
  • Store the response ID when support needs to trace a failed request.

API errors

Related pages