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

# OpenAI SDK

> Drop-in replacement for OpenAI with Nordlys model

## Quick Setup

1. [Sign up for an Nordlys API key](https://nordlyslabs.com/api-platform/orgs)

2. **Install the OpenAI SDK** (if you haven't already):

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

```bash yarn   theme={null}
yarn add openai
```

```bash pnpm theme={null}
pnpm add openai
```

2. **Change two lines** in your existing code:

```javascript JavaScript/Node.js theme={null}
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'your-nordlys-api-key',        // ← Your Nordlys API key
  baseURL: 'https://api.nordlyslabs.com/v1' // ← Only change needed
});

// Everything else works exactly the same
const completion = await openai.chat.completions.create({
  model: 'nordlys/hypernova',
  messages: [{ role: 'user', content: 'Hello!' }]
});
```

```python Python theme={null}
from openai import OpenAI

client = OpenAI(
    api_key="your-nordlys-api-key",        # ← Your Nordlys API key  
    base_url="https://api.nordlyslabs.com/v1" # ← Only change needed
)

# Everything else works exactly the same
completion = client.chat.completions.create(
    model="",
    messages=[{"role": "user", "content": "Hello!"}]
)
```

That's it! Your existing OpenAI code now uses Nordlys model.

## Key Features

> [card] Same API
>
> All OpenAI methods, parameters, and responses work identically

> [card] Model Model selection
>
> Set `model` to `nordlys/hypernova` for the Nordlys model

> [card] Streaming
>
> Streaming responses work exactly like OpenAI

> [card] Function Calling
>
> Function calling and tools work without changes

## Streaming Example

```javascript JavaScript theme={null}
const stream = await openai.chat.completions.create({
  model: 'nordlys/hypernova',
  messages: [{ role: 'user', content: 'Tell me a story' }],
  stream: true,
});

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

```python Python theme={null}
stream = client.chat.completions.create(
    model="",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")
```

## Response Format

Nordlys returns standard OpenAI responses:

```json theme={null}
{
  "id": "chatcmpl-123",
  "choices": [{
    "message": {"content": "Hello! How can I help you?"}
  }],
  "usage": {"total_tokens": 21}
}
```

## Error Handling

Handle common errors gracefully:

```javascript theme={null}
try {
  const completion = await openai.chat.completions.create({
    model: 'nordlys/hypernova',
    messages: [{ role: 'user', content: 'Hello!' }]
  });
  console.log(completion.choices[0].message.content);
} catch (error) {
  if (error.status === 401) {
    console.error('Invalid API key');
  } else if (error.status === 429) {
    console.error('Rate limit exceeded - try again later');
  } else {
    console.error('API error:', error.message);
  }
}
```

**Common Issues:**

* **401**: Check your Nordlys API key
* **429**: Rate limit exceeded - implement retry logic
* **400**: Invalid request parameters

## Need More Control?

> [card] [API Reference](https://docs.nordlyslabs.com/api-reference/chat-completions.md)
>
> See all available parameters and options

> [card] [Examples](https://docs.nordlyslabs.com/examples/basic-chat)
>
> Working code examples and use cases


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.