Skip to main content

A modern, Async-ready Unsplash Python API client with Pydantic

Project description

Unsplash Python SDK

PyPI version Python Versions Test License: MIT Ruff pre-commit

[!WARNING] This is an unofficial SDK and is currently under active development.

A modern, type-safe Python client for the Unsplash API. Built with Pydantic v2 for robust data validation and httpx for high-performance sync and async support.

✨ Features

  • Type Safe: Fully typed response models using Pydantic v2.
  • Async Native: First-class async/await support with AsyncUnsplashClient.
  • Modern: Built on httpx (HTTP/2 support, connection pooling).
  • Developer Friendly: IDE auto-completion, detailed error messages, and fully documented resources.
  • Resource Oriented: Clean API design mirroring the Unsplash documentation (Photos, Users, Collections, Search).

🛠️ Installation

Install usage pip:

pip install unsplash-pydantic

Or using Poetry:

poetry add unsplash-pydantic

🚀 Quick Start

Synchronous Client

Perfect for scripts and standard applications.

import os
from unsplash import UnsplashClient

# Initialize the client
client = UnsplashClient(access_key=os.getenv("UNSPLASH_ACCESS_KEY"))

# Get a random photo of nature
photo = client.photos.random(query="nature", orientation="landscape")

# Access typed fields
print(f"Photo by: {photo.user.name}")
print(f"Description: {photo.description}")
print(f"Download URL: {photo.urls.full}")

# Search for photos
results = client.search.photos("mountains", page=1, per_page=10)
print(f"Found {results.total} photos")

Asynchronous Client

Ideal for high-concurrency applications (FastAPI, etc).

import asyncio
import os
from unsplash import AsyncUnsplashClient

async def main():
    async with AsyncUnsplashClient(access_key=os.getenv("UNSPLASH_ACCESS_KEY")) as client:
        # Fetch user profile asynchronously
        user = await client.users.get("ousplash")
        print(f"{user.name} has {user.total_photos} photos")

        # Get their latest photos
        photos = await client.users.photos(user.username, per_page=5)
        for photo in photos:
            print(f"- {photo.id}: {photo.urls.regular}")

if __name__ == "__main__":
    asyncio.run(main())

📚 Core Concepts

Error Handling

All specific errors catch a base UnsplashError. Common HTTP errors (401, 404, 429) are mapped to specific exceptions.

from unsplash import UnsplashClient, UnsplashError, RateLimitError

try:
    client.photos.get("invalid-id")
except RateLimitError as e:
    print(f"Rate limited! Limit: {e.limit}, Remaining: {e.remaining}")
except UnsplashError as e:
    print(f"API Error: {e.message}")

Unsplash Guidelines

This SDK helps you follow Unsplash API Guidelines:

  1. Attribution: The Photo model includes the user object with name and links to properly credit photographers.
  2. Download Tracking: Use client.photos.track_download(id) or client.photos.download(id) to trigger the download event required by the API.
  3. Hotlinking: photo.urls provides hotlinkable URLs directly.

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


This library is not officially affiliated with Unsplash.

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

unsplash_pydantic-0.2.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

unsplash_pydantic-0.2.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file unsplash_pydantic-0.2.1.tar.gz.

File metadata

  • Download URL: unsplash_pydantic-0.2.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for unsplash_pydantic-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7ed6c5eeb5f2930019d2b5fd7e6198322aff2ad4c710fff819fda6860f579e67
MD5 526b7aa832282945ed1829fdb8260229
BLAKE2b-256 d872d8bf5b77f6e2000ddf6b7e81b2baf78b287ce37015dca7b6dad26ea725bb

See more details on using hashes here.

File details

Details for the file unsplash_pydantic-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for unsplash_pydantic-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4008040969c5a11a19a6e25c47f3d27bc6efd11d2fe3e95d267dbec179f8ba77
MD5 7dfcdb439c53291d1e21ba3d55b46e45
BLAKE2b-256 3575d06472200f11dddffbeccbe081862936ddb3a7d1f9d711001f1f6927eea3

See more details on using hashes here.

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