SFW API Endpoint

Version 2.197 (Release Notes ↗)

Description

Detect whether a given image is suitable for work (does not contain adult content). This the opposite of what the nsfw endpoint does.

HTTP Methods

GET, POST

HTTP Parameters

Required

Fields Type Description
img URL Input image URL. If uploading directly from your app, submit a multipart/form-data POST request. Only JPEG & PNG formats supported. Convert other formats using the convert endpoint.
key String Your PixLab API Key ↗.

POST Request Body

This section details the requirements for using a POST request instead of a simple GET request.

Allowed Content-Types:

  • multipart/form-data (direct file upload)
  • application/json (pre-uploaded images)

For JSON requests, images must be publicly accessible. Upload images first via the store endpoint if needed.

HTTP Response

Fields Type Description
status Integer Status code 200 indicates success, any other code indicates failure.
score Float SFW score value between 0 .. 1. Values closer to 1 indicate safer content with no adult material.
error String Error message if status != 200.
application/json

The API returns a JSON response. The score field is the primary indicator - values approaching 1.0 signify safer content. Response structure:

Code Samples


import requests

def check_image_safety(image_url: str, api_key: str) -> None:
    """Check if an image contains adult content using PixLab API."""
    try:
        response = requests.get(
            'https://api.pixlab.io/sfw',
            params={'img': image_url, 'key': api_key},
            timeout=10
        )
        response.raise_for_status()
        data = response.json()

        if data['status'] != 200:
            print(f"Error: {data.get('error', 'Unknown error')}")
        elif data.get('score', 0) > 0.5:
            print("This image is pretty safe & does not appear to have any adult content!")
        else:
            print("This picture is highly NSFW & may contain adult content, perhaps censor it?")
            # Consider calling api.pixlab.io/blur or api.pixlab.io/encrypt
            # to create a censored version of the picture

    except requests.exceptions.RequestException as e:
        print(f"API request failed: {e}")
    except (KeyError, ValueError) as e:
        print(f"Error processing API response: {e}")

if __name__ == "__main__":
    check_image_safety(
        image_url='https://i.redd.it/oetdn9wc13by.jpg',
        api_key='PIXLAB_API_KEY'
    )
← Return to API Endpoint Listing