Skip to main content

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/search with a name:"…" query (quotes escaped). Returns ScryfallCardList (first page only; use next_page if you implement pagination).
  • search_card_by_id(card_id, …)GET /cards/:id. Returns a single ScryfallCard.

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

AI Disclosure

Part of this project has been developed with the help of an AI Model. Specifically I used a locally-hosted QWEN3-CODER using Ollama.

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

pyscryfall-0.1.1.tar.gz (34.9 kB view details)

Uploaded Source

Built Distribution

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

pyscryfall-0.1.1-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyscryfall-0.1.1.tar.gz
  • Upload date:
  • Size: 34.9 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

Hashes for pyscryfall-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e39818e0505f984b82facd2aaf0ab06fd25205c87bed1a780e60349b215c64ea
MD5 0e2d09053cdc7aa24011a3e67efda725
BLAKE2b-256 980da7a5bd112796f7f759f060f120625f918d4a2910115ff4feeacdba7a52cc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyscryfall-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 23.1 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

Hashes for pyscryfall-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0b8b62b78b3fe6e5498a5877079d47e708c97ef3d3b07657458f56ef941337d
MD5 7323d2ce703ba13345f00ec38929c2a3
BLAKE2b-256 cd81e4baf3ca56110d5c1d7e5018437da9a0b65e8f8a9fc2c4a93f2a9711b9bf

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