# Use the OpenAI SDK with the i14 API

> Point the official OpenAI Python or Node.js SDK at the i14 API endpoint to use Sugar Glider in your existing code.

Source: https://i14.ai/docs/integrations/openai-sdk/

Use the official OpenAI SDK from Python or Node.js to call i14. Set the base URL to `https://api.i14.ai/v1`, pass your i14 API key, and choose a [model ID your key can use](https://i14.ai/docs/api-reference/models/). These examples use [Chat Completions](https://i14.ai/docs/api-reference/chat-completions/). Check the [API reference](https://i14.ai/docs/api-reference/overview/) before reusing code that depends on another endpoint or a model-specific feature.

## Python

Install the OpenAI Python SDK if you have not already:

```bash
pip install openai
```

### Sugar Glider - general assistant

```python
import openai

client = openai.OpenAI(
    base_url="https://api.i14.ai/v1",
    api_key="YOUR_I14_API_KEY",
)

response = client.chat.completions.create(
    model="i14-sugarglider-nemotron3-super-120b-v1",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain the difference between a process and a thread."},
    ],
    max_tokens=8192,
    stream=True,
)

for chunk in response:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
```

### Sugar Glider - coding tasks

```python
import openai

client = openai.OpenAI(
    base_url="https://api.i14.ai/v1",
    api_key="YOUR_I14_API_KEY",
)

response = client.chat.completions.create(
    model="i14-sugarglider-nemotron3-super-120b-v1",
    messages=[
        {"role": "system", "content": "You are an expert software engineer."},
        {"role": "user", "content": "Write a Python function that reverses a string."},
    ],
    max_tokens=8192,
    stream=True,
)

for chunk in response:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
```

## Node.js / TypeScript

Install the OpenAI Node.js SDK if you have not already:

```bash
npm install openai
```

### Sugar Glider - general assistant

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.i14.ai/v1",
  apiKey: "YOUR_I14_API_KEY",
});

const response = await client.chat.completions.create({
  model: "i14-sugarglider-nemotron3-super-120b-v1",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "Explain the difference between a process and a thread." },
  ],
  max_tokens: 8192,
  stream: true,
});

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

### Sugar Glider - coding tasks

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.i14.ai/v1",
  apiKey: "YOUR_I14_API_KEY",
});

const response = await client.chat.completions.create({
  model: "i14-sugarglider-nemotron3-super-120b-v1",
  messages: [
    { role: "system", content: "You are an expert software engineer." },
    { role: "user", content: "Write a TypeScript function that reverses a string." },
  ],
  max_tokens: 8192,
  stream: true,
});

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

## Environment Variables

Hardcoding your API key is fine for quick tests, but you should store it in an environment variable for any code you share or deploy.

Set the variable in your shell:

* macOS and Linux

  bash or zsh

  ```bash
  export I14_API_KEY="your-api-key-here"
  ```

* Windows

  PowerShell

  ```powershell
  $env:I14_API_KEY = "your-api-key-here"
  ```

Then read it in your code:

* Python

  ```python
  import os
  import openai

  client = openai.OpenAI(
      base_url="https://api.i14.ai/v1",
      api_key=os.environ["I14_API_KEY"],
  )
  ```

* Node.js

  ```typescript
  import OpenAI from "openai";

  const client = new OpenAI({
    baseURL: "https://api.i14.ai/v1",
    apiKey: process.env.I14_API_KEY,
  });
  ```

> **Note**
>
> You can create your API key at [platform.i14.ai](https://platform.i14.ai/). Never commit your key to source control or expose it in client-side code.

> **Tip**
>
> Set `max_tokens` on every request - if you omit it, the API stops after 100 tokens. Sugar Glider thinks before it answers, and the thinking counts toward `max_tokens`. The examples above stream the reply because a thinking model can take more than 30 seconds to finish. See the [Streaming guide](https://i14.ai/docs/api-usage/streaming/) for details.
