---
title: "Integrate OpenAI JavaScript"
description: "Run the E2E-tested OpenAI JavaScript client by changing only Base URL and inference key."
---

> Documentation Index
> Fetch the complete documentation index at: https://nexus.microvoid.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate OpenAI JavaScript

## Goal

把已有 OpenAI JavaScript Client 接到一条明确的 Nexus ProviderConnection。

## Outcome

应用只改 Base URL 和 Key；请求的 `model`、Messages 和 Provider-native Payload 原样到达上游。

## Prerequisites

- 应用交付的 Gateway Base URL 和一次性 Inference Key。
- 目标 Provider 接受的原始模型名。
- Node.js 22.12+。

## Lifecycle note

Inference Key 解析到既有 WorkspaceAccess。更换 Key 不创建新 Access 或 QuotaPeriod；更换 Base URL 中的 UUID 是显式选择另一条 ProviderConnection。

## 1. Install


```sh title="Terminal"
npm install openai@6.48.0
```


## 2. Configure


```sh title="Terminal"
export NEXUS_BASE_URL="https://gateway.nexus.microvoid.io/providers/00000000-0000-0000-0000-000000000141"
export NEXUS_API_KEY="<NEXUS_INFERENCE_KEY>"
export NEXUS_MODEL="provider/opaque-model-v9"
```


## 3. Run the complete client


```js title="openai-javascript.mjs"
import OpenAI from 'openai';

function requiredEnvironment(name) {
  const value = process.env[name]?.trim();
  if (!value) throw new Error(`${name} is required`);
  return value;
}

const client = new OpenAI({
  baseURL: requiredEnvironment('NEXUS_BASE_URL'),
  apiKey: requiredEnvironment('NEXUS_API_KEY'),
  maxRetries: 0,
});

const completion = await client.chat.completions.create({
  model: requiredEnvironment('NEXUS_MODEL'),
  messages: [{ role: 'user', content: 'Keep this request unchanged.' }],
  temperature: 0.37,
});

console.log(
  JSON.stringify({
    model: completion.model,
    content: completion.choices[0]?.message.content,
  }),
);
```


```sh title="Terminal"
node openai-javascript.mjs
```

## Check your work

输出包含 Provider 返回的 `model` 和正文；Console 中 Invocation 的 `requestedModel`、WorkspaceAccess 和 `providerConnectionId` 与请求一致。

## Failure modes

- `invalid_inference_key`：检查是否误用了 Provider Key 或 Management Key。
- `provider_connection_not_found`：UUID 不属于 Key 解析出的 Workspace，或连接不是 ACTIVE。
- Provider 返回模型错误：使用目标 Provider 接受的原始模型名；Nexus 不做模型映射。

## Security notes

不要把 Provider Secret 或 Management Key 放进客户端。服务端长期 Key 应通过环境或 Secret Manager 注入；浏览器和移动端不要持有长期 Inference Key。

## Next steps

需要 Streaming 时使用 [OpenAI Python Recipe](/cookbook/openai-python) 的同等模式，或阅读 [Gateway API](/api-reference/gateway)。

Source: https://nexus.microvoid.io/cookbook/openai-javascript/index.mdx
