SwissAI API
Un’API chat completions compatibile con OpenAI. Crea una chiave nel portale, imposta il token Bearer e parti — gli SDK OpenAI esistenti funzionano cambiando base_url.
Guida rapida
Tre passi per la prima risposta:
1. Nella dashboard sviluppatori crea una chiave sotto API Keys. Viene mostrata una sola volta — conservala al sicuro. Formato: sk-souheng-…
2. Invia una richiesta all’endpoint di chat:
curlcurl -N https://api.swiss-ai.one/api/v1/external/chat/completions \
-H "Authorization: Bearer sk-souheng-TUA_CHIAVE" \
-H "Content-Type: application/json" \
-d '{
"messages": [{ "role": "user", "content": "Saluta in svizzero tedesco." }],
"stream": true
}'
3. Ricevi uno stream SSE compatibile con OpenAI (vedi Streaming).
Autenticazione
Autentica ogni richiesta con la tua chiave API nell’header Authorization:
httpAuthorization: Bearer sk-souheng-TUA_CHIAVE
model nel corpo della richiesta viene ignorato — puoi inviare un valore qualsiasi (comodo per SDK OpenAI non modificati).Le chiavi API si gestiscono esclusivamente nel portale (creare, elencare, revocare). Massimo 20 chiavi attive per account.
URL di base
| Scopo | URL |
|---|---|
| Base API | https://api.swiss-ai.one |
| Chat Completions | /api/v1/external/chat/completions |
SDK OpenAI base_url | https://api.swiss-ai.one/api/v1/external |
POST/api/v1/external/chat/completions
Genera una risposta di chat. Lo streaming è attivo per impostazione predefinita.
Corpo della richiesta
| Campo | Tipo | Default | Descrizione |
|---|---|---|---|
messages obbligatorio | array | — | Elenco di { "role", "content" }. Ruoli: system, user, assistant, tool. |
stream | boolean | true | Streaming SSE on/off. |
temperature | number | 0.7 | 0.0–2.0. |
max_tokens | integer | 4096 | 1–128000. |
tools | array | null | Definizioni di tool in formato OpenAI. |
tool_choice | string | null | es. "auto". |
model è accettato ma ignorato (il modello deriva dalla chiave). I campi sconosciuti vengono ignorati.Streaming (SSE)
Con "stream": true l’API restituisce text/event-stream in formato chunk OpenAI. Lo stream termina con 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]
Senza streaming
Con "stream": false ricevi un singolo oggetto chat.completion:
json{
"id": "chatcmpl-1a2b3c4d",
"object": "chat.completion",
"created": 1750000000,
"model": "swissai",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "Ciao! Come posso aiutarti?" },
"finish_reason": "stop"
}
]
}
Tool / function calling
Passa i tool in formato OpenAI. Quando il modello chiama un tool, tornano tool_calls (in streaming come non). Rinvia il risultato come messaggio tool con il tool_call_id corrispondente.
json{
"messages": [{ "role": "user", "content": "Che tempo fa a Zurigo?" }],
"stream": false,
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Meteo attuale di una città",
"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-TUA_CHIAVE"},
json={"messages": [{"role": "user", "content": "Ciao"}], "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-TUA_CHIAVE",
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: "Ciao" }],
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);
}
}
SDK OpenAI
Gli SDK OpenAI esistenti funzionano impostando base_url e api_key. model è un campo obbligatorio dell’SDK ma viene ignorato lato server.
pythonfrom openai import OpenAI
client = OpenAI(
api_key="sk-souheng-TUA_CHIAVE",
base_url="https://api.swiss-ai.one/api/v1/external",
)
stream = client.chat.completions.create(
model="swissai", # qualsiasi — ignorato
messages=[{"role": "user", "content": "Ciao"}],
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-TUA_CHIAVE",
baseURL: "https://api.swiss-ai.one/api/v1/external",
});
const stream = await client.chat.completions.create({
model: "swissai", // qualsiasi — ignorato
messages: [{ role: "user", content: "Ciao" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
Fatturazione e limiti
- Le richieste sono addebitate a token sul tuo saldo (wallet).
- Saldo e utilizzo li vedi nella dashboard, dove puoi anche ricaricare.
- Massimo 20 chiavi API attive per account.
Codici di errore
| Stato | Significato |
|---|---|
401 | Chiave API mancante, non valida o inattiva. |
402 | Saldo esaurito — ricarica nella dashboard. |
404 | Percorso / endpoint sconosciuto. |
429 | Limite di frequenza superato — riprova più tardi. |
5xx | Problema temporaneo del server / upstream. |