← Docs
Code Examples
Examples for using the StepBlend Routing API in different programming languages. See the API Reference for complete endpoint documentation.
Python
Non-streaming request
import requests
url = "https://stepblend.com/api/route"
headers = {
"Authorization": "Bearer YOUR_JWT",
"Content-Type": "application/json"
}
data = {
"prompt": "Summarize this article about AI routing...",
"strategy": "balanced",
"max_cost": 0.01
}
response = requests.post(url, json=data, headers=headers)
result = response.json()
print(f"Model used: {result['model_used']}")
print(f"Cost: $" + str(result['estimated_cost']))
print(f"Task: {result.get('task_type')}, confidence: {result.get('classification_confidence')}")
print(f"Response: {result['result']}")Streaming request
import requests
import json
url = "https://stepblend.com/api/route/stream"
headers = {
"Authorization": "Bearer YOUR_JWT",
"Content-Type": "application/json"
}
data = {
"prompt": "Write a short story about...",
"strategy": "lowest_cost"
}
response = requests.post(url, json=data, headers=headers, stream=True)
for line in response.iter_lines():
if line:
line_str = line.decode('utf-8')
if line_str.startswith('data: '):
data_str = line_str[6:]
try:
event_data = json.loads(data_str)
if event_data.get('event') == 'meta':
print(f"Model: {event_data.get('model_used')}")
print(f"Task: {event_data.get('task_type')}, confidence: {event_data.get('classification_confidence')}")
print(f"Cost: $" + str(event_data.get('estimated_cost')))
elif event_data.get('event') == 'delta':
print(event_data.get('content', ''), end='', flush=True)
elif event_data.get('event') == 'done':
print("\n\nDone!")
except json.JSONDecodeError:
passRecommendation only
import requests
url = "https://stepblend.com/api/recommend"
headers = {
"Authorization": "Bearer YOUR_JWT",
"Content-Type": "application/json"
}
data = {
"prompt": "Generate Python code to parse JSON",
"strategy": "lowest_cost"
}
response = requests.post(url, json=data, headers=headers)
recommendation = response.json()
print(f"Recommended: {recommendation['provider']}/{recommendation['model_used']}")
print(f"Estimated cost: $" + str(recommendation['estimated_cost']))
print(f"Task type: {recommendation['task_type']}, confidence: {recommendation.get('classification_confidence')}")
print(f"Alternatives: {len(recommendation['alternatives'])} models")JavaScript / TypeScript
Non-streaming request
async function routePrompt(prompt: string, strategy = 'balanced') {
const response = await fetch('https://stepblend.com/api/route', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.STEPBLEND_JWT}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt,
strategy,
max_cost: 0.01,
}),
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const result = await response.json();
return result;
}
// Usage
const result = await routePrompt('Explain quantum computing');
console.log(`Model: ${result.model_used}`);
console.log(`Cost: $${result.estimated_cost}`);
console.log(`Task: ${result.task_type}, confidence: ${result.classification_confidence}`);
console.log(`Response: ${result.result}`);Streaming request
async function streamRoute(prompt: string) {
const response = await fetch('https://stepblend.com/api/route/stream', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.STEPBLEND_JWT}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ prompt, strategy: 'lowest_cost' }),
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const reader = response.body?.getReader();
const decoder = new TextDecoder();
if (!reader) return;
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = JSON.parse(line.slice(6));
if (data.event === 'meta') {
console.log(`Model: ${data.model_used}, Cost: $${data.estimated_cost}`);
} else if (data.event === 'delta') {
process.stdout.write(data.content);
} else if (data.event === 'done') {
console.log('\n\nDone!');
}
}
}
}
}
// Usage
await streamRoute('Write a poem about AI');cURL
Non-streaming request
curl -X POST https://stepblend.com/api/route \
-H "Authorization: Bearer YOUR_JWT" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Explain how AI routing works",
"strategy": "balanced",
"max_cost": 0.01
}'Streaming request
curl -X POST https://stepblend.com/api/route/stream \
-H "Authorization: Bearer YOUR_JWT" \
-H "Content-Type: application/json" \
-d '{"prompt": "Write a haiku about code", "strategy": "lowest_cost"}' \
--no-bufferRecommendation only
curl -X POST https://stepblend.com/api/recommend \
-H "Authorization: Bearer YOUR_JWT" \
-H "Content-Type: application/json" \
-d '{"prompt": "Generate SQL query", "strategy": "max_reliability"}'Go
Non-streaming request
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
type RouteRequest struct {
Prompt string `json:"prompt"`
Strategy string `json:"strategy,omitempty"`
MaxCost float64 `json:"max_cost,omitempty"`
}
type RouteResponse struct {
Result string `json:"result"`
ModelUsed string `json:"model_used"`
Provider string `json:"provider"`
EstimatedCost float64 `json:"estimated_cost"`
TaskType string `json:"task_type"`
}
func routePrompt(prompt string) (*RouteResponse, error) {
jwt := os.Getenv("STEPBLEND_JWT")
url := "https://stepblend.com/api/route"
reqBody := RouteRequest{
Prompt: prompt,
Strategy: "balanced",
MaxCost: 0.01,
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+jwt)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result RouteResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return &result, nil
}
func main() {
result, err := routePrompt("Explain Go concurrency")
if err != nil {
panic(err)
}
fmt.Printf("Model: %s\n", result.ModelUsed)
fmt.Printf("Cost: $%.6f\n", result.EstimatedCost)
fmt.Printf("Response: %s\n", result.Result)
}Ruby
require 'net/http'
require 'json'
require 'uri'
def route_prompt(prompt, strategy = 'balanced')
uri = URI('https://stepblend.com/api/route')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
request = Net::HTTP::Post.new(uri)
request['Authorization'] = "Bearer #{ENV['STEPBLEND_JWT']}"
request['Content-Type'] = 'application/json'
request.body = {
prompt: prompt,
strategy: strategy,
max_cost: 0.01
}.to_json
response = http.request(request)
JSON.parse(response.body)
end
# Usage
result = route_prompt('Explain Ruby metaprogramming')
puts "Model: #{result['model_used']}"
puts "Cost: $#{result['estimated_cost']}"
puts "Response: #{result['result']}"PHP
<?php
function routePrompt($prompt, $strategy = 'balanced') {
$url = 'https://stepblend.com/api/route';
$jwt = getenv('STEPBLEND_JWT');
$data = [
'prompt' => $prompt,
'strategy' => $strategy,
'max_cost' => 0.01
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $jwt,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
throw new Exception("API error: " . $httpCode);
}
return json_decode($response, true);
}
// Usage
$result = routePrompt('Explain PHP arrays');
echo "Model: " . $result['model_used'] . "\n";
echo "Cost: $" . $result['estimated_cost'] . "\n";
echo "Response: " . $result['result'] . "\n";
?>Getting your JWT token
See the API Reference for instructions on how to get your JWT token for authentication.