Code Examples
These examples use the official OpenAI SDKs — they work with Schatzi AI by changing the base URL. Every snippet is copy-pasteable and ready to run.
Python (OpenAI SDK)
Install the SDK:
pip install openai
Basic Chat Completion
from openai import OpenAI
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://backend.schatziai.ch/v1"
)
response = client.chat.completions.create(
model="MODEL_ID",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Summarise the key points of FADP compliance."}
],
max_tokens=500
)
print(response.choices[0].message.content)
Streaming
stream = client.chat.completions.create(
model="MODEL_ID",
messages=[{"role": "user", "content": "Explain Swiss data sovereignty in 3 paragraphs."}],
stream=True
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
Embeddings
response = client.embeddings.create(
model="EMBEDDING_MODEL_ID",
input="Swiss financial regulations"
)
vector = response.data[0].embedding
print(f"Dimensions: {len(vector)}")
JavaScript / TypeScript (OpenAI SDK)
Install the SDK:
npm install openai
Basic Chat Completion
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-your-api-key",
baseURL: "https://backend.schatziai.ch/v1",
});
const response = await client.chat.completions.create({
model: "MODEL_ID",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Draft a client email in German and English." },
],
max_tokens: 500,
});
console.log(response.choices[0].message.content);
Streaming
const stream = await client.chat.completions.create({
model: "MODEL_ID",
messages: [{ role: "user", content: "List 5 benefits of Swiss data residency." }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}
curl
Basic Request
curl https://backend.schatziai.ch/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "MODEL_ID",
"messages": [
{"role": "user", "content": "What is FADP?"}
],
"max_tokens": 200
}'
Streaming
curl https://backend.schatziai.ch/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-N \
-d '{
"model": "MODEL_ID",
"messages": [{"role": "user", "content": "Count to 5"}],
"stream": true
}'
List Models
curl https://backend.schatziai.ch/v1/models \
-H "Authorization: Bearer sk-your-api-key"
Extended Parameters
Reasoning Control (Python)
For reasoning models, use reasoning_effort and max_completion_tokens to control the balance between speed and accuracy:
response = client.chat.completions.create(
model="MODEL_ID",
messages=[{"role": "user", "content": "Solve this step by step: what is 247 * 389?"}],
extra_body={
"reasoning_effort": "high",
"max_completion_tokens": 16384,
}
)
Set reasoning_effort to "low" for fast tool-calling workflows, or "high" for complex multi-step reasoning.
Structured JSON Output (Python)
Request structured output using response_format:
response = client.chat.completions.create(
model="MODEL_ID",
messages=[{"role": "user", "content": "List 3 Swiss cities with population"}],
extra_body={
"response_format": {
"type": "json_object"
}
}
)
import json
data = json.loads(response.choices[0].message.content)
Deterministic Output (curl)
Use seed for reproducible results:
curl https://backend.schatziai.ch/v1/chat/completions \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "MODEL_ID",
"messages": [{"role": "user", "content": "Generate a UUID"}],
"seed": 42,
"max_tokens": 50
}'
Extended parameters (reasoning_effort, max_completion_tokens, response_format, seed, logprobs, top_logprobs, parallel_tool_calls) are passed through to the inference backend. Not all models support all parameters — if a model doesn't support a parameter, the API returns a 400 error. See Endpoint Reference for the full parameter list.
Migrating from OpenAI
If you have existing code that uses the OpenAI API, switching to Schatzi AI requires only two changes:
| Setting | OpenAI | Schatzi AI |
|---|---|---|
| Base URL | https://api.openai.com/v1 | https://backend.schatziai.ch/v1 |
| API Key | sk-... (OpenAI key) | sk-... (Schatzi AI key from your dashboard) |
Model IDs differ between platforms. Use GET /v1/models to discover available models.
Environment Variable Pattern (recommended)
export OPENAI_API_KEY="sk-your-schatzi-key"
export OPENAI_BASE_URL="https://backend.schatziai.ch/v1"
The OpenAI Python and JS SDKs read these environment variables automatically — no code changes needed beyond setting these variables.
Error Handling (Python)
from openai import OpenAI, APIError, RateLimitError, AuthenticationError
client = OpenAI(
api_key="sk-your-api-key",
base_url="https://backend.schatziai.ch/v1"
)
try:
response = client.chat.completions.create(
model="MODEL_ID",
messages=[{"role": "user", "content": "Hello"}]
)
print(response.choices[0].message.content)
except AuthenticationError:
print("Invalid API key. Check your key in the dashboard.")
except RateLimitError:
print("Usage limit reached. Check your billing period.")
except APIError as e:
print(f"API error: {e.message}")
Tips
- Store your API key in an environment variable, not in source code.
- Use streaming for long responses to improve perceived latency.
- Set
max_tokensto control response length and cost. - Use system messages to set consistent behaviour across requests.
- See Model Comparison for choosing the right model.
- See Monitoring Usage for tracking costs.