The new SWX Twitter API (and how to build your first Flash Twitter mashup in four lines of code!)

Twitter released an update to the official Twitter API this month and I've updated the SWX Twitter API so that it now has all the latest official Twitter API methods as well as some new custom ones.

Try it out!

You can try out the SWX Twitter API methods online right this moment by using the SWX Service Explorer.

If you want to develop Flash (and Flash Lite) applications that use data from Twitter, you can get started without installing anything at all by using the public SWX gateway on this site. (The public SWX gateway is located at http://swxformat.org/php/swx.php).

In fact, you can create your very first Twitter mashup in just four lines of code by following these simple instructions:

  1. Open the SWX Data Analyzer
  2. In Flash, create a new FLA.
  3. Create a new movie clip and give it the instance name loader.
  4. On the frame that has the loader movie clip, add the following script:
    loader.serviceClass = "Twitter";
    loader.method = "getPublicUpdates";
    loader.debug = true;
    loader.loadMovie("http://swxformat.org/php/swx.php", "GET");

That's all you need to get the latest public timeline updates from Twitter into Flash (you can see that the data is being loaded if you look in the SWX Data Analyzer).

To display the updates in Flash, trace out the value of loader.result.

Note: The Flash IDE will give you a security sandbox warning but the application will run correctly (look in the SWX Data Analyzer or trace out the loaded data to check this.) This warning occurs because you are running the SWF in the Flash IDE. If you put the SWF file on the same domain as the SWX gateway, it will also work without requiring any further code. However, if you want to use the Public SWX gateway and deploy your Flash applications to your own server, you must either manually call System.security.allowDomain, or (the recommended way is to) use the SWX Full API, as exlained below.

Notice that you didn't need to download or install anything. That's because SWX is native! It uses SWF files to store and exchange data.

That's all you need to start working with SWX and Twitter! No API, external classes, etc. are necessary! And that little snippet of code (which, by the way, fits on to a moo card) also shows you exactly how SWX works. It loads the data in a SWF file and the data is accessible the moment it loads as native Flash objects. No deserialization necessary.

Of course, if you want to host your own SWX gateway or develop and test offline on your own machine, you can download and install SWX to your own machine. That will also allow you to create your own service classes and APIs.

Using the SWX Full API

If you don't want to work manually with movie clips (and that is understandable), you can use the SWX Full API -- a completely abstract (and recommended) way of working with SWX.

Here's how to display the text of the last status update of your friends using the SWX Full API:

import org.swxformat.*;

var swx:SWX = new SWX();
swx.gateway = "http://swxformat.org/php/swx.php";
swx.encoding = "POST";
swx.debug = true;

var callParameters:Object =
{
    serviceClass: "Twitter",
    method: "getNumFriendsUpdates",
    args: ["aral", 1],
    result: [this, resultHandler]
}

swx.call(callParameters);

function resultHandler(event:Object)
{
    // Display the text property of the first result.
    trace (event.result[0].text);
}

I hope you enjoy working with the SWX Twitter API (also check out the Flickr API while you're at it) and, again, please feel free to hit the public SWX gateway here on swxformat.org.

New Twitter API method reference

The newly added official Twitter API methods are listed below for your convenience. You can find (and test) the full list of methods in the SWX Service Explorer.

Status methods

User methods

Direct message methods

Friendship methods

New custom methods

Status methods

Friendship methods

Deprecated methods

The following methods are deprecated as of the upcoming Beta 1.4 release in favor of the new official API methods that replace them. Please do not rely on these methods in the future as they may be removed from the API completely later on.

Comments