Skip to content

Get Routing Rule

Returns a single routing rule by ID for the authenticated user.

GET /v1/routing-rules/{id}
ParameterTypeRequiredDescription
idintegerYesThe routing rule ID
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);
{
"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"
}
}
FieldTypeDescription
idintegerUnique rule identifier
user_idintegerOwner user ID
namestringRule name
is_enabledbooleanWhether rule is active
priorityintegerRule priority (-1000 to 1000)
match_jsonobjectMatch conditions
action_jsonobjectActions to apply
created_atstringISO timestamp of creation
updated_atstringISO timestamp of last update

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
}
}
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');
}
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}`);
}
// Get existing rule
const original = await client.routingRules.retrieve(123);
// Create a similar rule with different priority
const 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);

Use this endpoint to verify rule settings before enabling:

const rule = await client.routingRules.retrieve(ruleId);
// Verify conditions
assert(rule.data.match_json.task === 'coding');
// Verify actions
assert(rule.data.action_json.set_provider === 'openai');
// Enable if verified
if (!rule.data.is_enabled) {
await client.routingRules.update(ruleId, { is_enabled: true });
}

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');
}