Metadata-Version: 2.5
Name: sfwapi
Version: 0.1.1
Summary: Official Python client for the SFW API: NSFW and nudity detection for images, GIFs and videos with four content levels
Project-URL: Source, https://github.com/sfwbot/sfwapi-python
Project-URL: Issues, https://github.com/sfwbot/sfwapi-python/issues
Project-URL: Homepage, https://sfwapi.dev
Project-URL: Documentation, https://sfwapi.dev/docs/sdks/python
Project-URL: API Reference, https://api.sfwapi.dev/reference
Project-URL: Playground, https://api.sfwapi.dev/playground
Project-URL: Changelog, https://sfwapi.dev/changelog
Author-email: SFW API <support@sfwapi.dev>
License-Expression: MIT
License-File: LICENSE
Keywords: adult content,async,content moderation,gif moderation,image moderation,nsfw,nsfw api,nsfw detection,nudity detection,sfwapi,ugc moderation,video moderation
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Multimedia :: Graphics
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: httpx>=0.27
Provides-Extra: test
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# sfwapi

Official Python client for the [SFW API](https://sfwapi.dev): NSFW and nudity detection for images, GIFs and videos. Provides both synchronous (`SfwApi`) and asynchronous (`AsyncSfwApi`) clients built on `httpx`, with Python 3.9+ type annotations.

Media is processed in memory and never stored. Every account includes 500 free credits a month, with no card required.

## Install

```bash
pip install sfwapi
```

Get an API key at [sfwapi.dev/console](https://sfwapi.dev/console) and store it in `SFWAPI_API_KEY`.

## Quick start

```python
import os
from sfwapi import SfwApi, SfwApiError

with SfwApi(api_key=os.environ["SFWAPI_API_KEY"]) as api:
    # Analyse media at a public URL with a blocking policy
    r = api.analyze_url(
        "https://example.com/photo.jpg",
        policy={"block_level": 2, "block_sex_objects": True, "art": "same_as_photos"},
    )
    print(r["nudity"]["level"])     # 0 | 1 | 2 | 3
    print(r["nudity"]["scores"])    # {"revealing": ..., "implied_nudity": ..., "explicit": ...}
    print(r["policy"]["blocked"])   # True | False
    print(r["usage"]["balance"])    # credits remaining

    # Upload a local image, GIF or video
    g = api.analyze_file("clip.gif", max_frames=5, include_frames=True)
```

### Async client

`AsyncSfwApi` exposes the same methods with `await` and works with FastAPI, Starlette and asyncio services:

```python
import os
from sfwapi import AsyncSfwApi

async with AsyncSfwApi(api_key=os.environ["SFWAPI_API_KEY"]) as api:
    r = await api.analyze_bytes(upload_bytes, policy={"block_level": 2})
    if r["policy"]["blocked"]:
        ...
```

## Content levels

Each score runs from `0` to `100` and counts above `50` (`CUTOFF`). `nudity.level` is the highest level that counts.

| Level | Name | Meaning |
|---|---|---|
| `0` | Everyday | Normal photos, clothed people, landscapes, objects |
| `1` | Revealing | Swimwear, lingerie, cleavage and other revealing clothing |
| `2` | Implied nudity | Partial or covered nudity, suggestive poses without exposed genitalia |
| `3` | Explicit | Exposed genitalia or explicit sexual acts |

Responses also include `flags.art` (drawings, anime, paintings, stylised 3D) and `flags.sex_object` (adult toys and objects). Policy options (`art`: `same_as_photos`, `allow`, `block_from_1`, `block_from_2`, `block_from_3`) let you handle illustrated content separately from photos.

## Methods

| Method | Description |
|---|---|
| `analyze_url(url, ...)` | Analyse an image, GIF or video at a public `http(s)` URL |
| `analyze_bytes(data, ...)` | Upload raw `bytes` or `bytearray` as multipart |
| `analyze_file(path, ...)` | Read and upload a local file by path |
| `me()` | Account tier, rate limits and credit balance |
| `usage()` | Current month's request, credit and byte totals |

Analysis methods accept `max_frames` (1 to 32), `include_frames` and `policy`. A request costs 1 credit per image, or 1 per analysed frame for a GIF or video.

## Errors and retries

Non-2xx responses raise `SfwApiError` with `.status`, `.type`, `.code`, `.message`, `.request_id` and `.doc_url`. Statuses `429`, `503` and `504` are retried twice honouring `Retry-After` (uploads are not retried; pass `max_retries=0` to disable). Failed requests are never charged.

## Links

- Python SDK reference: [sfwapi.dev/docs/sdks/python](https://sfwapi.dev/docs/sdks/python)
- Content levels: [sfwapi.dev/docs/levels](https://sfwapi.dev/docs/levels)
- Interactive playground: [api.sfwapi.dev/playground](https://api.sfwapi.dev/playground)
- OpenAPI reference: [api.sfwapi.dev/reference](https://api.sfwapi.dev/reference)
- Pricing: [sfwapi.dev/pricing](https://sfwapi.dev/pricing)
- Other clients: TypeScript ([`@sfwapi/sdk`](https://www.npmjs.com/package/@sfwapi/sdk)), .NET ([`SfwApi`](https://www.nuget.org/packages/SfwApi)), MCP server ([`@sfwapi/mcp`](https://www.npmjs.com/package/@sfwapi/mcp))
