Examples
Integrate with the stack you already use.
Each example is complete and copy-ready. Add your key as an environment variable and pick a model ID from the catalog.
Basic chat completion
The smallest useful request. Swap the model ID to route anywhere in the catalog.
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.PRBYCODE_API_KEY,
baseURL: "https://api.prbycode.com/v1",
});
const completion = await client.chat.completions.create({
model: "glm-5.3-flash",
messages: [{ role: "user", content: "Explain request routing in two sentences." }],
});
console.log(completion.choices[0].message.content);Streaming to a terminal
Incremental deltas arrive as server-sent events. Print them as they land to keep perceived latency low.
const stream = await client.chat.completions.create({
model: "glm-5.3-flash",
messages: [{ role: "user", content: "Write a short changelog entry." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}Tool calling
Describe a function and let the model decide when to call it. Tool definitions are passed through unchanged.
tools = [{
"type": "function",
"function": {
"name": "get_usage",
"description": "Return token usage for a model",
"parameters": {
"type": "object",
"properties": {"model": {"type": "string"}},
"required": ["model"],
},
},
}]
response = client.chat.completions.create(
model="glm-5.3-flash",
messages=[{"role": "user", "content": "How many tokens did glm-5.3-flash use?"}],
tools=tools,
tool_choice="auto",
)
for call in response.choices[0].message.tool_calls or []:
print(call.function.name, call.function.arguments)Structured JSON output
Ask for a JSON object and validate it against your own schema before it reaches application code.
import json
response = client.chat.completions.create(
model="glm-5.3-flash",
messages=[
{"role": "system", "content": "Return only JSON."},
{"role": "user", "content": "Summarise this ticket: login fails on Safari."},
],
response_format={"type": "json_object"},
)
payload = json.loads(response.choices[0].message.content)
print(payload["summary"], payload["severity"])Catch a failure and retry elsewhere
Retry on 5xx and 429 by naming a different model, so a provider incident does not become an outage.
const chain = ["claude-sonnet-5", "glm-5.3-flash"];
async function complete(messages) {
let lastError;
for (const model of chain) {
try {
return await client.chat.completions.create({ model, messages });
} catch (error) {
if (![429, 500, 502, 503, 504].includes(error.status)) throw error;
lastError = error;
}
}
throw lastError;
}Server-side call from PHP
Any language with an HTTP client can reach the gateway. This is a plain cURL call from PHP.
<?php
$payload = json_encode([
"model" => "glm-5.3-flash",
"messages" => [["role" => "user", "content" => "Hello from PHP"]],
]);
$ch = curl_init("https://api.prbycode.com/v1/chat/completions");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . getenv("PRBYCODE_API_KEY"),
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => $payload,
]);
$response = json_decode(curl_exec($ch), true);
echo $response["choices"][0]["message"]["content"];Anthropic message shape
Clients written against the messages API can point at the same base URL.
curl https://api.prbycode.com/v1/messages \
-H "x-api-key: $PRBYCODE_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-5",
"max_tokens": 512,
"messages": [{ "role": "user", "content": "Hello" }]
}'Environment setup
Store the key outside your repository. Never inline a production key in client-side code.
export PRBYCODE_API_KEY="prby_live_…"
export PRBYCODE_BASE_URL="https://api.prbycode.com/v1"
# verify the key is live
curl "$PRBYCODE_BASE_URL/models" -H "Authorization: Bearer $PRBYCODE_API_KEY"Need the exact request fields?
The API reference documents every parameter, response field and error type.