Skip to main content

Prerequisites

You'll need an API key to use the Moderation API. To get one, you'll need to create a project.

1. Install the SDK

gem install moderation_api
Requires Ruby 3.2.0 or higher.

2. Submit content

Grab the API key from your project and begin submitting text, images, or other media to your project for moderation.
require "moderation_api"

# Initialize the client with your secret key
moderation_api = ModerationAPI::Client.new(
  secret_key: ENV["MODAPI_SECRET_KEY"] || "your-api-key"
)

# Submit text for moderation
response = moderation_api.content.submit(
  content: {
    type: "text",
    text: "Hello world!"
  },
  author_id: "123",
  conversation_id: "456",
  metadata: {
    custom_field: "value"
  }
)

# Check if content was flagged
if response.evaluation.flagged
  puts "Content was flagged"
end

# Act on the recommendation
case response.recommendation.action
when :allow
  puts "Text content is safe."
  # Save to database or proceed...
when :review
  puts "Text content needs review"
  # Send to review queue
when :reject
  puts "Text content rejected"
  # Block the content, show an error, etc...
end

3. Handle errors

The SDK throws specific error types for different scenarios:
require "moderation_api"

moderation_api = ModerationAPI::Client.new(
  secret_key: ENV["MODAPI_SECRET_KEY"] || "your-api-key"
)

begin
  response = moderation_api.content.submit(
    content: {
      type: "text",
      text: "Hello world!"
    }
  )

  puts "Recommendation: #{response.recommendation.action}"

rescue ModerationAPI::Errors::BadRequestError => e
  puts "Invalid request: #{e.message}"
rescue ModerationAPI::Errors::AuthenticationError => e
  puts "Authentication failed: #{e.message}"
rescue ModerationAPI::Errors::RateLimitError => e
  puts "Rate limit exceeded: #{e.message}"
rescue ModerationAPI::Errors::APIConnectionError => e
  puts "Connection error: #{e.message}"
rescue ModerationAPI::Errors::APITimeoutError => e
  puts "Request timed out: #{e.message}"
rescue ModerationAPI::Errors::InternalServerError => e
  puts "Server error: #{e.message}"
end
The SDK automatically retries failed requests up to 2 times by default. Configure retries globally with max_retries: 0 when initializing the client, or per-request using request_options: {max_retries: 5}. Timeouts default to 60 seconds and can be customized similarly.

Dry-run mode: If you want to analyze production data but don't want to block content, enable "dry-run" in your project settings.
With dry-run enabled, the API still analyzes content but it always returns flagged: false - yet content still shows in the review queue. This way you can implement your moderation workflows and start testing your project configuration without actually blocking content.

3. Review flagged content (optional)

If the AI flags the content, it will appear in the Review Queue. Head to the review queue to validate that the content is submitted correctly.
Review queue for reviewing and improving automated moderation

Review queue for reviewing and improving automated moderation

You can use review queues to implement moderation workflows or simply check how the AI is performing.

All Done!

Congratulations! You've run your first moderation checks. Here are a few next steps:
  • Continue tweaking your project settings and models to find the best moderation outcomes.
  • Create an AI agent and add your guidelines to it.
  • Explore advanced features like context-aware moderation.
  • If you have questions, reach out to our support team.
We love hearing from you—please share how you're using the service and let us know if you have suggestions or need help. Happy moderating!