Skip to content

Firewall API Overview

The Firewall API allows you to create content filtering rules that block, mask, or warn about sensitive content in requests and responses. Firewall rules help protect your applications by preventing sensitive data from being processed or returned.

Firewall rules let you define patterns that, when matched, trigger specific actions:

  • Block - Reject the entire request/response (returns 422 error)
  • Mask - Replace matched content with a replacement string
  • Warn - Allow the request but add a warning to the response

Rules are evaluated in priority order (highest first):

  1. Rules with higher priority values execute first
  2. Rules apply to either prompts (input) or responses (output)
  3. Only enabled rules are evaluated (is_enabled: true)
  4. Firewall rules are evaluated after routing rules in the pipeline
https://api.tokenrouter.io/v1/firewall-rules

All Firewall API endpoints require authentication using your TokenRouter API key:

Terminal window
Authorization: Bearer tr_your_api_key
OperationMethodEndpoint
List all rulesGET/v1/firewall-rules
Get single ruleGET/v1/firewall-rules/{id}
Create rulePOST/v1/firewall-rules
Update rulePATCH/v1/firewall-rules/{id}
Delete ruleDELETE/v1/firewall-rules/{id}
{
"name": "Block Credit Cards",
"priority": 100,
"is_enabled": true,
"scope": "prompt",
"type": "regex",
"pattern": "\\d{4}[\\s-]?\\d{4}[\\s-]?\\d{4}[\\s-]?\\d{4}",
"action": "block",
"replacement": null
}
  • prompt - Apply rule to user input before sending to LLM
  • response - Apply rule to LLM output before returning to user
  • substring - Case-insensitive substring matching
  • regex - Regular expression pattern matching (PCRE-compatible)
  • block - Reject request/response with 422 error
  • mask - Replace matched content with replacement string (default: [redacted])
  • warn - Add warning to response metadata but allow request
import Tokenrouter from 'tokenrouter';
const client = new Tokenrouter({
apiKey: process.env.TOKENROUTER_API_KEY
});
const rule = await client.firewallRules.create({
name: 'Block Sensitive Keywords',
priority: 100,
is_enabled: true,
scope: 'prompt',
type: 'regex',
pattern: '(password|ssn|credit.card)',
action: 'block'
});
const rule = await client.firewallRules.create({
name: 'Mask Email Addresses',
priority: 50,
is_enabled: true,
scope: 'response',
type: 'regex',
pattern: '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}',
action: 'mask',
replacement: '[EMAIL_REDACTED]'
});
const rule = await client.firewallRules.create({
name: 'Warn on Profanity',
priority: 30,
is_enabled: true,
scope: 'response',
type: 'substring',
pattern: 'badword',
action: 'warn'
});

When a firewall rule matches:

{
"error": {
"message": "Request blocked by firewall rule \"Block Sensitive Keywords\"",
"type": "routing_error",
"http_status": 422,
"meta": {
"rule_id": 123
}
}
}

Input: "My email is john@example.com" Output: "My email is [EMAIL_REDACTED]"

{
"metadata": {
"warnings": ["Profanity detected in response"],
"applied_rules": ["Warn on Profanity"]
}
}

When firewall rules are applied, the response includes metadata:

{
"metadata": {
"applied_rules": ["Mask Email Addresses"],
"warnings": []
}
}
Use CaseScopeTypeAction
Block credit card numberspromptregexblock
Mask SSNs in responsesresponseregexmask
Block API keyspromptregexblock
Redact namesresponsesubstringmask
Warn about profanityresponsesubstringwarn
Block SQL injectionpromptregexblock