Skip to content

List Routing Rules

Returns all routing rules for the authenticated user, ordered by priority (highest first).

GET /v1/routing-rules

None. This endpoint returns all rules for the authenticated user.

import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({
apiKey: process.env.TOKENROUTER_API_KEY
});
const response = await client.routingRules.list();
// Rules are ordered by priority (highest first)
response.data.forEach(rule => {
console.log(`${rule.name} (Priority: ${rule.priority})`);
console.log(`Enabled: ${rule.is_enabled}`);
console.log(`Conditions:`, rule.match_json);
console.log(`Actions:`, rule.action_json);
console.log('---');
});
{
"data": [
{
"id": 123,
"user_id": 456,
"name": "High Priority Code Review",
"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"
},
{
"id": 124,
"user_id": 456,
"name": "Cost Mode for Documentation",
"is_enabled": true,
"priority": 50,
"match_json": {
"contains": ["documentation", "docs"]
},
"action_json": {
"set_mode": "cost"
},
"created_at": "2025-11-10T13:00:00Z",
"updated_at": "2025-11-10T13:00:00Z"
},
{
"id": 125,
"user_id": 456,
"name": "Disabled Test Rule",
"is_enabled": false,
"priority": 10,
"match_json": {
"mode": "quality"
},
"action_json": {
"set_provider": "openai"
},
"created_at": "2025-11-10T14:00:00Z",
"updated_at": "2025-11-10T14: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

Rules are returned in the order they will be evaluated during routing:

  1. By Priority (DESC) - Highest priority first
  2. By ID (ASC) - Older rules first when priorities are equal

Example order:

Priority 100 → Evaluated first
Priority 50 → Evaluated second
Priority 10 → Evaluated third
Priority -100 → Evaluated last
const response = await client.routingRules.list();
// Get only enabled rules
const activeRules = response.data.filter(rule => rule.is_enabled);
console.log(`${activeRules.length} active rules`);

If no rules exist, the response is an empty array:

{
"data": []
}
const response = await client.routingRules.list();
const codingRule = response.data.find(
rule => rule.match_json.task === 'coding' && rule.is_enabled
);
if (codingRule) {
console.log('Found coding rule:', codingRule.name);
} else {
console.log('No active coding rule found');
}
const response = await client.routingRules.list();
const highPriority = response.data.filter(r => r.priority >= 50).length;
const lowPriority = response.data.filter(r => r.priority < 0).length;
console.log(`High priority rules: ${highPriority}`);
console.log(`Low priority rules: ${lowPriority}`);