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.
- Type Safe: Fully typed response models using Pydantic v2.
- Async Native: First-class
async/awaitsupport withAsyncUnsplashClient. - 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).
Install usage pip:
pip install unsplash-pydanticOr using Poetry:
poetry add unsplash-pydanticPerfect 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")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())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}")This SDK helps you follow Unsplash API Guidelines:
- Attribution: The
Photomodel includes theuserobject withnameandlinksto properly credit photographers. - Download Tracking: Use
client.photos.track_download(id)orclient.photos.download(id)to trigger the download event required by the API. - Hotlinking:
photo.urlsprovides hotlinkable URLs directly.
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
This library is not officially affiliated with Unsplash.