Twitter API trick: use standard update method for direct messages

Posted by Matt Thommes on December 23, 2008 | Post type: Gain

If you utilize the Twitter API, it may be refreshing to know that you can save some development time when working with direct messages.

The Twitter API provides various direct message methods, including one called "new", which sends a user a new direct message.

The URL for the new method looks like this:

http://twitter.com/direct_messages/new.format

This contrasts the standard "update" method URL (for public status updates), which looks like this:

http://twitter.com/statuses/update.format

Rather than building your application around these two separate, distinct URL's, you can choose to use only the "update" method, even for sending direct messages.

Just prepend the "update" method call's text (the actual Twitter status update) with d username, where username is the Twitter user to send the direct message to. This is often referred to as a "shorthand" way of sending a direct message, mainly for SMS users.

Even though the "update" method is not mentioned as an official way to send a direct message, it still works. The magic occurs on Twitter's end, where the update is properly parsed to check for d username at the beginning.

This is a smart feature of the API, because they made it work just like the standard web interface works.

It's funny, though - I can't find any mention of this capability in the API documentation. Nothing should be assumed, especially something that saves developers time.

Interface logic

Depending on how your application's interface is set up, it may be easier to just use the official direct message method. But you should at least inform your users that the alternate, "shorthand" approach is available.

About the author(s)

Matt Thommes is an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Comments

Note: Comments may be viewed by authors, but if you have a more specific question you'd like to ask them, please email matt.thommes@paininthetech.com.