Change the base URL in the OpenAI Node SDK

Set baseURL on the OpenAI Node client to reach the gateway from a server: the import style, the env behavior, and a check the setting loaded before the client.

Checked 2026-11-22

·All guides·Models

A Node service reaches UplinkSwitch through the OpenAI SDK's custom-endpoint option: construct the client with the base https://api.uplinkswitch.com/v1, plus a gateway key and a model id that is available, then confirm the request resolves to the chat route. This page is the server-side Node and TypeScript reference, which differs from the Python one in option spelling and in how the process loads its configuration. Keep the key server-side; none of this belongs in browser code. Pin the SDK version you build against, because an option name is stated for a release, not forever. The option, the variable and the precedence below were checked on 2026-11-22 against openai 7.62.0 for Node, with the example typechecked and its request captured by a local listener.

Construct the client with baseURL

The Node client takes the custom endpoint as the baseURL option on construction. The import is the default export, and the chat call is awaited at the top level, so run it as an ES module (a .mjs file or a package with "type": "module").

typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.uplinkswitch.com/v1",
  apiKey: process.env.UPLINKSWITCH_API_KEY,
});

const completion = await client.chat.completions.create({
  model: "gpt-6.1-sol",
  messages: [{ role: "user", content: "One sentence on backpressure." }],
});
console.log(completion.choices[0].message.content);

The base ends in /v1, and the client appends the resource, so the request lands on /v1/chat/completions. Reading the key from process.env keeps it out of the source and out of any bundle. The model in the example, gpt-6.1-sol, was ok when the catalog dated 2026-11-23 was read.

Why Python config does not transplant

The Node option is baseURL, with the camel-case spelling, not the Python base_url. A typechecked TypeScript build refuses the snake-case name as an unknown property, but plain JavaScript, or TypeScript run without a typecheck, silently leaves the option unset, so the client falls back to OPENAI_BASE_URL or, without it, its default host, the key is rejected there, and it looks like a bad key when the base simply never changed. Spell the option the Node way, and the environment variable the SDK reads is OPENAI_BASE_URL; in 7.62.0 an explicit baseURL wins over that variable, and the variable is read when the client is constructed.

Load the environment before the client

A server client captures its configuration at construction. If you load environment values from a file, do that load before you create the client, or the client is built against whatever was set earlier, often nothing. In practice: run the environment setup at the top of the entry point, construct the client after it, and if you build more than one client, give each the base it needs rather than assuming a shared default. For the generic multi-SDK setup see the SDKs guide; the Python equivalent of this page is the OpenAI Python guide; and a higher-level TypeScript toolkit is a different SDK, covered by the Vercel AI SDK guide.

Confirm the destination

Confirm the path the same careful way: log the outgoing request, or aim one call at a tiny local receiver that reports what arrived, and check for POST /v1/chat/completions; blank the authorization header before any log line. A captured path beats trusting the option name.

Questions and answers

The baseURL option on new OpenAI({ ... }), set to https://api.uplinkswitch.com/v1. The client appends the resource, so the request resolves to /v1/chat/completions.

Because the option is named differently. Node uses baseURL; Python uses base_url. A typecheck flags the snake-case name; without one, it is ignored, the client falls back to OPENAI_BASE_URL or its default host, and the request never reaches the gateway unless that variable points here.

Load environment values at the top of the entry point, construct the client after that, and confirm by logging the outgoing request's host along with its POST /v1/chat/completions path (authorization header redacted), since the path alone is the same on the default host. A client built before the setting loaded keeps whatever base it saw then, often the default host.
Join on Telegram
Help