Scripting Twitter with cURL
March 18th, 2007 by Stephane KattoorTwitter already provides an API but it is currently very limited. It will let you fairly easily change your status, but won’t let you send a direct message.
Comes in cURL. cURL is a very versatile command line utility which is designed to script web pages interactions. As a little demo, I’ll show you how to use it to easily overcome the shortcomings of Twitter’s API.
cURL availability
cURL is available for many Unix/Linux systems as well as for Windows. You can download a binary package from the download page for the cURL project. If you’re using a recent Linux distro, chances are that it is already installed.
Scripting status update
For this very task, we can simply use Twitter’s API :
curl --basic --user username:password --data status="Having fun with cURL" http://twitter.com/statuses/update.xml
Let’s explain this snippet a bit :
--basic --user username:password: as explained in the API help, the API is password protected by a “Basic Auth”. This takes care of it, by providing your Twitter username and password to the Basic Auth required by the API.--data status="Having fun with cURL": this is the data that will be sent to the API, actually the new status, which will be wrapped in a POST HTTP request.http://twitter.com/statuses/update.xml: last but not least, this is the URL of the API
Scripting a direct message
This requires a little bit more evolved trick. Indeed, Twitter’s API doesn’t provide anything to send a direct message.
To overcome this lack of feature, we are going to use cURL to do this through the web forms, just as if we would do it by hand :
curl --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 --data 'username_or_email=username' --data 'password=password' --data 'commit=Sign In' http://twitter.com/login
This piece performs the login / authentication :
--cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt: we are reading / storing the cookies from the file /tmp/cookies.txt. Right now this file is empty, but when the authentication will have been done, it will contain the session cookie which will let us keep authenticated at the next request--user-agent Mozilla/4.0: curl will pretend it is Mozilla/4.0.--data[...]: the data to be sent, actually the login and passwordhttp://twitter.com/login: the URL of the login form
This will log us in, and let us send the direct message with the following command :
curl --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --user-agent Mozilla/4.0 --data "text=little message to send" --data 'commit=Send' http://twitter.com/direct_messages/create/123456
There is not much more to explain with this command. The URL which is used here is the one you can get by going on Twitter, then clicking on the friend you want to send a direct message to, and then clicking on “message” on the right hand sidebar.
That’s it, you’ve sent your Twitter direct message from the command line with cURL ! Of course you could wrap up those two lines in a shell script and make it more user friendly …
Worth reading :
- Twitter : Send free SMS messages to your friends : now you can do this from the command line
- Scripting Twitter with cURL
- Scripting Twitter with Perl + LWP : a script in Perl doing the same thing (much simpler).
- Perl+Twitter : Getting @mentions from command line


















March 18th, 2007 at 19:48
[...] little follow up on yesterday article about scripting Twitter with cURL: the solution was working, but somehow inconvenient. Not practical to have to go and fetch the user [...]
March 19th, 2007 at 21:28
[...] out it’s this easy. And yes, I can’t take any credit for discovering it – I shamelessly stole the idea from Stéphane Kattoor. If you read Stéphane’s post, you will see that the relative ease of posting regular [...]
March 19th, 2007 at 21:29
Thanks for posting this. I didn’t realize that posting updates was this easy. I hacked up a small shell script so that I can easily update from the command line.
March 21st, 2007 at 8:39
[...] Scripting Twitter with Curl [...]
March 31st, 2007 at 8:19
[...] Script Twitter with cURL [...]
April 12th, 2007 at 10:45
Does anybody know how to make it work with Unicode characters? I can’t seem to send them through (the status gets truncated), and usual escaping doesn’t seem to work (ampersand-hash, percent-u, or backslash-u).
June 9th, 2007 at 12:07
Hi,
nice suggestions. It was from this post that I got the idea to make a weather-bot for Lisbon, Portugal.
http://twitter.com/weatherlisbon
I describe it in this blog:
http://webtopmania.blogspot.com/2007/06/twitter-weatherlisbon.html
Cheers,
Guillaume
July 10th, 2007 at 21:34
[...] on a combination of Mattias Wikistrom’s Apt Update Script and a Tech@Sakana article called “Scripting Twitter with cURL.” Posted by Macskeeball Filed in [...]
November 11th, 2007 at 14:40
[...] read more | digg story [...]
January 24th, 2008 at 19:18
Hi
There is now a better API call for sending direct messages. Have a look at this http://www.rossgoodman.com/2008/01/21/my-first-twitterbot/ for my implementation.
Ross
http://www.RossGoodman.com
March 20th, 2008 at 3:25
[...] original to me, I stole this idea, too. (via this post, which was in one of Jessamyn’s [...]
March 22nd, 2008 at 16:32
[...] is easy using the Twitter API through curl. I just wrote the following bash script to update a twitter feed for a specified [...]
May 9th, 2008 at 19:18
[...] Scripting Twitter with Curl [...]
June 25th, 2008 at 8:54
Is there a way to use the Curl system to change the default “from web” at the end of your tweets. That would be hip.
June 26th, 2008 at 11:41
Jeremy,
I’m afraid not, since this information is added by Twitter according to the source of the tweets …
Just curious, what would you have wanted to have written instead ?
Stephane
September 20th, 2008 at 19:56
[...] Scripting Twitter with Curl [...]
September 22nd, 2008 at 18:59
The source is just another query param that you pass in.
source=foo&status=message+here
Useful example:
http://my.opera.com/edvakf/blog/2008/07/24/update-twitter-status-from-opera-address-bar
October 5th, 2008 at 17:12
ah HA ! Thanks for the info fearphage, that’s cool to know !
Stéphane
November 14th, 2008 at 0:40
[...] written back in April as a hackday idea. The code posts Twitter updates using a variation on the twitter-from-curl approach of HTTP POSTing “status=MyTweet” to [...]
November 24th, 2008 at 21:25
Seems that the process of logging in via curl has changed to:
u=myname;
p=mypass;
c=~/.twitter-cookies.txt;
curl –cookie $c –cookie-jar $c \
–user-agent Mozilla/4.0 \
–data “session[username_or_email]=$u” \
–data “session[password]=$p” \
–data ‘commit=Sign In’ \
http://twitter.com/sessions
This is why Official APIs are important
January 20th, 2009 at 18:35
[...] Scripting Twitter with cURL - Tech@Sakana – A sysadmin’s blog Cómo usar la herramienta <tt>curl</tt> (Catch URL, obtener URL en inglés) para pasar datos a Twitter y realizar diferentes operaciones. Lo estoy usando para mandar actualizaciones a Tweeter sobre el ritmo de la tesis… (tags: programming tutorial bash cURL howto scripting web_services Twitter) [...]
March 3rd, 2009 at 4:30
[...] Scripting Twitter with cURL cURL is a versatile command-line utility designed to script Web page interactions. As a little demo, this tutorial shows you how to use it to easily to overcome the shortcomings of Twitter’s API. You can also view the Perl + LWP version of this tutorial here: Scripting Twitter with Perl + LWP [...]
March 3rd, 2009 at 14:53
[...] Scripting Twitter with cURL cURL is a versatile command-line utility designed to script Web page interactions. As a little demo, this tutorial shows you how to use it to easily to overcome the shortcomings of Twitter’s API. You can also view the Perl + LWP version of this tutorial here: Scripting Twitter with Perl + LWP [...]
March 8th, 2009 at 0:34
Hmmm…I’m not able to get this guy working, even with RichardBronsky’s suggestions.
March 29th, 2009 at 22:10
[...] Scripting Twitter via Curl via Sakana.fr [...]
March 30th, 2009 at 16:10
Yet another use for my old trusty pal curl.
I’m gonna give this a try, Thanks.
May 4th, 2009 at 3:06
[...] Scripting Twitter with cURL cURL is a varied command-line programme fashioned to playscript Web tender interactions. As a lowercase demo, this tutorial shows you how to ingest it to easily to overcome the shortcomings of Twitter’s API. You crapper also analyse the Perl + LWP edition of this tutorial here: Scripting Twitter with Perl + LWP [...]
May 27th, 2009 at 4:47
thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.
June 11th, 2009 at 20:21
[...] based on a combination of Mattias Wikistrom’s Apt Update Script and a Sakana article called “Scripting Twitter with cURL.” [...]
August 9th, 2009 at 7:36
Hi,
if you can help
I am looking for any code that can make a simple search to twitter and get an answer
Thank You!
David
August 21st, 2009 at 21:23
[...] ?????? ??? ??? ???? ???? ??? ????, ??? ????? ?? ????? ??????, ??????? ???? ??? ??? ???? ??????, ?????? ?? ?????? ????? ??????. ?? ???? ?? ??? ????? ????? ???? ????? ???????? ?? ????? ????, ?? ?? ???? ???. (???? ?????? ????) [...]
August 22nd, 2009 at 22:43
It appears that the …&source=foo option of status update has disappeared, unless someone knows the trick. I see if you do not add source it identifies the course as API but if you add a source=foo you end up with the result of “… from web”.
Any insight or pointers are greatly appreciated.
Thanks,
Bill
September 7th, 2009 at 20:49
[...] Scripting Twitter with Curl [...]
September 21st, 2009 at 20:07
For all who have problems with some special symbols – try to use unicode and URL encode.
PHP Functions such as iconv and urlencode with do a good job for you!
September 22nd, 2009 at 9:03
Sir, could you please tell me how to append the source parameter (which the twitter uses to fill ‘from’ field ) using curl ?
Thanks
September 25th, 2009 at 0:12
seems source= always return as from web, any idea how to resolve it ?
November 6th, 2009 at 7:08
How can i tweet more than 140 characters with your curl command as explained??
November 8th, 2009 at 18:59
Hello Shyam,
As far as I can tell, you can’t tweet more than 140 characters … that’s the very concept of twitter
Of course, you can still split a too long message into 140 characters chunks.
Regards,
Stéphane
November 10th, 2009 at 22:18
[...] Scripting Twitter With cURL – Tech@Sakana [...]
November 13th, 2009 at 7:52
Well thx for this info, I’m gearing up to make first twitter app. However my experience out of the classroom is very limited so.. hell even my classroom is limited.. but I’m going to give this programming a real shot
Anthony V. Gibby
Get paid just for using Twitter
GibbyDevelopments.com
I’m on Twitter Also
January 4th, 2010 at 19:14
Hi,
I’m a windows programming and this is pretty interesting? Can I ask how you figure out what commands to send over to twitter?
The login command on the post doesnt work, curl spits out a login page.
Also, this –> u=username;p=pass;c=~/.twitter-cookies.txt;curl –cookie $c –cookie-jar $c \–user-agent Mozilla/4.0 \–data “session[username_or_email]=$thegiddygeek†\–data “session[password]=$pass†\–data ‘commit=Sign In’ \http://twitter.com/sessions
Doesn’t seem to be working either? It says “u is not recognized”
Thanks
Gideon
March 6th, 2010 at 12:30
[...] Scripting Twitter with cURL [...]
June 14th, 2010 at 4:15
[...] Scripting Twitter with cURL cURL is a versatile command-line utility designed to script Web page interactions. As a little demo, this tutorial shows you how to use it to easily to overcome the shortcomings of Twitter’s API. You can also view the Perl + LWP version of this tutorial here: Scripting Twitter with Perl + LWP [...]
June 18th, 2010 at 5:28
[...] Wichtig: Mit diesem Befehl kann man nur seinen Status veraendern. Moechte man Direktnachrichten ueber twitter versenden ist etwas mehr als nur dieser Einzeiler noetig. [...]
July 7th, 2010 at 6:02
awesome post, i found all the info i needed right here
July 12th, 2010 at 15:52
well this command is working fine if i run through command prompt.But How to execute this command through PHP code.