> ## Documentation Index
> Fetch the complete documentation index at: https://docs.moderationapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Content moderation with Python

> Learn how to moderate content using Moderation API's Python SDK

***

<Snippet file="prerequisites.mdx" />

***

## 1. Install the SDK

```bash theme={"theme":"nord"}
pip install moderation_api
```

<Note>
  Requires Python 3.9 or higher. For improved async performance, install with `pip install moderation_api[aiohttp]`.
</Note>

## 2. Submit content

Grab the API key from your [project](https://dash.moderationapi.com) and begin submitting text, images, or other media to your project for moderation.

<CodeGroup>
  ```python Text moderation theme={"theme":"nord"}
  import os
  from moderation_api import ModerationAPI

  # Initialize the client with your secret key
  client = ModerationAPI(
      secret_key=os.environ.get("MODAPI_SECRET_KEY")
  )

  # Submit text for moderation
  response = client.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:
      print("Content was flagged")

  # Act on the recommendation
  if response.recommendation.action == "allow":
      print("Text content is safe.")
      # Save to database or proceed...
  elif response.recommendation.action == "review":
      print("Text content needs review")
      # Send to review queue
  elif response.recommendation.action == "reject":
      print("Text content rejected")
      # Block the content, show an error, etc...
  ```

  ```python Image moderation theme={"theme":"nord"}
  import os
  from moderation_api import ModerationAPI

  # Initialize the client with your secret key
  client = ModerationAPI(
      secret_key=os.environ.get("MODAPI_SECRET_KEY")
  )

  # Submit image for moderation
  response = client.content.submit(
      content={
          "type": "image",
          "url": "https://example.com/image.jpg"
      },
      author_id="123",
      conversation_id="456",
      metadata={
          "custom_field": "value"
      }
  )

  # Check if content was flagged
  if response.evaluation.flagged:
      print("Image was flagged")

  # Act on the recommendation
  if response.recommendation.action == "allow":
      print("Image is safe.")
      # Save to database or proceed...
  elif response.recommendation.action == "review":
      print("Image needs review")
      # Send to review queue
  elif response.recommendation.action == "reject":
      print("Image rejected")
      # Block or require review
  ```

  ```python Object moderation theme={"theme":"nord"}
  import os
  from moderation_api import ModerationAPI

  # Initialize the client with your secret key
  client = ModerationAPI(
      secret_key=os.environ.get("MODAPI_SECRET_KEY")
  )

  # Submit object for moderation
  response = client.content.submit(
      content={
          "type": "object",
          "data": {
              "name": {
                  "type": "text",
                  "text": "John Doe"
              },
              "email": {
                  "type": "text",
                  "text": "john.doe@example.com"
              },
              "picture": {
                  "type": "image",
                  "url": "https://example.com/image.jpg"
              }
          }
      },
      author_id="123",
      conversation_id="456",
      metadata={
          "custom_field": "value"
      }
  )

  # Check if content was flagged
  if response.evaluation.flagged:
      print("Object was flagged")

  # Act on the recommendation
  if response.recommendation.action == "allow":
      print("Object is safe.")
      # Save to database or proceed...
  elif response.recommendation.action == "review":
      print("Object needs review")
      # Send to review queue
  elif response.recommendation.action == "reject":
      print("Object rejected")
      # Block or require review
  ```
</CodeGroup>

## 3. Handle errors

The SDK raises specific exception types for different error scenarios:

```python theme={"theme":"nord"}
import os
from moderation_api import ModerationAPI
from moderation_api import APIConnectionError, APIStatusError

client = ModerationAPI(
    secret_key=os.environ.get("MODAPI_SECRET_KEY")
)

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

    print(f"Recommendation: {response.recommendation.action}")

except APIStatusError as e:
    print(f"API error {e.status_code}: {e.message}")
except APIConnectionError as e:
    print(f"Connection error: {e}")
```

<Note>
  The SDK automatically retries failed requests up to 2 times by default. You can configure retries when initializing the client with `max_retries=0` or per-request.
</Note>

***

<Snippet file="dry-run.mdx" />

***

<Snippet file="review-flagged.mdx" />

***

<Snippet file="all-done.mdx" />
