scryfall

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package scryfall is the library behind the scryfall command line: the HTTP client, request shaping, and the typed data models for the Scryfall MTG card API.

The Client here is the spine every command shares. It sets a real User-Agent, paces requests so a busy session stays polite (Scryfall asks for 50-100ms between requests), and retries the transient failures (429 and 5xx) that any public API throws under load.

Index

Constants

View Source
const BaseURL = "https://" + Host

BaseURL is the root every request is built from.

View Source
const DefaultUserAgent = "scryfall-cli/dev (+https://github.com/tamnd/scryfall-cli)"

DefaultUserAgent identifies the client to Scryfall. Scryfall's docs ask for a real, honest User-Agent so they can contact you if something goes wrong.

View Source
const Host = "api.scryfall.com"

Host is the API host this client talks to, and the host the URI driver in domain.go claims.

Variables

This section is empty.

Functions

This section is empty.

Types

type Card

type Card struct {
	ID          string   `kit:"id" json:"id"`
	Name        string   `json:"name"`
	ManaCost    string   `json:"mana_cost"`
	CMC         float64  `json:"cmc"`
	TypeLine    string   `json:"type_line"`
	OracleText  string   `json:"oracle_text"`
	Power       string   `json:"power"`
	Toughness   string   `json:"toughness"`
	Colors      []string `json:"colors"`
	SetCode     string   `json:"set"`
	SetName     string   `json:"set_name"`
	Rarity      string   `json:"rarity"`
	Prices      Prices   `json:"prices"`
	ScryfallURL string   `json:"scryfall_uri"`
	ReleasedAt  string   `json:"released_at"`
}

Card is a single Magic: The Gathering card as returned by the Scryfall API.

type Client

type Client struct {
	HTTP      *http.Client
	UserAgent string
	// Rate is the minimum gap between requests. Zero means no pacing.
	Rate    time.Duration
	Retries int
	// contains filtered or unexported fields
}

Client talks to the Scryfall API over HTTPS.

func NewClient

func NewClient() *Client

NewClient returns a Client with sensible defaults: a 30s timeout, a 100ms minimum gap between requests (Scryfall allows ~10 req/sec), and five retries on transient errors.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) ([]byte, error)

Get fetches url and returns the response body. It paces and retries according to the client's settings. The caller owns nothing extra; the body is read fully and closed here.

func (*Client) GetCardByName

func (c *Client) GetCardByName(ctx context.Context, name string) (*Card, error)

GetCardByName fetches a single card by fuzzy name match via /cards/named.

func (*Client) GetRandomCard

func (c *Client) GetRandomCard(ctx context.Context) (*Card, error)

GetRandomCard fetches a random card from /cards/random.

func (*Client) ListSets

func (c *Client) ListSets(ctx context.Context, limit int) ([]Set, error)

ListSets fetches all sets from /sets and returns up to limit results.

func (*Client) SearchCards

func (c *Client) SearchCards(ctx context.Context, query string, limit int) ([]Card, error)

SearchCards searches for cards matching query. limit caps the results; the max per_page is 175 (one Scryfall page), so we do not paginate for simplicity.

type Domain

type Domain struct{}

Domain is the Scryfall driver. It carries no state; the per-run client is built by the factory Register hands kit.

func (Domain) Classify

func (Domain) Classify(input string) (uriType, id string, err error)

Classify turns any accepted input into the canonical (type, id), so `ant resolve` and `ant url` touch no network. A 36-character UUID (with dashes) is a "card-id"; anything that looks like a scryfall.com URL is a "card-id" too; otherwise it is a "card" (name lookup).

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for the binary's help and version.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate is the inverse: the live https URL for a (type, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type Prices

type Prices struct {
	USD     string `json:"usd"`
	USDFoil string `json:"usd_foil"`
	EUR     string `json:"eur"`
}

Prices holds the market prices for a card in common currencies.

type Set

type Set struct {
	Code       string `kit:"id" json:"code"`
	Name       string `json:"name"`
	SetType    string `json:"set_type"`
	ReleasedAt string `json:"released_at"`
	CardCount  int    `json:"card_count"`
}

Set is a Magic: The Gathering card set as returned by the Scryfall API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL