> ## 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):

<CodeGroup>
  ```bash npm theme={null}
  npm install openai
  ```

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

  ```bash pnpm theme={null}
  pnpm add openai
  ```
</CodeGroup>

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

<CodeGroup>
  ```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!"}]
  )
  ```
</CodeGroup>

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

## Key Features

<CardGroup cols={2}>
  <Card title="Same API" icon="check">
    All OpenAI methods, parameters, and responses work identically
  </Card>

  <Card title="Model Model selection" icon="brain">
    Set `model` to `nordlys/hypernova` for the Nordlys model
  </Card>

  <Card title="Streaming" icon="zap">
    Streaming responses work exactly like OpenAI
  </Card>

  <Card title="Function Calling" icon="function">
    Function calling and tools work without changes
  </Card>
</CardGroup>

## Streaming Example

<CodeGroup>
  ```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="")
  ```
</CodeGroup>

## 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?

<CardGroup cols={2}>
  <Card title="API Reference" href="/api-reference/chat-completions" icon="book">
    See all available parameters and options
  </Card>

  <Card title="Examples" href="/examples/basic-chat" icon="code">
    Working code examples and use cases
  </Card>
</CardGroup>
