Code Examples
Ready-to-run samples in Python, JavaScript, and curl.
Switch from OpenAI in 3 small edits — base URL, API key, and the model id (tier-1 … tier-4). Text chat completions only; tool calling isn't supported yet.
Python (OpenAI SDK)
Use the official OpenAI Python client pointed at the TierUp.AI base URL.
python
from openai import OpenAI
client = OpenAI(
base_url="https://api.tierup.ai/v1", # ← Changed
api_key="tup_your_api_key_here", # ← Changed
)
response = client.chat.completions.create(
model="tier-2", # ← Changed — pick tier-1 … tier-4
messages=[{"role": "user", "content": "Explain quantum computing"}],
)
print(response.choices[0].message.content)Python with Streaming
Enable streaming to receive tokens as they are generated.
python
stream = client.chat.completions.create(
model="tier-1",
messages=[{"role": "user", "content": "Write a poem"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")JavaScript (fetch)
Works in any environment that supports the Fetch API — browsers, Node.js, Deno, and edge runtimes.
javascript
const response = await fetch("https://api.tierup.ai/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer tup_your_api_key_here",
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: "Hello!" }],
tier: 1,
}),
});
const data = await response.json();
console.log(data.choices[0].message.content);JavaScript with Streaming
Read the response body as a stream and parse each server-sent event line.
javascript
const response = await fetch("https://api.tierup.ai/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer tup_your_api_key_here",
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: "Hello!" }],
tier: 2,
stream: true,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
for (const line of chunk.split("\n")) {
if (line.startsWith("data: ") && line !== "data: [DONE]") {
const data = JSON.parse(line.slice(6));
process.stdout.write(data.choices?.[0]?.delta?.content || "");
}
}
}curl
The quickest way to test from a terminal without any dependencies.
bash
curl -X POST https://api.tierup.ai/v1/chat/completions \
-H "Authorization: Bearer tup_your_api_key_here" \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Hello!"}], "tier": 1}'curl with Streaming
Pass -N to disable curl's output buffering so you see tokens as they arrive.
bash
curl -N -X POST https://api.tierup.ai/v1/chat/completions \
-H "Authorization: Bearer tup_your_api_key_here" \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Hello!"}], "tier": 2, "stream": true}'Install the OpenAI SDK
Python:
bash
pip install openai
Node.js:
bash
npm install openai