Get Routing Rule
Get Routing Rule
Section titled “Get Routing Rule”Returns a single routing rule by ID for the authenticated user.
Endpoint
Section titled “Endpoint”GET /v1/routing-rules/{id}Path Parameters
Section titled “Path Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
id | integer | Yes | The routing rule ID |
Examples
Section titled “Examples”import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({ apiKey: process.env.TOKENROUTER_API_KEY});
const rule = await client.routingRules.retrieve(123);
console.log(`Rule: ${rule.data.name}`);console.log(`Priority: ${rule.data.priority}`);console.log(`Enabled: ${rule.data.is_enabled}`);console.log(`Match Conditions:`, rule.data.match_json);console.log(`Actions:`, rule.data.action_json);from tokenrouter import Tokenrouter
client = Tokenrouter( api_key=os.getenv("TOKENROUTER_API_KEY"))
rule = client.routing_rules.retrieve(123)
print(f"Rule: {rule.data.name}")print(f"Priority: {rule.data.priority}")print(f"Enabled: {rule.data.is_enabled}")print(f"Match Conditions: {rule.data.match_json}")print(f"Actions: {rule.data.action_json}")curl https://api.tokenrouter.io/v1/routing-rules/123 \ -H "Authorization: Bearer tr_..."Response
Section titled “Response”{ "data": { "id": 123, "user_id": 456, "name": "Code Review → Anthropic", "is_enabled": true, "priority": 100, "match_json": { "task": "code_review" }, "action_json": { "set_provider": "anthropic", "set_model": "claude-3-5-sonnet-20241022" }, "created_at": "2025-11-10T12:00:00Z", "updated_at": "2025-11-10T12:00:00Z" }}Response Fields
Section titled “Response Fields”| Field | Type | Description |
|---|---|---|
id | integer | Unique rule identifier |
user_id | integer | Owner user ID |
name | string | Rule name |
is_enabled | boolean | Whether rule is active |
priority | integer | Rule priority (-1000 to 1000) |
match_json | object | Match conditions |
action_json | object | Actions to apply |
created_at | string | ISO timestamp of creation |
updated_at | string | ISO timestamp of last update |
Error Responses
Section titled “Error Responses”404 Not Found
Section titled “404 Not Found”Returned when the rule doesn’t exist or doesn’t belong to the authenticated user:
{ "error": { "message": "Routing rule not found", "type": "not_found_error", "http_status": 404 }}Practical Examples
Section titled “Practical Examples”Check if Rule is Active
Section titled “Check if Rule is Active”const rule = await client.routingRules.retrieve(123);
if (rule.data.is_enabled) { console.log('Rule is active and will be evaluated');} else { console.log('Rule is disabled');}rule = client.routing_rules.retrieve(123)
if rule.data.is_enabled: print("Rule is active and will be evaluated")else: print("Rule is disabled")Inspect Rule Actions
Section titled “Inspect Rule Actions”const rule = await client.routingRules.retrieve(123);
const actions = rule.data.action_json;
if (actions.set_provider) { console.log(`Forces provider: ${actions.set_provider}`);}
if (actions.set_model) { console.log(`Forces model: ${actions.set_model}`);}
if (actions.set_mode) { console.log(`Overrides mode: ${actions.set_mode}`);}rule = client.routing_rules.retrieve(123)
actions = rule.data.action_json
if 'set_provider' in actions: print(f"Forces provider: {actions['set_provider']}")
if 'set_model' in actions: print(f"Forces model: {actions['set_model']}")
if 'set_mode' in actions: print(f"Overrides mode: {actions['set_mode']}")Clone Rule for Editing
Section titled “Clone Rule for Editing”// Get existing ruleconst original = await client.routingRules.retrieve(123);
// Create a similar rule with different priorityconst cloned = await client.routingRules.create({ name: `${original.data.name} (Copy)`, priority: original.data.priority - 10, is_enabled: false, // Start disabled match_json: original.data.match_json, action_json: original.data.action_json});
console.log('Created cloned rule:', cloned.data.id);# Get existing ruleoriginal = client.routing_rules.retrieve(123)
# Create a similar rule with different prioritycloned = client.routing_rules.create( name=f"{original.data.name} (Copy)", priority=original.data.priority - 10, is_enabled=False, # Start disabled match_json=original.data.match_json, action_json=original.data.action_json)
print(f"Created cloned rule: {cloned.data.id}")Use Cases
Section titled “Use Cases”Audit Rule Configuration
Section titled “Audit Rule Configuration”Use this endpoint to verify rule settings before enabling:
const rule = await client.routingRules.retrieve(ruleId);
// Verify conditionsassert(rule.data.match_json.task === 'coding');
// Verify actionsassert(rule.data.action_json.set_provider === 'openai');
// Enable if verifiedif (!rule.data.is_enabled) { await client.routingRules.update(ruleId, { is_enabled: true });}Monitor Rule Changes
Section titled “Monitor Rule Changes”Track when rules were last modified:
const rule = await client.routingRules.retrieve(ruleId);
const lastModified = new Date(rule.data.updated_at);const hoursSinceUpdate = (Date.now() - lastModified.getTime()) / (1000 * 60 * 60);
if (hoursSinceUpdate > 24) { console.log('Rule hasn\'t been updated in over 24 hours');}