Create Routing Rule
Create Routing Rule
Section titled “Create Routing Rule”Create a new routing rule for the authenticated user.
Endpoint
Section titled “Endpoint”POST /v1/routing-rulesRequest Body
Section titled “Request Body”| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Human-readable name for the rule (max 128 characters) |
is_enabled | boolean | Yes | Whether the rule is active |
priority | integer | Yes | Priority for rule evaluation (-1000 to 1000, higher = first) |
match_json | object|string | Yes | Conditions that trigger the rule |
action_json | object|string | Yes | Actions to apply when rule matches |
Match Conditions (match_json)
Section titled “Match Conditions (match_json)”| Condition | Type | Description | Example |
|---|---|---|---|
contains | string or array | Text in input (case-insensitive) | "code" or ["code", "bug"] |
metadata_equals | object | Metadata key-value pairs | {"task": "analysis"} |
mode | string | Routing mode equals | "balanced", "cost", "quality", "latency" |
task | string | Shorthand for metadata_equals.task | "coding" |
Empty conditions ({}) match all requests.
Actions (action_json)
Section titled “Actions (action_json)”| Action | Type | Description | Valid Values |
|---|---|---|---|
set_provider | string | Force specific provider | openai, anthropic, gemini, mistral, deepseek |
set_model | string | Force specific model | Any valid model name |
set_mode | string | Override routing mode | balance, cost, quality, latency |
add_warning | object | Add warning message | {"message": "text"} |
Examples
Section titled “Examples”Example 1: Force OpenAI for Coding Tasks
Section titled “Example 1: Force OpenAI for Coding Tasks”const rule = await client.routingRules.create({ name: 'Coding Tasks → OpenAI', priority: 100, is_enabled: true, match_json: { task: 'coding' }, action_json: { set_provider: 'openai', set_model: 'gpt-4o' }});
console.log(rule.data.id); // Created rule IDrule = client.routing_rules.create( name="Coding Tasks → OpenAI", priority=100, is_enabled=True, match_json={"task": "coding"}, action_json={ "set_provider": "openai", "set_model": "gpt-4o" })
print(rule.data.id) # Created rule IDcurl -X POST https://api.tokenrouter.io/v1/routing-rules \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_..." \ -d '{ "name": "Coding Tasks → OpenAI", "priority": 100, "is_enabled": true, "match_json": {"task": "coding"}, "action_json": { "set_provider": "openai", "set_model": "gpt-4o" } }'Example 2: Cost Mode for Documentation
Section titled “Example 2: Cost Mode for Documentation”const rule = await client.routingRules.create({ name: 'Documentation → Cost Mode', priority: 50, is_enabled: true, match_json: { contains: ['documentation', 'docs', 'readme'] }, action_json: { set_mode: 'cost' }});rule = client.routing_rules.create( name="Documentation → Cost Mode", priority=50, is_enabled=True, match_json={"contains": ["documentation", "docs", "readme"]}, action_json={"set_mode": "cost"})curl -X POST https://api.tokenrouter.io/v1/routing-rules \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_..." \ -d '{ "name": "Documentation → Cost Mode", "priority": 50, "is_enabled": true, "match_json": {"contains": ["documentation", "docs", "readme"]}, "action_json": {"set_mode": "cost"} }'Example 3: Default Rule with Warning
Section titled “Example 3: Default Rule with Warning”const rule = await client.routingRules.create({ name: 'Always Warn About Beta', priority: -100, is_enabled: true, match_json: {}, // Match all requests action_json: { add_warning: { message: 'Beta routing engine in use' } }});rule = client.routing_rules.create( name="Always Warn About Beta", priority=-100, is_enabled=True, match_json={}, # Match all requests action_json={ "add_warning": {"message": "Beta routing engine in use"} })curl -X POST https://api.tokenrouter.io/v1/routing-rules \ -H "Content-Type: application/json" \ -H "Authorization: Bearer tr_..." \ -d '{ "name": "Always Warn About Beta", "priority": -100, "is_enabled": true, "match_json": {}, "action_json": { "add_warning": {"message": "Beta routing engine in use"} } }'Response
Section titled “Response”{ "data": { "id": 123, "user_id": 456, "name": "Coding Tasks → OpenAI", "is_enabled": true, "priority": 100, "match_json": { "task": "coding" }, "action_json": { "set_provider": "openai", "set_model": "gpt-4o" }, "created_at": "2025-11-10T12:00:00Z", "updated_at": "2025-11-10T12:00: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 }}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"] } }}Best Practices
Section titled “Best Practices”- Use descriptive names - Make rule purposes clear
- Set appropriate priorities - Higher values for more specific rules
- Test with
is_enabled: false- Test rules before activating - Use specific conditions - Avoid overly broad match conditions
- Combine actions carefully - Only first provider/model/mode applies