Python library to search Magic The Gathering cards information on scryfall.com
Project description
PyScryfall: A WIP/unofficial Scryfall API Wrapper
Python client for the Scryfall REST API: search Magic: The Gathering cards by name or Scryfall ID and work with typed card payloads (dataclass models).
Requirements: Python 3.12+, requests. Network access is required for live API calls.
Installation
Using uv (recommended for this repo):
uv sync
That creates or updates .venv, installs runtime dependencies, and includes the dev dependency group (pytest) by default via default-groups in pyproject.toml.
To install only production dependencies in another workflow, use your tool’s equivalent of installing the pyscryfall project with its [project] dependencies.
Package layout
The library lives under src/pyscryfall/ on disk. After uv sync or pip install, you import it as pyscryfall. Modules split HTTP entry points, typed JSON models, errors, and internal parsing helpers.
Repository layout (high level):
src/pyscryfall/ # installable package (api, schemas, exceptions, helpers, __init__)
tests/
pyproject.toml
flowchart LR
subgraph public [Public surface]
init["pyscryfall/__init__.py"]
end
subgraph impl [Implementation]
api["api.py"]
schemas["schemas.py"]
exceptions["exceptions.py"]
helpers["helpers.py"]
end
init --> api
init --> schemas
init --> exceptions
api --> schemas
api --> exceptions
schemas --> helpers
| Module | Role |
|---|---|
pyscryfall.api |
GET requests to Scryfall, JSON parsing, validation of object field, construction of ScryfallCard / ScryfallCardList. |
pyscryfall.schemas |
Dataclasses mirroring Scryfall card and list JSON; from_dict / to_dict (and list loads / dumps). |
pyscryfall.exceptions |
ScryfallApiError for HTTP failures and API error payloads; ScryfallErrorBody for structured error fields. |
pyscryfall.helpers |
Internal helpers (_optional_model, _list_of, …) used by schemas; not part of the public __all__. |
The package root re-exports the search functions, core card types, and exceptions (see src/pyscryfall/__init__.py __all__).
API and data flow
High-level flow from your code to typed objects:
sequenceDiagram
participant App as YourCode
participant API as pyscryfall.api
participant HTTP as ScryfallHTTPServer
participant Sch as schemas
App->>API: search_cards_by_name or search_card_by_id
API->>HTTP: GET cards/search or GET cards/id
HTTP-->>API: JSON body
API->>API: parse JSON, check object type
alt success list
API->>Sch: ScryfallCardList.from_dict
Sch-->>App: ScryfallCardList
else success card
API->>Sch: ScryfallCard.from_dict
Sch-->>App: ScryfallCard
else HTTP error or object error
API-->>App: ScryfallApiError
end
search_cards_by_name(name, …)→GET /cards/searchwith aname:"…"query (quotes escaped). ReturnsScryfallCardList(first page only; usenext_pageif you implement pagination).search_card_by_id(card_id, …)→GET /cards/:id. Returns a singleScryfallCard.
Both accept optional session (requests.Session) and timeout. Base URL defaults to https://api.scryfall.com; override with environment variable SCRYFALL_BASE_URL (e.g. for tests or mocks).
Class and composition model
ScryfallCard is the main aggregate: many optional fields and nested dataclasses. ScryfallCardList wraps a page of cards.
classDiagram
direction TB
class ScryfallCardList {
+str object
+int total_cards
+bool has_more
+list data
+str next_page
+from_dict()
+loads()
+to_dict()
+dumps()
}
class ScryfallCard {
+str id
+str name
+from_dict()
+to_dict()
}
class CardFace {
+from_dict()
}
class ImageUris
class Prices
class PreviewInfo
class RelatedUris
class PurchaseUris
class ScryfallRelatedCard
ScryfallCardList "1" --> "*" ScryfallCard : data
ScryfallCard "0..*" --> CardFace : card_faces
ScryfallCard "0..1" --> ImageUris : image_uris
ScryfallCard "0..1" --> Prices : prices
ScryfallCard "0..1" --> PreviewInfo : preview
ScryfallCard "0..1" --> RelatedUris : related_uris
ScryfallCard "0..1" --> PurchaseUris : purchase_uris
ScryfallCard "0..*" --> ScryfallRelatedCard : all_parts
CardFace "0..1" --> ImageUris : image_uris
Errors from the library use ScryfallApiError: message, optional http_status, and optional body (ScryfallErrorBody with code, details, status, etc.) when Scryfall returns an error object.
Usage examples
Search by name (all prints on the first page)
from pyscryfall import search_cards_by_name, ScryfallCardList
result: ScryfallCardList = search_cards_by_name("Lightning Bolt")
print(result.total_cards, result.has_more)
for card in result.data:
print(card.name, card.set_name, card.collector_number)
Optional arguments match Scryfall’s search API (see docstrings): e.g. unique="prints", order="released".
Fetch a single card by Scryfall ID
from pyscryfall import search_card_by_id, ScryfallCard
card: ScryfallCard = search_card_by_id("de652420-eacf-4f9d-9f13-c6bc02b0fa72")
print(card.name, card.type_line, card.oracle_text)
Handle API and HTTP errors
from pyscryfall import search_card_by_id, ScryfallApiError
try:
search_card_by_id("00000000-0000-0000-0000-000000000000")
except ScryfallApiError as exc:
print(exc)
print(exc.http_status)
if exc.body:
print(exc.body.code, exc.body.details)
Custom session or timeout
import requests
from pyscryfall import search_cards_by_name
session = requests.Session()
session.headers["User-Agent"] = "MyApp/1.0"
cards = search_cards_by_name("Island", session=session, timeout=60.0)
Serialize models
from pyscryfall import search_card_by_id
card = search_card_by_id("de652420-eacf-4f9d-9f13-c6bc02b0fa72")
payload = card.to_dict()
For a stored JSON string of a list response, ScryfallCardList.loads(s) and ScryfallCardList.dumps() are available on the list type.
Running tests
Tests live under tests/ and call the real Scryfall API, so they need network access.
With uv (from the repository root):
uv sync
uv run pytest
Verbose output:
uv run pytest -v
uv sync installs this project in editable mode so import pyscryfall works. Pytest is configured in pyproject.toml with testpaths = ["tests"] and pythonpath = ["."].
References
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyscryfall-0.1.0.tar.gz.
File metadata
- Download URL: pyscryfall-0.1.0.tar.gz
- Upload date:
- Size: 32.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c468363d3f5e5517171f632fe069eafcc7c16164eb5504f93f77798caa7f371
|
|
| MD5 |
ba261150eca624697d84455b0829f4f9
|
|
| BLAKE2b-256 |
d3a89ad086b3e3c8f88857ed59e1d1d1e37af9043bd4ebbc4829f76dceabacf4
|
File details
Details for the file pyscryfall-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pyscryfall-0.1.0-py3-none-any.whl
- Upload date:
- Size: 22.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adef5397e8393ce42c18a3ffa46c72a3ebb88aa07911cdf22fed0a710642395a
|
|
| MD5 |
30d26b173798677aea55dbb3c4d24fb7
|
|
| BLAKE2b-256 |
01277b17a840fb173eadccdbecf4b02ccf750d7e8b46bc3c59e54b9ef73633ce
|