Magic: The Gathering SDK for JavaScript

repository·master·Indexed 43 days ago

https://github.com/magicthegathering/mtg-sdk-javascript

A JavaScript wrapper around the MTG API (magicthegathering.io) for programmatically querying cards and sets. Version 1.0.1 provides methods to find entities by ID, filter by properties using .where(), and retrieve paginated results via .all().

Tokens
1.6K
Snippets
8
Records
12
Agent score
55%

What's inside MTG SDK

  1. Understand the MTG Local Server technology stack

    master

    The MTG Local Server is composed of the following technologies:

    Server-Side:

    • Runtime: NodeJS (requires version > v6)
    • API Framework: ExpressJS (v4.x)
    • View Engine: PUG

    Client-Side:

    • Framework: React JS
    • Bundler: Webpack (v3.10.0) for both JavaScript and Styles
    • Communication: All API calls transfer JSON
    • Source Location: All client-side code is located in ./src
  2. Configure the MTG Local Server environment

    master

    The MTG Local Server uses an .env file in the root directory to manage configuration. You must define the PORT and the ENV mode.

    Supported values for ENV:

    • development: Enables Webpack dev server, hot reload, and automatic compilation. CORS is disabled. In Views/home.pug, you must point to layout.pug.
    • production: Disables Webpack dev server and hot reload. Expects a build to have been run. CORS is enabled. All bundled files are rendered to the /public directory. In Views/home.pug, you must point to layout.production.pug.
    PORT=8080
    ENV=development
  3. Query cards and sets with specific properties

    master

    Use the .where() method to filter cards or sets based on specific property values.

    Note: For sets, you can currently only query using the name and block fields.

    // Querying cards by supertypes and subtypes
    const cardResult = await mtg.card.where({ supertypes: 'legendary', subtypes: 'goblin' })
    console.log(cardResult.cards[0].name) // e.g., "Squee, Goblin Nabob"
    
    // Querying sets by block and border
    const setResult = await mtg.set.where({ block: 'Shadows over Innistrad', border: 'black' })
    console.log(setResult.sets[0].name)
    const result = await card.where({ supertypes: 'legendary', subtypes: 'goblin' })
    console.log(cards[0].name) // "Squee, Goblin Nabob"
    
    const result = await set.where({ block: 'Shadows over Innistrad', border: 'black' })
    console.log(sets[0].name) // "Welcome Deck 2016"
    console.log(sets[1].name) // "Shadows over Innistrad"
    console.log(sets[2].name) // "Eldritch Moon"
  4. Retrieve all results across multiple pages using .all()

    master

    To retrieve all matching entities across multiple pages of results, use the .all() method. This method returns an event emitter that you can listen to using the .on('data', callback) pattern. You can control the number of results per page using the pageSize option.

    // Retrieve all cards matching a name
    mtg.card.all({ name: 'Squee', pageSize: 1 })
      .on('data', card => {
        console.log(card.name)
      })
    
    // Retrieve all sets matching a name
    mtg.set.all({ name: 'limited' })
      .on('data', set => {
        console.log(set.name)
      })
    card.all({ name: 'Squee', pageSize: 1 })
    .on('data', card => {
        console.log(card.name)
    })
    
    set.all({ name: 'limited' })
    .on('data', set => {
      console.log(set.name)
    })
  5. Find a card or set by ID

    master

    Use the .find() method on the card or set objects to retrieve a specific entity by its unique identifier.

    const mtg = require('mtgsdk')
    
    // Find a card by ID
    const cardResult = await mtg.card.find(3)
    console.log(cardResult.card.name) // e.g., "Black Lotus"
    
    // Find a set by ID
    const setResult = await mtg.set.find('AER')
    console.log(setResult.set.name) // e.g., "Aether Revolt"
    const mtg = require('mtgsdk')
    
    const result = await mtg.card.find(3)
    console.log(result.card.name) // "Black Lotus"
    
    const result = await mtg.set.find('AER')
    console.log(result.set.name) // "Aether Revolt"
  6. Access card and set data via the MTG SDK

    master

    The main entrypoint of the SDK provides access to different Magic: The Gathering data collections. You can interact with card and set using a query builder pattern, while type, supertype, and subtype provide access to unqueryable metadata collections. These exported objects are the primary way to fetch data from the SDK.

    const mtg = require('mtgsdk');
    
    // Accessing cards and sets (queryable)
    mtg.card.find({ name: 'Black Lotus' });
    mtg.set.find({ code: 'LEA' });
    
    // Accessing types (unqueryable)
    mtg.type.all();
  7. Reference the MTG SDK exported collections

    master

    The SDK exports the following collections. Note that card and set are queryable via the query builder, whereas the type-related collections are unqueryable.

    card: queryable (cards)
    set: queryable (sets)
    type: unqueryable (types)
    supertype: unqueryable (supertypes)
    subtype: unqueryable (subtypes)