NSFW Detection API

Version 2.197 (Release Notes ↗)

Description

The NSFW Detection API endpoint helps developers and product teams score images or video frames for adult content before that media reaches end users. It is a practical fit for upload moderation, marketplace compliance, community safety rules, and review queues where you need a fast signal to accept, flag, or block content. The returned score can also drive automated workflows with endpoints such as blur, encrypt, and mogrify when you need to censor or transform flagged assets on the fly.

HTTP Methods

GET, POST

HTTP Parameters

Required

Fields Type Description
img URL Input image URL. If you want to upload your image directly from your app, then submit a multipart/form-data POST request.
key String Your PixLab API Key ↗.

POST Request Body

This section explains when to use a POST request instead of a simple GET request.

Allowed Content-Types:

  • multipart/form-data
  • application/json

Use multipart/form-data when you want to upload an image directly from your app. Refer to the REST API code samples for a working implementation. If you prefer application/json, upload the asset first with store and then pass the stored image reference to this endpoint.

HTTP Response

application/json

This endpoint returns a JSON response. The score field is the main moderation signal, and values closer to 1.0 indicate a higher probability of adult content. Response structure:
Fields Type Description
status Integer HTTP status code. A value of 200 indicates success.
score Float NSFW probability score in the 0.0 to 1.0 range.
error String Error details returned when status ≠ 200.

Code Samples


import requests
from typing import Dict, Any

def censor_nsfw_image(image_url: str, api_key: str) -> None:
    """Check and censor NSFW content from an image using PixLab API."""
    
    # NSFW detection endpoint
    nsfw_params = {
        'img': image_url,
        'key': api_key
    }
    
    try:
        # Check for NSFW content
        response = requests.get(
            'https://api.pixlab.io/nsfw',
            params=nsfw_params,
            timeout=10
        )
        response.raise_for_status()
        nsfw_data: Dict[str, Any] = response.json()

        if nsfw_data['status'] != 200:
            print(f"Error: {nsfw_data.get('error', 'Unknown error')}")
            return

        if nsfw_data['score'] < 0.5:
            print("No adult content detected in this image")
            return

        # Censor NSFW content
        print("Censoring NSFW image...")
        blur_params = {
            'img': image_url,
            'key': api_key,
            'rad': 50,
            'sig': 30
        }
        
        blur_response = requests.get(
            'https://api.pixlab.io/blur',
            params=blur_params,
            timeout=10
        )
        blur_response.raise_for_status()
        blur_data: Dict[str, Any] = blur_response.json()

        if blur_data['status'] != 200:
            print(f"Error: {blur_data.get('error', 'Unknown error')}")
        else:
            print(f"Censored image: {blur_data['link']}")

    except requests.exceptions.RequestException as e:
        print(f"API request failed: {e}")

if __name__ == "__main__":
    # Configuration
    TARGET_IMAGE = 'https://i.redd.it/oetdn9wc13by.jpg'
    API_KEY = 'PIXLAB_API_KEY'  # Replace with your actual API key
    
    censor_nsfw_image(TARGET_IMAGE, API_KEY)
← Return to API Endpoint Listing