Update Routing Rule
Update Routing Rule
Section titled “Update Routing Rule”Updates an existing routing rule. All fields are optional - only provide the fields you want to change.
Endpoint
Section titled “Endpoint”PATCH /v1/routing-rules/{id}Path Parameters
Section titled “Path Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
id | integer | Yes | The routing rule ID |
Request Body
Section titled “Request Body”All fields are optional. Only include fields you want to update:
| Parameter | Type | Description |
|---|---|---|
name | string | Human-readable name (max 128 characters) |
is_enabled | boolean | Whether the rule is active |
priority | integer | Rule priority (-1000 to 1000) |
match_json | object|string | Match conditions |
action_json | object|string | Actions to apply |
Examples
Section titled “Examples”Example 1: Enable/Disable Rule
Section titled “Example 1: Enable/Disable Rule”import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({ apiKey: process.env.TOKENROUTER_API_KEY});
// Disable a ruleawait client.routingRules.update(123, { is_enabled: false});
// Enable a ruleawait client.routingRules.update(123, { is_enabled: true});from tokenrouter import Tokenrouter
client = Tokenrouter( api_key=os.getenv("TOKENROUTER_API_KEY"))
# Disable a ruleclient.routing_rules.update(123, is_enabled=False)
# Enable a ruleclient.routing_rules.update(123, is_enabled=True)# Disable a rulecurl -X PATCH https://api.tokenrouter.io/v1/routing-rules/123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_..." \ -d '{"is_enabled": false}'Example 2: Change Priority
Section titled “Example 2: Change Priority”// Increase rule priority to execute it earlierawait client.routingRules.update(123, { priority: 200});# Increase rule priority to execute it earlierclient.routing_rules.update(123, priority=200)curl -X PATCH https://api.tokenrouter.io/v1/routing-rules/123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_..." \ -d '{"priority": 200}'Example 3: Update Conditions
Section titled “Example 3: Update Conditions”// Change match conditionsawait client.routingRules.update(123, { match_json: { task: 'code_review', mode: 'quality' // Add additional condition }});# Change match conditionsclient.routing_rules.update( 123, match_json={ "task": "code_review", "mode": "quality" # Add additional condition })curl -X PATCH https://api.tokenrouter.io/v1/routing-rules/123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_..." \ -d '{ "match_json": { "task": "code_review", "mode": "quality" } }'Example 4: Update Actions
Section titled “Example 4: Update Actions”// Change from forcing provider to just overriding modeawait client.routingRules.update(123, { action_json: { set_mode: 'cost' }});# Change from forcing provider to just overriding modeclient.routing_rules.update( 123, action_json={"set_mode": "cost"})curl -X PATCH https://api.tokenrouter.io/v1/routing-rules/123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_..." \ -d '{"action_json": {"set_mode": "cost"}}'Example 5: Update Multiple Fields
Section titled “Example 5: Update Multiple Fields”await client.routingRules.update(123, { name: 'Updated Rule Name', priority: 150, is_enabled: true, match_json: { contains: 'analyze' }, action_json: { set_provider: 'anthropic', set_mode: 'quality' }});client.routing_rules.update( 123, name="Updated Rule Name", priority=150, is_enabled=True, match_json={"contains": "analyze"}, action_json={ "set_provider": "anthropic", "set_mode": "quality" })curl -X PATCH https://api.tokenrouter.io/v1/routing-rules/123 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_..." \ -d '{ "name": "Updated Rule Name", "priority": 150, "is_enabled": true, "match_json": {"contains": "analyze"}, "action_json": { "set_provider": "anthropic", "set_mode": "quality" } }'Response
Section titled “Response”{ "data": { "id": 123, "user_id": 456, "name": "Updated Rule Name", "is_enabled": true, "priority": 150, "match_json": { "contains": "analyze" }, "action_json": { "set_provider": "anthropic", "set_mode": "quality" }, "created_at": "2025-11-10T12:00:00Z", "updated_at": "2025-11-10T15:30:00Z" }}Error Responses
Section titled “Error Responses”403 Forbidden - Free Plan
Section titled “403 Forbidden - Free Plan”{ "error": { "message": "Routing rules are not available on the free plan", "type": "authorization_error", "http_status": 403 }}404 Not Found
Section titled “404 Not Found”{ "error": { "message": "Routing rule not found", "type": "not_found_error", "http_status": 404 }}422 Validation Error
Section titled “422 Validation Error”{ "error": { "message": "Validation failed", "type": "validation_error", "http_status": 422, "errors": { "priority": ["The priority must be between -1000 and 1000"] } }}Practical Patterns
Section titled “Practical Patterns”Temporarily Disable Rule
Section titled “Temporarily Disable Rule”// Disable temporarily for testingconst original = await client.routingRules.retrieve(ruleId);
await client.routingRules.update(ruleId, { is_enabled: false});
// Test your requests...
// Re-enableawait client.routingRules.update(ruleId, { is_enabled: true});# Disable temporarily for testingoriginal = client.routing_rules.retrieve(rule_id)
client.routing_rules.update(rule_id, is_enabled=False)
# Test your requests...
# Re-enableclient.routing_rules.update(rule_id, is_enabled=True)Adjust Priority Relative to Others
Section titled “Adjust Priority Relative to Others”// Get all rulesconst rules = await client.routingRules.list();
// Find highest priorityconst highestPriority = Math.max(...rules.data.map(r => r.priority));
// Make this rule highest priorityawait client.routingRules.update(ruleId, { priority: highestPriority + 10});# Get all rulesrules = client.routing_rules.list()
# Find highest priorityhighest_priority = max(rule.priority for rule in rules.data)
# Make this rule highest priorityclient.routing_rules.update(rule_id, priority=highest_priority + 10)Swap Rule Priorities
Section titled “Swap Rule Priorities”// Swap priorities between two rulesconst rule1 = await client.routingRules.retrieve(id1);const rule2 = await client.routingRules.retrieve(id2);
await client.routingRules.update(id1, { priority: rule2.data.priority});
await client.routingRules.update(id2, { priority: rule1.data.priority});# Swap priorities between two rulesrule1 = client.routing_rules.retrieve(id1)rule2 = client.routing_rules.retrieve(id2)
client.routing_rules.update(id1, priority=rule2.data.priority)client.routing_rules.update(id2, priority=rule1.data.priority)Best Practices
Section titled “Best Practices”- Update one field at a time when testing changes
- Disable before major changes to prevent unexpected routing
- Use descriptive names when renaming rules
- Test priority changes by listing rules after update
- Keep match conditions simple - complex logic can be split into multiple rules