November 13th, 2008
It seems like every great web app has an API of some sort. Sometimes they are a complete API that allows you to use the applications entire functionality from logging in, to creating new “content”, to sending messages. Others only allow for some access for users, which I can understand. They’re usually just getting started and are not sure of how many people are going to be interested in using an API, and they don’t want to accidentally kill the application because someone’s script is stuck in a loop that repeatedly hits their server.
I personally love API’s. It’s fun building little AIR apps that talk with someone else’s hard work. If you have a web app (that is not powered by someone else’s API) and don’t plan to offer your users an API, you’re going to wish you did when your competition starts getting more attention because of what the community has built from its API. Most if not all web apps that offer API’s offer an XML version. Your script kindly asks their system for information and you get a nice XML response, which is great, every programming language supports XML (or should).
There is a problem though. Even though use of the colon and hyphen in XML tags is permitted according to the XML Recommendation at the W3C, they can be pure hell for a developer. Case in point, the other night I was playing with the Basecamp API in Flex. Basecamp’s API returns XML as expected, however you get tags like and . Great, they are valid XML names, however Flex doesn’t like it when you try:
var XMLDATA:XML = new XML(loader.data);
for each (var t:* in XMLDATA.todo-lists)
{
// Do something with the XML here.
}
It throws a fit about the hyphen because it considers it a minus sign, so my code was reading as XMLDATA.todo minus lists which obviously does nothing. I spent a great deal of time searching Google and even posted to Twitter a few times looking for help. Turns out in order to get this to work you have to do:
var XMLDATA:XML = new XML(loader.data);
for each (var t:* in XMLDATA.elements(‘todo-lists'))
{
// Do something with the XML here.
}
Does that make sense right away? Maybe to Flex developers, but to someone like myself who is only playing around with Flex and just learning, not so much. I’m not sure how Flex handles a colon in a name my guess is that it probably bombs like the hyphen does in the first example, but will work fine in the second.
There are two other things I want to touch on about building an XML API. The first is don’t make your API overly secure. Sure secure it enough that you require a username/password combo be sent via HTTP Headers, but don’t require a user to pass along a MD5 checksum of the request in order to get a response (I know someone who did this and the app really doesn’t require it). The second thing is name your tags so they are human readable, a tag with a persons user id should be called something like “user_id” or “userId” not “r”, “s”, “u”, “i”, or “p”. If you absolutely want to do that, then be kind and provide a legend for your API users to reference.