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

# Python and Node.js SDKs

> Call APIONE from your server using the OpenAI SDK.

Configure the OpenAI SDK with the APIONE base URL, API Key, and model ID. These examples run on your server.

## Before you run the examples

Set `APIONE_API_KEY` and `APIONE_MODEL` using the [quickstart](https://apione.ai/docs/en/quickstart.md). Copy the model ID from the [available model list](https://apione.ai/docs/en/models/catalog.md). Missing variables stop the examples instead of falling back to a fixed model.

| Setting | Value |
| - | - |
| Python `base_url` / Node.js `baseURL` | `https://api.apione.ai/v1` |
| API Key | Environment variable `APIONE_API_KEY` |
| Model | Environment variable `APIONE_MODEL` |

Do not put the full `/chat/completions` endpoint in the SDK base URL. The SDK appends the endpoint path.

## Python

Install the dependency:

```bash theme={null}
python -m pip install openai
```

Save as `app.py`:

```python theme={null}
import os
from openai import OpenAI, APIStatusError, APIConnectionError

try:
    with OpenAI(
        base_url="https://api.apione.ai/v1",
        api_key=os.environ["APIONE_API_KEY"],
        timeout=60.0,
        max_retries=0,
    ) as client:
        raw = client.chat.completions.with_raw_response.create(
            model=os.environ["APIONE_MODEL"],
            messages=[{"role": "user", "content": "List three essential sections of API documentation."}],
        )
        request_ids = {name: raw.headers[name] for name in
                       ("X-Request-ID", "X-Maxx-Trace-Id", "X-Maxx-Request-ID")
                       if name in raw.headers}
        print("request identifiers:", request_ids)
        response = raw.parse()
        if response.choices:
            print(response.choices[0].message.content)
        print(response.usage)
except APIStatusError as error:
    print("HTTP status:", error.status_code)
    headers = error.response.headers
    print("request identifiers:", {name: headers[name] for name in
          ("X-Request-ID", "X-Maxx-Trace-Id", "X-Maxx-Request-ID") if name in headers})
    raise SystemExit(1) from None
except APIConnectionError:
    print("Connection failed: no HTTP response or server request ID received.")
    raise SystemExit(1) from None
```

Run `python app.py`. Successful text output is in `choices[0].message.content`; `usage` helps track tokens. Check the console for final charges.

## Node.js

Install the dependency:

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

Save as `app.mjs`:

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

const apiKey = process.env.APIONE_API_KEY;
const model = process.env.APIONE_MODEL;
if (!apiKey || !model) {
  throw new Error("Set APIONE_API_KEY and APIONE_MODEL first");
}
const client = new OpenAI({
  baseURL: "https://api.apione.ai/v1",
  apiKey,
  timeout: 60_000,
  maxRetries: 0,
});
try {
  const { data: response, response: httpResponse } = await client.chat.completions.create({
    model,
    messages: [{ role: "user", content: "List three essential sections of API documentation." }],
  }).withResponse();
  console.log("request identifiers:", Object.fromEntries(
    ["X-Request-ID", "X-Maxx-Trace-Id", "X-Maxx-Request-ID"]
      .map(name => [name, httpResponse.headers.get(name)])
      .filter(([, value]) => value)
  ));
  console.log(response.choices[0]?.message?.content);
  console.log(response.usage);
} catch (error) {
  if (error instanceof OpenAI.APIError) {
    console.error("HTTP status:", error.status ?? "No HTTP response");
    console.error("request identifiers:", Object.fromEntries(
      ["X-Request-ID", "X-Maxx-Trace-Id", "X-Maxx-Request-ID"]
        .map(name => [name, error.headers?.get(name)])
        .filter(([, value]) => value)
    ));
  } else {
    console.error("Local configuration or unexpected error; no server response confirmed.");
  }
  process.exitCode = 1;
}
```

Run `node app.mjs`. Use a Node.js version supported by the SDK and pin dependencies in your project.

### Set environment variables and run

Set your key and an actual model ID in the same terminal, then run the saved file:

```bash theme={null}
export APIONE_API_KEY="YOUR_APIONE_KEY"
export APIONE_MODEL="YOUR_APIONE_MODEL_ID"
node app.mjs
```

```powershell theme={null}
$env:APIONE_API_KEY = "YOUR_APIONE_KEY"
$env:APIONE_MODEL = "YOUR_APIONE_MODEL_ID"
node app.mjs
```

## Timeouts, retries, and troubleshooting

The examples set a 60-second SDK timeout and disable SDK retries to make a single request easier to inspect. These are example settings, not platform time limits. Set them for your workload and check SDK defaults so retries do not multiply application retries.

Record status codes, request IDs, and timing, and redact sensitive fields. On failure, verify the base URL, key, and model ID before [troubleshooting](https://apione.ai/docs/en/support/errors.md). For incremental output, see [streaming](https://apione.ai/docs/en/models/streaming.md).

Reference: [APIONE OpenAI SDK documentation](https://apione.ai/en/api-docs/sdk/sdk-openai).


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