# API

!!! tip "Looking for plain model access?"

    If you only want to send requests to a language model from your own code, use
    [Aqueduct](https://datalab.tuwien.ac.at/aiml/aqueduct/) instead. It is the dedicated API gateway for TU Wien AI services.
    The Open WebUI API is mainly useful when you want to work with Open WebUI features, such as your
    knowledge bases, from a script.

## Getting Started

### Step 1: Get a Personal API Key

API keys for Open WebUI are **not enabled by default**. If you need one, contact us in the
[Matrix room](https://matrix.to/#/#llm-service:tuwien.ac.at) and tell us what you want to use it for. Once it is enabled
for your account:

1. Navigate to **Settings** (gear icon) in Open WebUI
2. Select the **Account** tab
3. Click **"Create API Key"**
4. Copy the displayed key and store it securely (e.g., in a password manager)

**Security Warning:** Treat your API key **like a password!** Never share it with others or write it in publicly accessible code.

### Step 2: Endpoints

All endpoints are relative to `https://chat.ai.datalab.tuwien.ac.at`:

| What | Endpoint |
| --- | --- |
| List models (including your assistants) | `GET /api/models` |
| Chat completions | `POST /api/chat/completions` |
| Upload a file | `POST /api/v1/files/` |
| Add a file to a knowledge base | `POST /api/v1/knowledge/{id}/file/add` |

See [API endpoints](https://docs.openwebui.com/reference/api-endpoints) in the official documentation for details and
more endpoints.

### Step 3: Making API Requests

**cURL example:**

```bash
curl -X POST https://chat.ai.datalab.tuwien.ac.at/api/chat/completions \
  -H "Authorization: Bearer $OPENWEBUI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen-3.8-27b",
    "messages": [
      {"role": "user", "content": "Explain the principle of Rayleigh scattering in three sentences."}
    ]
  }'
```

**Python example (with the `openai` library):**

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://chat.ai.datalab.tuwien.ac.at/api",
    api_key=os.environ["OPENWEBUI_API_KEY"],
)

chat_completion = client.chat.completions.create(
    model="qwen-3.8-27b",
    messages=[
        {"role": "user", "content": "Explain the principle of Rayleigh scattering in three sentences."},
    ],
)
print(chat_completion.choices[0].message.content)
```

## Knowledge Bases (RAG)

The API is the way to automate bulk uploads to a [knowledge base](https://datalab.tuwien.ac.at/aiml/openwebui/documents/#knowledge-bases): upload each file with
`POST /api/v1/files/`, then add it to the knowledge base with `POST /api/v1/knowledge/{id}/file/add`.

## Best Practices

- **Keep keys out of code:** read them from environment variables
- **Handle errors:** expect authentication errors (`401`), rate limiting (`429`) and unknown model IDs
- **Large jobs:** for large-scale automated processing, use [Aqueduct](https://datalab.tuwien.ac.at/aiml/aqueduct/) and ask for a service account in the [Matrix room](https://matrix.to/#/#llm-service:tuwien.ac.at)