# JavaScript SDK

> Use ScaleMax from JavaScript or TypeScript with OpenAI-compatible clients, secure environment variables, and streaming.

Use the official OpenAI Node SDK and point it at ScaleMax. Model IDs come from the [models and pricing](https://docs.scalemax.pro/docs/models) page, and every request field is listed in [Chat Completions](https://docs.scalemax.pro/docs/chat-completions).

Set `SCALEMAX_BASE_URL` to `https://api.scalemax.pro/v1` for API credit or `https://api.scalemax.pro/token/v1` for an API-token key. This is the OpenAI-compatible route; native Claude apps use their origin-only setup instead.

**macOS / Linux**

```bash
# API-credit key:
export SCALEMAX_BASE_URL="https://api.scalemax.pro/v1"
# API-token key:
# export SCALEMAX_BASE_URL="https://api.scalemax.pro/token/v1"
```

**Windows PowerShell**

```powershell
# API-credit key:
$env:SCALEMAX_BASE_URL = "https://api.scalemax.pro/v1"
# API-token key:
# $env:SCALEMAX_BASE_URL = "https://api.scalemax.pro/token/v1"
```

## Install

**macOS / Linux**

```bash
npm install openai
```

**Windows PowerShell**

```powershell
npm.cmd install openai
```

Windows PowerShell blocks npm’s .ps1 launchers by default (“running scripts is disabled on this system”), so these commands use the .cmd launcher: npx.cmd, npm.cmd, scalemax.cmd. In Command Prompt you can leave out .cmd.

## Basic chat

client.ts:

```ts
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.SCALEMAX_API_KEY,
  baseURL: process.env.SCALEMAX_BASE_URL,
});

const response = await client.chat.completions.create({
  model: "YOUR_AUTHENTICATED_MODEL_ID",
  messages: [{ role: "user", content: "Write a haiku about fast inference." }],
});

console.log(response.choices[0]?.message?.content);
```

## Streaming

stream.ts:

```ts
const stream = await client.chat.completions.create({
  model: "YOUR_AUTHENTICATED_MODEL_ID",
  messages: [{ role: "user", content: "Stream a short greeting." }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
```

Server-sent-event framing and safe disconnect handling are covered in [Streaming](https://docs.scalemax.pro/docs/streaming). The same flow in other languages: [Python](https://docs.scalemax.pro/docs/examples/python), [cURL](https://docs.scalemax.pro/docs/examples/curl).
