TinyURL JavaScript API

I’ve been writing a bookmarklet for Twitter that will allow you to post URLs to Twitter, that are first compressed using TinyURL.

So I thought I’d share the TinyURL callback API I wrote.

The API allows the dynamic creation of TinyURLs on the fly using JavaScript.

The API url is: http://remysharp.com/tinyurlapi

Here’s the source code for your own hosting: http://remysharp.com/downloads/tinyurlapi.php

There are full instructions and examples on the page along with a demonstration.

Note that since creating the script, TinyURL have changed their page so we can’t create tinyurls on the fly (though we can still look up), so I’ve plugged in is.gk instead for creation.

28 Responses to “TinyURL JavaScript API”

  1. it works but it has to go through your site rather then test.com, you need to provide the backend php script in order for other sites to really use it.

  2. @Mongoose – sorry, I didn’t make that clear. The guts of the API is a little server side script that I’m hosting to create a new tinyurl. I’ve changed the example script to point to my real domain (rather than test.com).

    If you want, I can provide the actual script and you can host it yourself if that’s more valuable.

  3. Thanks Remy
    This is cool! I really think tinyurl is a great service and twitter is making it even more popular. Such APIs are indeed quite helpful. Using your API is there a way to do a reverse lookup?

  4. @Akshay – I’ve tweaked the TinyURL API to also support reverse lookups. If you enter a tinyurl instead of a normal URL, it will workout where the URL is pointing to.

    e.g. http://remysharp.com/tinyurlapi?url=http://tinyurl.com/yrgztp

  5. I’ve also upgraded the api to handle ‘RESTful’ type requests, e.g.


    and visa versa:


  6. You can use the official API too, for example:


    You can send the URL by GET (as the example) or by POST

  7. @pedro – of course – this isn’t meant to replace the service, but to allow developers to pull in tinyurls on the fly within JavaScript driven web apps.

  8. really nice api!
    with using your api, I write a greasemonkey script for twitter (and any web sites) that shows the original URL in tooltip.


    thanks again for your good job!

  9. @Jimbo – thanks for the script – I had spotted it was being used as greasemonkey script from my logs but couldn’t find it on the Internet.

    I’m going to have a think about upgrading the API to handle multiple URLs to reduce the number of calls. Comma separated URLs and the response can be an array of the resolved URLs in the same order requested.

    Do you think your script will benefit from this? I guess the only problem may be if you request one URL it will return as a string – so you’ll have to sniff the object type in the callback so the API is backward compatible.

  10. @Remy – I’m ready to change my script, althought it won’t be affected by the upgrading. (I don’t use the calback function correctly :P)

  11. Remy, It is interesting. How are you doing reverse lookups? Are you sending any HEAD socket queries or fopen+regexp?


  12. @Arun – pretty easy – I use the preview.tinyurl.com path and do a simple bit of DOM querying to find the value of the reverse lookup.

    I could have used a HEAD query, but the way I wrote the code, the forward and reverse lookup use the same code.

  13. I wrote a small snippet of PHP today to resolve tinyurl.

  14. How could I modify the code to remove the bullet point from the beginning of each twitter?

  15. @William – did you mean to post this comment on the TinyURL API post, or did you mean to do it on the Add twitter to your blog post?

    Either way, it’s best done using CSS:

    UL { list-style: none; }
  16. Hi,

    Here’s a web service for resolving TinyURLs, based on Dapper:


    You can use it as a browser button (bookmarklet) or a button your page. It will convert all TinyURLs on the current page into their full version.


  17. [...] (11/14/07): So after looking over a few pages, and the inexplicable problems I had with the JSON api, I’ve updated the script to use the [...]

  18. The TinyURL API cuts off any URL variables I have that begin with “&”. For example, in the following URL:


    “&source=twitter” gets cut off when I attempt to use the URL with the API. Any suggestions?

  19. Did you ever post the server script? Your javascript sample is no longer functional. It only seems to be returning null.

  20. @Daniel – crap, the Tinyurl monkey has been set loose. I’ll round him up and set him back to work in no time.

  21. Hey would it be possible to have this put onto my website?
    I run a little game you see and users are cheating by sending out tinyURL links
    And the other members are clicking them and coursing chaos.

    Thanks, Jamie.

  22. Hay Remy, nice job, thanks for this. You mentioned that we could get the code to host on our own site – is that possible? If so, could you please tell me where I can download it? Great stuff though, very useful – thanks ;)

    btw I am not the same Jamie as above! Just noticed that – hi Jamie ;)


  23. @Jamie + Jamie – yep – I’ve added a link to the source code in the original post above. Cheers.

  24. Thanks and good job!

    I writed about your api in my blog and I’m using it on feedky.com

  25. @Remy @jimbo


    As an ISP in Spain has blocked TinyURL (it seems that ISP has added them to their own ‘axis of evil’), I edited the script for modify not only the title, but also the href, the link itself.

    I haved posted the script here:

    Thanks for your job!

  26. This is amazing. Ingenious to parse the DOM of the preview page.

    I’ve incorporated your code into my site, which fetches and displays my Twitter tweets… now that I can do the reverse tinyurl lookup, I can parse youtube links that I’ve posted and embed them in the page.

    Thank you!

  27. I ran a quick test and it seems that Arun’s method above of using an fsock & reading the header is about 3x faster than parsing the DOM… also seems less prone to getting broken by cosmetic changes at tinyurl. His code needs a bit of restructuring to be error safe, but I would consider incorporating his method into your code.

  28. Major, above, had this issue:

    The TinyURL API cuts off any URL variables I have that begin with “&”. For example, in the following URL:


    “&source=twitter” gets cut off when I attempt to use the URL with the API. Any suggestions?

    I found that this happened when I used post instead of get in my curl request.

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight