SwissAI API
Eine OpenAI-kompatible Chat-Completions-API. Key im Portal erstellen, Bearer-Token setzen, loslegen — bestehende OpenAI-SDKs funktionieren mit geändertem base_url.
Quickstart
In drei Schritten zur ersten Antwort:
1. Im Developer-Dashboard unter API Keys einen Schlüssel erstellen. Er wird nur einmal angezeigt — sicher speichern. Format: sk-souheng-…
2. Anfrage an den Chat-Endpoint senden:
curlcurl -N https://api.swiss-ai.one/api/v1/external/chat/completions \
-H "Authorization: Bearer sk-souheng-DEIN_KEY" \
-H "Content-Type: application/json" \
-d '{
"messages": [{ "role": "user", "content": "Sag Hallo auf Schweizerdeutsch." }],
"stream": true
}'
3. Du erhältst einen OpenAI-kompatiblen SSE-Stream zurück (siehe Streaming).
Authentifizierung
Jede Anfrage authentifizierst du mit deinem API-Key im Authorization-Header:
httpAuthorization: Bearer sk-souheng-DEIN_KEY
model im Request-Body wird ignoriert — du kannst einen beliebigen Wert senden (praktisch für unveränderte OpenAI-SDKs).API-Keys werden ausschliesslich im Portal verwaltet (erstellen, auflisten, widerrufen). Maximal 20 aktive Keys pro Konto.
Basis-URL
| Zweck | URL |
|---|---|
| API-Basis | https://api.swiss-ai.one |
| Chat Completions | /api/v1/external/chat/completions |
OpenAI-SDK base_url | https://api.swiss-ai.one/api/v1/external |
POST/api/v1/external/chat/completions
Erzeugt eine Chat-Antwort. Standardmässig wird gestreamt.
Request-Body
| Feld | Typ | Default | Beschreibung |
|---|---|---|---|
messages erforderlich | array | — | Liste aus { "role", "content" }. Rollen: system, user, assistant, tool. |
stream | boolean | true | SSE-Streaming an/aus. |
temperature | number | 0.7 | 0.0–2.0. |
max_tokens | integer | 4096 | 1–128000. |
tools | array | null | Tool-Definitionen im OpenAI-Format. |
tool_choice | string | null | z. B. "auto". |
model wird akzeptiert, aber ignoriert (das Modell ergibt sich aus dem Key). Unbekannte Felder werden ignoriert.Streaming (SSE)
Bei "stream": true liefert die API text/event-stream im OpenAI-Chunk-Format. Der Stream endet mit data: [DONE].
ssedata: {"id":"chatcmpl-1a2b3c4d","object":"chat.completion.chunk","created":1750000000,"model":"swissai","choices":[{"index":0,"delta":{"content":"Hallo"},"finish_reason":null}]}
data: {"id":"chatcmpl-1a2b3c4d","object":"chat.completion.chunk","created":1750000000,"model":"swissai","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}
data: {"id":"chatcmpl-1a2b3c4d","object":"chat.completion.chunk","created":1750000000,"model":"swissai","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
Ohne Streaming
Mit "stream": false erhältst du ein einzelnes chat.completion-Objekt:
json{
"id": "chatcmpl-1a2b3c4d",
"object": "chat.completion",
"created": 1750000000,
"model": "swissai",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "Hallo! Wie kann ich helfen?" },
"finish_reason": "stop"
}
]
}
Tools / Function Calling
Übergib Tools im OpenAI-Format. Ruft das Modell ein Tool auf, kommen tool_calls zurück (Streaming wie Non-Streaming). Das Ergebnis sendest du als tool-Nachricht mit passender tool_call_id zurück.
json{
"messages": [{ "role": "user", "content": "Wie ist das Wetter in Zürich?" }],
"stream": false,
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Aktuelles Wetter für eine Stadt",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
}],
"tool_choice": "auto"
}
Python
requests (Streaming)
pythonimport json, requests
resp = requests.post(
"https://api.swiss-ai.one/api/v1/external/chat/completions",
headers={"Authorization": "Bearer sk-souheng-DEIN_KEY"},
json={"messages": [{"role": "user", "content": "Hallo"}], "stream": True},
stream=True,
)
for line in resp.iter_lines():
if not line:
continue
line = line.decode()
if line.startswith("data: "):
data = line[6:]
if data == "[DONE]":
break
delta = json.loads(data)["choices"][0]["delta"]
print(delta.get("content", ""), end="", flush=True)
JavaScript / Node
javascriptconst res = await fetch(
"https://api.swiss-ai.one/api/v1/external/chat/completions",
{
method: "POST",
headers: {
"Authorization": "Bearer sk-souheng-DEIN_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: "Hallo" }],
stream: true,
}),
}
);
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
for (;;) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop();
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const data = line.slice(6);
if (data === "[DONE]") { reader.cancel(); break; }
const delta = JSON.parse(data).choices[0].delta;
if (delta.content) process.stdout.write(delta.content);
}
}
OpenAI-SDK
Bestehende OpenAI-SDKs funktionieren, indem du base_url und api_key setzt. model ist Pflichtfeld der SDKs, wird serverseitig aber ignoriert.
pythonfrom openai import OpenAI
client = OpenAI(
api_key="sk-souheng-DEIN_KEY",
base_url="https://api.swiss-ai.one/api/v1/external",
)
stream = client.chat.completions.create(
model="swissai", # beliebig — wird ignoriert
messages=[{"role": "user", "content": "Hallo"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
javascriptimport OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-souheng-DEIN_KEY",
baseURL: "https://api.swiss-ai.one/api/v1/external",
});
const stream = await client.chat.completions.create({
model: "swissai", // beliebig — wird ignoriert
messages: [{ role: "user", content: "Hallo" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
Abrechnung & Limits
- Anfragen werden tokenbasiert deinem Guthaben (Wallet) belastet.
- Guthaben und Nutzung siehst du im Dashboard; dort kannst du auch aufladen.
- Maximal 20 aktive API-Keys pro Konto.
Fehlercodes
| Status | Bedeutung |
|---|---|
401 | API-Key fehlt, ist ungültig oder inaktiv. |
402 | Guthaben aufgebraucht — im Dashboard aufladen. |
404 | Unbekannter Pfad / Endpoint. |
429 | Rate-Limit überschritten — später erneut versuchen. |
5xx | Vorübergehendes Server-/Upstream-Problem. |