> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crazyrouter.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js Basic Chat

> Node.js integration with Crazyrouter using the openai package

> Last updated: 2026-06-06

## Installation

```bash theme={null}
npm install openai
```

## Basic Chat

```javascript theme={null}
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'sk-xxx',
  baseURL: 'https://api.crazyrouter.com/v1',
});

async function main() {
  const response = await client.chat.completions.create({
    model: 'gpt-5.5',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: 'Explain what Node.js is in one sentence' },
    ],
  });

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

main();
```

## Streaming Output

```javascript theme={null}
async function streamChat() {
  const stream = await client.chat.completions.create({
    model: 'gpt-5.5',
    messages: [{ role: 'user', content: 'Write a poem about programming' }],
    stream: true,
  });

  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content || '';
    process.stdout.write(content);
  }
  console.log();
}

streamChat();
```

## Multi-turn Conversation

```javascript theme={null}
const messages = [
  { role: 'system', content: 'You are a JavaScript expert.' },
];

async function chat(userInput) {
  messages.push({ role: 'user', content: userInput });

  const response = await client.chat.completions.create({
    model: 'gpt-5.5',
    messages,
  });

  const reply = response.choices[0].message.content;
  messages.push({ role: 'assistant', content: reply });
  return reply;
}

async function main() {
  console.log(await chat('What is a Promise?'));
  console.log(await chat('How is it different from async/await?'));
}

main();
```

## CommonJS Usage

```javascript theme={null}
const OpenAI = require('openai');

const client = new OpenAI({
  apiKey: 'sk-xxx',
  baseURL: 'https://api.crazyrouter.com/v1',
});

client.chat.completions
  .create({
    model: 'gpt-5.5',
    messages: [{ role: 'user', content: 'Hello' }],
  })
  .then((res) => console.log(res.choices[0].message.content));
```

<Note>
  These examples default to `gpt-5.5`, which was verified successfully in Crazyrouter production on March 23, 2026. If you need to swap models, prefer other verified options such as `claude-opus-4-8` or `gemini-3.1-pro`.
</Note>
