Skip to main content

A clean, type-safe Python client for the Wallhaven.cc API v1

Project description

xanax

A clean, type-safe Python client for the Wallhaven.cc API v1.

Features

  • Type-safe: All responses are parsed into Pydantic models with full type hints
  • Validated parameters: Search parameters are validated before making API requests
  • Clean error handling: Structured error hierarchy for easy error handling
  • Rate limit aware: Handles rate limiting with optional retry support
  • Secure: API key is never logged or exposed in any output
  • No magic: Explicit method calls, no hidden defaults

Installation

pip install xanax

Or with uv:

uv add xanax

Quick Start

from xanax import Xanax, SearchParams, Purity, Sort

# Create a client (optional API key for NSFW content)
client = Xanax(api_key="your-api-key")

# Search for wallpapers
params = SearchParams(
    query="+anime -sketch",
    purity=[Purity.SFW],
    sorting=Sort.TOPLIST,
)

results = client.search(params)

# Iterate through results
for wallpaper in results.data:
    print(f"{wallpaper.resolution} - {wallpaper.path}")

# Check pagination
print(f"Page {results.meta.current_page} of {results.meta.last_page}")

Authentication

The Wallhaven API requires an API key for NSFW content. You can get your API key from your Wallhaven account settings.

client = Xanax(api_key="your-api-key")

The API key is stored securely and never exposed in any string representations.

Search Parameters

SearchParams provides type-safe search parameters:

from xanax import SearchParams
from xanax.enums import Category, Purity, Sort, Order, TopRange, Color, FileType

params = SearchParams(
    query="+nature -water",      # Include/exclude tags
    categories=[Category.GENERAL, Category.ANIME],
    purity=[Purity.SFW],         # SFW, SKETCHY, NSFW (requires API key)
    sorting=Sort.TOPLIST,        # date_added, relevance, random, views, favorites, toplist
    order=Order.DESC,            # desc or asc
    top_range=TopRange.ONE_MONTH, # 1d, 3d, 1w, 1M, 3M, 6M, 1y (only with toplist sorting)
    resolutions=["1920x1080", "2560x1440"],
    ratios=["16x9", "4x3"],
    colors=[Color.BLUE, Color.GREEN],
    file_type=FileType.PNG,     # Filter by file type (png or jpg)
    like="94x38z",              # Find wallpapers similar to this ID
    page=1,
    seed="abc123",              # For random sorting consistency
)

API Reference

Xanax Client

client = Xanax(api_key=None, timeout=30.0, max_retries=0)

Methods:

  • client.wallpaper(id: str) -> Wallpaper - Get a specific wallpaper
  • client.search(params: SearchParams) -> SearchResult - Search for wallpapers
  • client.tag(id: int) -> Tag - Get tag information
  • client.settings() -> UserSettings - Get authenticated user's settings
  • client.collections(username: str | None = None) -> list[Collection] - Get collections
  • client.collection(username: str, id: int) -> CollectionListing - Get wallpapers in a collection

Error Handling

from xanax import Xanax
from xanax.errors import (
    XanaxError,
    AuthenticationError,
    RateLimitError,
    NotFoundError,
    ValidationError,
    APIError,
)

try:
    client = Xanax(api_key="your-key")
    results = client.search(SearchParams(query="anime"))
except AuthenticationError:
    print("Invalid API key")
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after} seconds")
except NotFoundError:
    print("Resource not found")
except ValidationError as e:
    print(f"Invalid parameters: {e.message}")
except APIError as e:
    print(f"API error: {e.status_code}")

Pagination Helper

from xanax.pagination import PaginationHelper

results = client.search(params)

# Use the helper for easier pagination
helper = PaginationHelper(results.meta)

if helper.has_next:
    next_page = helper.next_page_number()
    next_params = params.with_page(next_page)
    next_results = client.search(next_params)

Models

All API responses are parsed into typed Pydantic models:

  • Wallpaper - Single wallpaper details
  • Tag - Tag information
  • Uploader - User uploader info with avatar
  • Avatar - User avatar at different sizes
  • Thumbnails - Thumbnail URLs
  • SearchResult - Search results with wallpapers and meta
  • PaginationMeta - Pagination information
  • QueryInfo - Resolved search query info
  • UserSettings - User preferences
  • Collection - Collection info
  • CollectionListing - Collection wallpapers

Enumerations

All search parameters have type-safe enums:

  • Category - general, anime, people
  • Purity - sfw, sketchy, nsfw
  • Sort - date_added, relevance, random, views, favorites, toplist
  • Order - desc, asc
  • TopRange - 1d, 3d, 1w, 1M, 3M, 6M, 1y
  • Color - All valid wallhaven colors
  • FileType - png, jpg

Rate Limiting

The Wallhaven API allows 45 requests per minute. By default, the client fails fast when rate limited. You can enable automatic retries:

client = Xanax(max_retries=3)  # Retry up to 3 times with exponential backoff

Development

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run with coverage
pytest --cov=xanax

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xanax-0.1.1.tar.gz (49.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xanax-0.1.1-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file xanax-0.1.1.tar.gz.

File metadata

  • Download URL: xanax-0.1.1.tar.gz
  • Upload date:
  • Size: 49.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xanax-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3ed797e7b6b577da108af8ef6f6cdc13bb44cbc9f1c52187e8d92d79178eacc5
MD5 77dfe83dd36dd6bb8e96f79374290f42
BLAKE2b-256 920f988681f8cebbfbf80c8678f1907f0252e589bc602b03ed98c34711b990ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for xanax-0.1.1.tar.gz:

Publisher: publish.yml on violhex/Xanax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xanax-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: xanax-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xanax-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5c9ccfb0a4a359b2d5104aba8eda4f758d174a7ee413e2a2a3546757a9ebca8
MD5 03701e3e6571dd61b7b8df21cc79d472
BLAKE2b-256 e1b507ec24f4209eda860fddc2fe57d7745c7711896cb701bb1520010824a9d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for xanax-0.1.1-py3-none-any.whl:

Publisher: publish.yml on violhex/Xanax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page