Custom Rules API
Create custom moderation rules tailored to your platform's needs. Rules can use keywords, regex patterns, or semantic matching.
Endpoints
/api/v1/moderation/rules/api/v1/moderation/rules/api/v1/moderation/rules/:ruleId/api/v1/moderation/rules/:ruleId/api/v1/moderation/rules/:ruleId/api/v1/moderation/rules/:ruleId/testRule Types
keyword
Match specific words or phrases
{
"ruleType": "keyword",
"ruleDefinition": {
"keywords": ["spam", "scam", "click here"],
"keywordMatchMode": "any",
"caseSensitive": false
}
}regex
Match patterns using regular expressions
{
"ruleType": "regex",
"ruleDefinition": {
"patterns": [
"PRIVATE-[0-9]{4}"
]
}
}semantic
Classify content against your categories using AI
{
"ruleType": "semantic",
"ruleDefinition": {
"semanticCategories": [
"financial_advice",
"medical_claims",
"conspiracy_theories"
],
"semanticThreshold": 0.75
}
}combined
Combine multiple matching strategies
{
"ruleType": "combined",
"ruleDefinition": {
"keywords": ["investment", "guaranteed returns"],
"semanticCategories": ["financial_advice"],
"semanticThreshold": 0.6,
"contextExclusions": ["educational", "news"]
}
}Create Rule
/api/v1/moderation/rulesRequest Body
| Parameter | Type |
|---|---|
namerequiredRule name (1-100 characters). | string |
descriptionOptional description (max 500 characters). | string |
ruleTyperequiredOne of: "keyword", "regex", "semantic", "combined". | string |
ruleDefinitionrequiredRule matching configuration (varies by type). | object |
actionrequiredAction when triggered. One of: "flag", "reject", "escalate". | string |
severityOne of: "minor", "moderate", "severe". | stringDefault: "moderate" |
priorityRule priority (1-1000). Lower numbers run first. | numberDefault: 100 |
Rule Definition Fields
| Parameter | Type |
|---|---|
keywordsKeywords to match (for keyword/combined types). | string[] |
keywordMatchMode"any" = match any keyword, "all" = match all, "phrase" = exact phrase. | stringDefault: "any" |
caseSensitiveWhether keyword matching is case-sensitive. | booleanDefault: false |
patternsRegex patterns (for regex/combined types). | string[] |
semanticCategoriesAI categories to detect (for semantic/combined). | string[] |
semanticThresholdMinimum confidence for semantic match (0-1). | numberDefault: 0.7 |
contextExclusionsCase-insensitive substrings of content that bypass this rule. Use narrowly; these are not inferred context labels. | string[] |
Example Request
{
"name": "No Cryptocurrency Promotion",
"description": "Block promotional content about cryptocurrency investments",
"ruleType": "combined",
"ruleDefinition": {
"keywords": ["buy crypto", "moon", "HODL", "guaranteed returns"],
"keywordMatchMode": "any",
"semanticCategories": ["financial_promotion", "cryptocurrency"],
"semanticThreshold": 0.65,
"contextExclusions": ["news", "educational"]
},
"action": "reject",
"severity": "moderate",
"priority": 700
}Response
HTTP 201 returns an object containing rule. Its ID is a UUID. The SDK unwraps that object and returns the created rule directly.
console.log(rule.id, rule.name, rule.isActive);
console.log(rule.createdAt, rule.updatedAt);Test Rule
/api/v1/moderation/rules/:ruleId/testTest one rule against 1–10,000 characters of content. This evaluates the rule directly without creating a completed moderation request. The HTTP response contains rule, test, and result; result has triggered, explanation, and matches. The SDK returns result directly. Semantic evaluation still requires an available provider.
{
"content": "Buy DOGE now! 100x guaranteed returns! This is your chance to moon!"
}Code Examples
import { CommunitAIModerator } from '@communitai/maas';
import { env } from '@/env';
const client = new CommunitAIModerator({
apiKey: env.COMMUNITAI_API_KEY, // Import your application's validated server configuration.
});
// Create a rule
const rule = await client.createRule({
name: "No Personal Attacks",
ruleType: "semantic",
ruleDefinition: {
semanticCategories: ["personal_attack", "harassment"],
semanticThreshold: 0.7,
},
action: "reject",
severity: "severe",
});
console.log(`Created rule: ${rule.id}`);
// Test the rule
const test = await client.testRule(rule.id, "You're an idiot and should be banned");
console.log(test.triggered, test.explanation, test.matches);
// List all rules
const rules = await client.listRules({ activeOnly: true });
console.log(`Active rules: ${rules.length}`);
// Disable a rule
await client.updateRule(rule.id, { isActive: false });Example Semantic Categories
Categories are your text descriptions sent to the classifier, not a fixed enum. Choose precise descriptions for your policy. These are examples:
Safety
hate_speechharassmentviolenceself_harmsexual_contentdangerous_activity
Spam & Fraud
spamscamphishingfake_newsmisinformationclickbait
Financial
financial_advicefinancial_promotioncryptocurrencyinvestment_schemegambling
Content
personal_attackprofanityadult_contentpolitical_contentreligious_content