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

# JavaScript SDK

> Use Yelu AI API with the official OpenAI JavaScript SDK in Node.js for chat, responses, and streaming.

The official OpenAI JavaScript and TypeScript SDK works with Yelu by changing the API key and base URL. Use it in trusted server-side runtimes, not client-side browser bundles.

## Install

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
npm install openai
```

## Configure the client

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.YELU_API_KEY,
  baseURL: 'https://api.yelu.ai/v1',
  timeout: 60_000,
  maxRetries: 2,
});
```

## Chat Completions

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const completion = await client.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [
    { role: 'system', content: 'Be concise.' },
    { role: 'user', content: 'Explain vector search.' },
  ],
  max_completion_tokens: 200,
});

console.log(completion.choices[0].message.content);
console.log(completion.usage);
```

## Responses

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const response = await client.responses.create({
  model: 'gpt-4o-mini',
  instructions: 'Answer for a software engineer.',
  input: 'What is backpressure?',
  max_output_tokens: 200,
});

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

## Stream output

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const stream = await client.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [{ role: 'user', content: 'Write a short release note.' }],
  stream: true,
});

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

## Handle errors

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
try {
  await client.models.list();
} catch (error) {
  if (error instanceof OpenAI.AuthenticationError) {
    console.error('Check YELU_API_KEY');
  } else if (error instanceof OpenAI.RateLimitError) {
    console.error('Back off before retrying');
  } else if (error instanceof OpenAI.APIConnectionError) {
    console.error('Network or TLS failure', error.cause);
  } else if (error instanceof OpenAI.APIError) {
    console.error(error.status, error.code, error.message);
  } else {
    throw error;
  }
}
```

<Warning>
  The SDK can be imported in browser tooling, but a Yelu key must not be shipped to users. Call Yelu from your authenticated backend.
</Warning>

See the [Streaming example](/en/examples/streaming), [Function calling guide](/en/guides/function-calling), and [Errors](/en/errors) for complete integration patterns.
