Creating Custom Pricelists via the JSON API

Fri 17 July 2009

The JSON API has a new cards option, which is the first step in making the API customizable. The cards option is interpreted as an array of card names with optional set codes. If that option is set, the JSON API will return today's prices only for the cards in the list!

A call such as:

http://blacklotusproject.com/json/?cards=black+lotus

Would return a response like this:

[
  {
    "percent_change": "0.00",
    "name": "Black Lotus",
    "url": "http://blacklotusproject.com/cards/LEA/Black+Lotus/",
    "price": "1999.00",
    "up": true,
    "set_code": "LEA",
    "change": "0.00"
  },
  {
    "percent_change": "-4.07",
    "name": "Black Lotus",
    "url": "http://blacklotusproject.com/cards/LEB/Black+Lotus/",
    "price": "1052.89",
    "up": false,
    "set_code": "LEB",
    "change": "-44.71"
  },
  {
    "percent_change": "-1.54",
    "name": "Black Lotus",
    "url": "http://blacklotusproject.com/cards/2ED/Black+Lotus/",
    "price": "757.52",
    "up": false,
    "set_code": "2ED",
    "change": "-11.87",
  }
]

JSON Cards List Interface

The cards list has a few restrictions that try to keep the interface as simple as possible. If you aren't getting the responses you expect, check that your cards list follows these standards.

  • Card names are separated with the pipe character ("|").

    http://blacklotusproject.com/json/?cards=black+vise|time+spiral
  • Spaces in card names should either be escaped with a plus sign ("+") or with the HTML character code %20. Typing spaces into your browser's address bar will work just fine, but you can't copy/paste a URL with spaces in it or use it in code.

  • Card sets are specified by putting the card's expansion code, as defined in the "Expansion Code" column on Wikipedia's Magic: the Gathering sets page, in parentheses after the card's name. The API expects both parentheses to be present and will not recognize expansion codes outside parentheses.

    http://blacklotusproject.com/json/?cards=lightning+bolt+(3ed)
  • Cards with no set specified will return all versions of that card. If the card was only printed in a single set, only a single result for that card will be returned.

  • The JSON API fails silently but returns as many results as it can find. If you mistype a set on a single card within the list, all other cards in the list will still be returned. This might change in the future as the API becomes more complex.
  • JSON responses are returned with a MIME type of application/json, and Firefox's default action when seeing that type is to download the response. To view the JSON responses in Firefox, I recommend JSONView. I used it while testing the JSON API changes and found it hugely helpful.

Preview:

Loading...

Code:

The JavaScript ticker got a new cards option to go along with the update to the JSON API. It takes an array of card names with expansion codes if you need to get specific. Also the timeout option is actually called interval. I mistyped the name when I wrote the last post.

New Ticker Options:

cards
A list of full card names which may also contain expansion codes enclosed in parentheses. Card names are separated by the pipe character ("`|`").
interval
(Correct naming of timeout option) The time in milliseconds between each interval. Default is `40`.

0 comments

Leave a comment