Text Moderation Examples

Examples of how to moderate text content using the Omni API

Basic Text Moderation

This example shows how to moderate a single text item:

Request

curl -X POST https://api.omnimoderate.com/v1/moderate \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your_api_key_here" \
  -d '{
    "input": [
      { "type": "text", "text": "This restaurant has amazing food!" }
    ]
  }'

Response

{
  "id": "modr-123456",
  "model": "text-moderation-latest",
  "results": [
    {
      "flagged": false,
      "categories": {
        "hate": false,
        "harassment": false,
        "self-harm": false,
        "sexual": false,
        "violence": false
      },
      "category_scores": {
        "hate": 0.0,
        "harassment": 0.0,
        "self-harm": 0.0,
        "sexual": 0.0,
        "violence": 0.0
      }
    }
  ],
  "combined_text_moderation": {
    "flagged": false,
    "categories": {
      "hate": false,
      "harassment": false,
      "self-harm": false,
      "sexual": false,
      "violence": false
    },
    "category_scores": {
      "hate": 0.0,
      "harassment": 0.0,
      "self-harm": 0.0,
      "sexual": 0.0,
      "violence": 0.0
    }
  },
  "flagged": false,
  "signature": "abcdef1234567890"
}

Multiple Text Items

This example shows how to moderate multiple text items in a single request. The API will evaluate each item individually and also evaluate all items together to catch issues that might only be apparent when text fragments are considered as a whole.

Request

curl -X POST https://api.omnimoderate.com/v1/moderate \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your_api_key_here" \
  -d '{
    "input": [
      { "type": "text", "text": "This restaurant has amazing food!" },
      { "type": "text", "text": "The service was excellent and I highly recommend the steak." },
      { "type": "text", "text": "The atmosphere was cozy and welcoming." }
    ]
  }'

Response

{
  "id": "modr-123457",
  "model": "text-moderation-latest",
  "results": [
    {
      "flagged": false,
      "categories": {
        "hate": false,
        "harassment": false,
        "self-harm": false,
        "sexual": false,
        "violence": false
      },
      "category_scores": {
        "hate": 0.0,
        "harassment": 0.0,
        "self-harm": 0.0,
        "sexual": 0.0,
        "violence": 0.0
      }
    },
    {
      "flagged": false,
      "categories": {
        "hate": false,
        "harassment": false,
        "self-harm": false,
        "sexual": false,
        "violence": false
      },
      "category_scores": {
        "hate": 0.0,
        "harassment": 0.0,
        "self-harm": 0.0,
        "sexual": 0.0,
        "violence": 0.0
      }
    },
    {
      "flagged": false,
      "categories": {
        "hate": false,
        "harassment": false,
        "self-harm": false,
        "sexual": false,
        "violence": false
      },
      "category_scores": {
        "hate": 0.0,
        "harassment": 0.0,
        "self-harm": 0.0,
        "sexual": 0.0,
        "violence": 0.0
      }
    }
  ],
  "combined_text_moderation": {
    "flagged": false,
    "categories": {
      "hate": false,
      "harassment": false,
      "self-harm": false,
      "sexual": false,
      "violence": false
    },
    "category_scores": {
      "hate": 0.0,
      "harassment": 0.0,
      "self-harm": 0.0,
      "sexual": 0.0,
      "violence": 0.0
    }
  },
  "flagged": false,
  "signature": "abcdef1234567891"
}

Handling Flagged Content

This example shows how to handle a response where content has been flagged as inappropriate:

Request

curl -X POST https://api.omnimoderate.com/v1/moderate \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your_api_key_here" \
  -d '{
    "input": [
      { "type": "text", "text": "I hate this place and everyone who works there!" }
    ]
  }'

Response

{
  "id": "modr-123458",
  "model": "text-moderation-latest",
  "results": [
    {
      "flagged": true,
      "categories": {
        "hate": true,
        "harassment": true,
        "self-harm": false,
        "sexual": false,
        "violence": false
      },
      "category_scores": {
        "hate": 0.82,
        "harassment": 0.91,
        "self-harm": 0.01,
        "sexual": 0.0,
        "violence": 0.05
      }
    }
  ],
  "combined_text_moderation": {
    "flagged": true,
    "categories": {
      "hate": true,
      "harassment": true,
      "self-harm": false,
      "sexual": false,
      "violence": false
    },
    "category_scores": {
      "hate": 0.82,
      "harassment": 0.91,
      "self-harm": 0.01,
      "sexual": 0.0,
      "violence": 0.05
    }
  },
  "flagged": true,
  "signature": "abcdef1234567892"
}

Code Examples

JavaScript

moderate-text.js
async function moderateText(apiKey, textContent) {
  const response = await fetch('https://api.omnimoderate.com/v1/moderate', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-API-Key': apiKey
    },
    body: JSON.stringify({
      input: Array.isArray(textContent) 
        ? textContent.map(text => ({ type: 'text', text }))
        : [{ type: 'text', text: textContent }]
    })
  });

  if (!response.ok) {
    const errorData = await response.json();
    throw new Error(errorData.error?.message || 'Moderation request failed');
  }

  return await response.json();
}

// Example usage
async function example() {
  const apiKey = 'your_api_key_here';
  
  try {
    // Single text item
    const result1 = await moderateText(apiKey, 'This is a sample text to moderate.');
    console.log('Single text result:', result1.flagged);
    
    // Multiple text items
    const result2 = await moderateText(apiKey, [
      'This restaurant has amazing food!',
      'The service was excellent and I highly recommend the steak.'
    ]);
    console.log('Multiple text result:', result2.flagged);
    
    // Check individual results
    result2.results.forEach((result, index) => {
      console.log(`Text item ${index + 1} flagged: ${result.flagged}`);
    });
    
    // Check combined result
    console.log('Combined text flagged:', result2.combined_text_moderation.flagged);
  } catch (error) {
    console.error('Error:', error.message);
  }
}

Python

moderate_text.py
import requests

def moderate_text(api_key, text_content):
    url = "https://api.omnimoderate.com/v1/moderate"
    
    headers = {
        "Content-Type": "application/json",
        "X-API-Key": api_key
    }
    
    # Handle both single text and list of texts
    if isinstance(text_content, list):
        input_data = [{"type": "text", "text": text} for text in text_content]
    else:
        input_data = [{"type": "text", "text": text_content}]
    
    payload = {
        "input": input_data
    }
    
    response = requests.post(url, headers=headers, json=payload)
    
    if response.status_code != 200:
        error_data = response.json()
        raise Exception(error_data.get("error", {}).get("message", "Moderation request failed"))
    
    return response.json()

# Example usage
def example():
    api_key = "your_api_key_here"
    
    try:
        # Single text item
        result1 = moderate_text(api_key, "This is a sample text to moderate.")
        print(f"Single text result: {result1['flagged']}")
        
        # Multiple text items
        result2 = moderate_text(api_key, [
            "This restaurant has amazing food!",
            "The service was excellent and I highly recommend the steak."
        ])
        print(f"Multiple text result: {result2['flagged']}")
        
        # Check individual results
        for i, result in enumerate(result2['results']):
            print(f"Text item {i + 1} flagged: {result['flagged']}")
        
        # Check combined result
        print(f"Combined text flagged: {result2['combined_text_moderation']['flagged']}")
    except Exception as e:
        print(f"Error: {str(e)}")

if __name__ == "__main__":
    example()

Next Steps