My favorites | Sign in
Logo
                
Search
for
Updated Oct 13, 2009 by lehrburger
Labels: Featured
ApiDocumentation  
Documentation of the bit.ly REST API methods

See also the JavascriptClientApiDocumentation

Sections

Information

The bit.ly API allows developers to interact in a programmatic way with the bit.ly website. The current API version is 2.0.1


Authentication and Shared Parameters

All APIs require authentication credentials supplied as query arguments:

login=login&apiKey=apiKey

All APIs require a version identifier to be present:

version=2.0.1

All APIs support an optional return format specifier (json is the default response format. xml is also available):

format=json

All APIs support an optional callback specifier for use with json return format:

callback=callback


Rate Limiting

bit.ly currently limits API users to no more than five concurrent connections from a single IP address. In general, if you intend to be an extremely high-volume user of the api, please contact us at support@bit.ly to discuss your options.



REST API

/shorten

Given a long URL, /shorten encodes it as a shorter one and returns it.

Additional Parameters

To shorten multiple URLs in a single API call, just post as many longUrl parameters.
Note: Long URLs should be URL-escaped.

Examples


/expand

Given a bit.ly URL or hash, /expand decodes it back into a long source URL and returns it.

Additional Parameters

Either shortUrl or hash must be given as a parameter.

Examples


/info

Given a bit.ly URL or hash, return information about that page, such as the long source URL, associated usernames, and other information.

Additional Parameters

Either shortUrl or hash must be given as a parameter.

Examples


/stats

Given a bit.ly URL or hash, return traffic and referrer data.

Additional Parameters

Either shortUrl or hash must be given as a parameter.

Examples


/errors

Get a list of bit.ly API error codes.

Examples



Discussion

Please visit our Google Group for discussion of projects and technical issues.

http://groups.google.com/group/bitly-api


Comment by sorensenjens2000, Jan 08, 2009

Great API. I was wondering if you could create an interesting dashboard as a gadget for iGoogle if you used www.youcalc.com?, just like this one that provides an {http://www.youcalc.com/apps/1218724763465 interactive dashboard] on ebay statistics feed.

Comment by sorensenjens2000, Jan 08, 2009

Sorry my wiki skills are a bit rusty, the post should read: Great API. I was wondering if you could create an interesting dashboard as a gadget for iGoogle if you used youcalc, just like this one that provides an interactive dashboard on ebay statistics feed.

Comment by davidjanes, Jan 22, 2009
$ curl --location http://api.bit.ly/recent --include
HTTP/1.1 200 OK
Server: nginx/0.6.32
Date: Thu, 22 Jan 2009 09:05:40 GMT
Content-Type: text/html;charset=utf-8
Connection: keep-alive
Content-Length: 119
Allow: GET, HEAD, POST

{
    "errorCode": 203, 
    "errorMessage": "You must be authenticated to access recent", 
    "statusCode": "ERROR"

}

Hmmm ... if Basic Authentication is required, should this not be returning "401 UNAUTHORIZED"?

Comment by sleepbotzz, Jan 30, 2009

i'm having the same experience. besides the fact that 'recent' (un-documented but referenced as an example above) isn't supported:

{ "errorCode": 202, "errorMessage": "Undefined method recent", "statusCode": "ERROR" }

and as @davidjanes states, no HTTP Auth support seems to be in play here

Comment by jacinto.m.shy, Jan 30, 2009

Has anyone else noticed that URLs shortened through the API do not show up in the user's history when their API key is used?

Comment by billrob458, Feb 09, 2009

I have noticed it isn't showing in the history either. I found this thread so it appears there is a history parameter you can send.

http://groups.google.com/group/bitly-api/browse_thread/thread/d35fa7c5e1a764f9/3224b7104234b362?lnk=gst&q=history#3224b7104234b362

But don't know what the history parameter is.

Comment by maximz.fw, Feb 21, 2009

How do you customize the shortened url alias? I believe it's called "shortened custom name" or something like that. Hash is not the parameter for this, so is there a way to create a custom shortened url with this API?

Comment by dydimustk, Feb 24, 2009

Is there a parameter set optional custom name?

Comment by wisniewskit, Feb 24, 2009

Is it a bug that the xml response format will return the original url (in nodeKey) within a CDATA section, but also escape the ampersands into HTML entities (&)? Wouldn't it make more sense to not escape the data inside of a CDATA section? For instance, try the following link and see what's in the CDATA section:

http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl=http://test.com?a=1%26b=2&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07

Comment by neilco, Mar 03, 2009

For those looking to shorten to a custom keyword, the parameter is: keyword. Set the value to the keyword you desire.

Comment by personalhypequotient, Mar 31, 2009

What are the legal characters in a keyword? More specifically, how can I construct a regex to detect a bitly link in a body of text?

Comment by marun2, Apr 01, 2009

Correction in wording: "To shorten multiple URLs in a single API call, just post 2 longUrl parameters. " should read : To shorten multiple URLs with single API call, use as many longUrl parameters.

Comment by eckman.john, Apr 06, 2009

Does history=1 have to come last in the parameters list?

I'm passing it as part of a script that generates bit.ly urls, but the ones shortened via the API are not showing the user's history

Comment by grachyov, Apr 11, 2009

Very interesting: "login" parameter is case sensitive. I.e. "login=ABC" doesn't work, but "login=abc" OK.

Comment by clarionmax3dev, Apr 12, 2009

currenly twitter show short url after that the custom messages. How should I change this.. I would like to show custom message and then twitter short URL?

Comment by smartocci, Apr 13, 2009

Can I get an aggregate hash from a users hash over the api? I don't see that anywhere. I am trying to get total aggregate clicks from a users hash

Comment by vinayak.joshi, Apr 14, 2009

Hi

What is the method of obtaining an API key. I want all URL's shortened through the API to be available in my account. Is that possible?

Comment by vaibhavgupta28, Apr 15, 2009

If I json_encode the output of shorten method, I get url as a key. But the key contains invalid object characters like : or / cant we avoid it?

Output I see

{ "errorCode": 0, "errorMessage": "", "results": { "http://www.yahoo.com": { "hash": "4bYAV2", "shortKeywordUrl": "", "shortUrl": "http://bit.ly/PGr84", "userHash": "PGr84" } }, "statusCode": "OK" }

JSON Decode output

stdClass Object ( [errorCode] => 0 [errorMessage] => [results] => stdClass Object ( [*http://www.yahoo.com*] => stdClass Object ( [hash] => 4bYAV2 [shortKeywordUrl] => [shortUrl] => http://bit.ly/PGr84 [userHash] => PGr84 ) ) [statusCode] => OK ) 
Comment by smartocci, Apr 20, 2009
Comment by brian.shiro, Apr 25, 2009

I'm having trouble getting the API shorten call to work with long URLs that have multiple query arguments of their own. Surrounding them in quotes doesn't seem to work. It thinks the long URL ends at the & so the second argument (id in this case) isn't being read. e.g.:

http://www.prh.noaa.gov/ptwc/?region=2&id=hawaii.2009.04.22.030225

Also, is there an option to have it only output the short URL and nothing else? (The tinyurl API does this by default.) I'm calling this from a shell script and prefer not to have to parse the long JSON or XML output.

Any ideas? Thanks.

Comment by nziarek, Apr 26, 2009

Interested if the stats functionality will ever include daily / hourly clicks? If not, is it acceptable to poll hourly and store the results on an external server to simulate this functionality?

Comment by henkisdabro, Apr 29, 2009

I have a question regarding the FireFox? Extension UrlbarExt?. It uses the bit.ly api to shorten urls through a URL bar shortcut button. How can I incorporate using my bit.ly login account when shortening to be able to get proper stats etc.?

It has the following options available to fill in (I typed in the current settings as well):

API: http://bit.ly/api Arguments: url=%?url% Post: On or Off Filter: Prefix: Suffix:

How shall I fill these in to make it use my login details for bit.ly?

Comment by pjkixx, May 04, 2009

when using the api i don't see the links showing up in the recent section. i sometimes see these show up under "Top Bits" ... is there another place to keep track these links and get stats that are under the same api key?

Comment by twittermoo, May 06, 2009

It is possible to expand multiple URLs with single API call? Multiple shortUrl or hash parameters not working. How to?

Comment by alexlinebrink, May 06, 2009

I know I can limit the "shorten" call to returning ONLY the shortened URL by including "&format=text".

Is there a way to limit the "stats" call to returning ONLY total clicks?

Comment by emailfire, May 14, 2009

URLs shortened via the API are not added to your history unless you append history=1 to your API call.

Comment by pronux.switzerland, May 14, 2009

Is it allowed to poll the server hourly and store the results on an external server to make some simulations with the statistical data ?

Comment by northwestdev, May 15, 2009

How do I get an API key? and is there better documentation on how to use the API?

Comment by sent...@hindu.org, May 19, 2009

I, too, am interested in getting an API key so I can post from twitterfeed. How do I get one? I have a bit.ly account, but it is not evident.

Comment by ncwild...@gmail.com, May 20, 2009

@sent...@hindu.org You can get the API key from your accounts page. http://bit.ly/account/

Comment by ncwild...@gmail.com, May 22, 2009

I agree with twittermoo, you should be allowed to make one API call for multiple shortUrls or hashes with expand.

Comment by jcastelain, Jun 01, 2009

i'm working on an actionscript3 port of this, any idea how i can share?

Comment by tysonk, Jun 01, 2009

I'm noticing a minor bug when using the API. If I try to shorten a URL for a domain and I forget the trailing slash, bit.ly adds the slash to the end of the URL automatically. This would be fine, except in situations like this:

URL to shorten: http://www.mysite.com?track=yes

Bit.ly returns: http://www.mysite.com?track=yes/ <- Slash added to end

Can you advise?

Comment by grischa.brockhaus, Jun 11, 2009

As far as I understand, each account (apikey) get's its own short url for a single link.

Is it possible to fetch all known short urls for one given long url?

Comment by MatteBlackArts, Jun 15, 2009

Is it possible to reset the URL for an existing Custom Name? I want to change the target URL for a widely distributed Custom Name. I can't be the only one who wants to do that.

Comment by Haewoon.Kwak, Jun 21, 2009

Great. I have one question about /stats. Do you have a plan to make it accessible the location of users who clicked the shorten URL? Those information are available through bit.ly web sites, but not through API.

Comment by juannavarroperez, Jun 22, 2009

Great API and great service from bit.ly! I wanted to ask if it is possible to have an API call to get the list of public urls shared by a particular user (maybe the logged in user). Thanks!

Comment by skabber, Jun 24, 2009

I am also interested in an api call that gets stats data for all of the logged in users urls.

Comment by niall.smart, Jun 25, 2009

Is a reverse search possible? i.e., list all bit.ly URLs for the long URL http://somesite.com/about.html

Comment by david.frechette, Jul 03, 2009

Does the custom keyword is broken ? I tried any keywords and it always return 'The custom keyword you tried to use was already used by someone else.'. Did I missed something ?

http://api.bit.ly/shorten?version=2.0.1&longUrl=http://www.cnn.com&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&keyword=485fht653

Here the result : { "errorCode": 0, "errorMessage": "", "results": { "http://www.cnn.com": { "errorCode": 1212, "errorMessage": "The custom keyword you tried to use was already used by someone else.", "hash": "2EEjBl", "shortKeywordUrl": "", "shortUrl": "http://bit.ly/2iyjJb", "statusCode": "ERROR", "userHash": "2iyjJb" } }, "statusCode": "OK" }

Comment by philnash, Jul 07, 2009

David, I just emailed bit.ly support and they said that custom keywords were always an unsupported part of the API and have now been removed permanently.

Guess we have to stop using that part of the call now.

Comment by BjoernGT, Jul 13, 2009

So it is encouraged to publish one's API key on a web site? Because if I use it from JavaScript? (for example with the Bitly client), everybody can just do "show source" and see my API key.

What's more, since history is not automatically logged(?), I won't even notice if somebody else uses my key.

Not that it necessarily matters, but I don't quite see the point of the API key if it is not a secret anyway.

Comment by gautam.2011, Jul 14, 2009

Want to ask the same question that is asked above. Is it any harm to dislay the API key in public?

Comment by josuhr, Jul 16, 2009

I was just about to add bit.ly support to Gravity (my S60 Twitter Client). Could you add an API call that's returning the URLs and hashes and stats for a specific (authorized) user? That would be cool for adding bit.ly to a mobile client.

Cheers Ole (ole at mobileways.de)

Comment by BjoernGT, Jul 17, 2009

Just realized that I am creating a lot of bit.ly URLs just to get the information from /stats about a URL. Would be easier if you added a lonUrl parameter to the stats method, so that it could be accessed without a bitly hash for the URL.

Comment by mcerabona, Jul 21, 2009

has anyone seen any samples code? I have the API working on the post, but I can not figure out how to get the shortend URL back into my form.

Comment by ad.teixera, Jul 27, 2009

Hi there,

The api chops off everything which is folloing an '&' in an URL, so the bit.ly link is getting useless. How to escape the '&" chars in the URL ?

brian.shiro, asked in Apr 25, 2009 the same.

Any ideas? Thanks.

Comment by Daniel.Gladwell, Jul 27, 2009

@henkisdabro - I have the same question. I've tried a ton of different options for inputting everything, but not luck yet. Can anyone else help?

RE: UrlbarExt??. It uses the bit.ly api to shorten urls through a URL bar shortcut button. How can I incorporate using my bit.ly login account when shortening to be able to get proper stats etc.?

It has the following options available to fill in (I typed in the current settings as well):

API: http://bit.ly/api Arguments: url=%?url% Post: On or Off Filter: Prefix: Suffix:

How shall I fill these in to make it use my login details for bit.ly?

Comment by ad.teixera, Jul 27, 2009
Comment by t...@crsolutions.be, Jul 27, 2009

Hello,

I wrote a PHP5-wrapper-class that implements all API-functions. Maybe it can be usefull for people that have to generate bit.ly-urls in their next Web 2.0-app.

http://blog.verkoyen.eu/blog/p/detail/bit-ly-php-wrapper-class

Comment by Bob.SDFRE, Jul 28, 2009

Hi, I screwed up and did not escape the & when I created my short url USING KEYWORDS. Now, I want to keep my keyword phrase, but I need to correct the underlying long link. How do I do that?

Comment by pras.sarkar, Aug 04, 2009

Is there a way through the API to get all Bit.ly hashes (short URLs) for a given long URL?

Comment by sat...@ebanyan.net, Aug 09, 2009

How can I delete a short URL from my account. When I am using googlespreadsheet, I keep modifying various values (campaigns, mediums, etc.,). As I keep modifying those, the api keeps on creating short urls.

There should be a mechanism for me to delete some of the short urls created by me. -Satish

Comment by spcghst440, Aug 11, 2009

I would love to see a bookmarklet for posting the current web page straight to twitter.

Comment by rob.barry, Aug 11, 2009

Is there any way to get the date the URL was submitted for shortening?

Comment by mikawber25, Aug 20, 2009

Is there a way to verify the API Key? I've looked around and haven't found anything!

Comment by m...@lenwo.de, Aug 29, 2009

Sorry, but I don't get the Authentication

What to use for:

login=login&apiKey=apiKey ?

Thanks in Advance!

Comment by raim...@rkuipers.nl, Aug 30, 2009

I also don't get the authentication. What is the problem?

Comment by jmsche, Aug 31, 2009

For login, use your username. The apiKey is given in account settings.

Comment by webridges, Sep 02, 2009
Comment by garionw, Sep 04, 2009

Am I able to get a list of the links I've published with Bit.ly (the history). It would make integration with my site much much easier.

Comment by ivantis3, Sep 09, 2009

When will there be API support for j.mp?

Comment by raim...@rkuipers.nl, Sep 13, 2009

Although i use the correct login and apikey, i still get the error you're not authorized.... Help!

Comment by serban.constantin, Sep 15, 2009

@ivantis3, from what I saw you can already use http://api.j.mp/shorten[rest_of_default_code] to get a j.mp shortened link.

Comment by jparicka, Sep 19, 2009

Is it possible to perform search? I cannot seem to be able to figure it out (neither for tweetmeme). Thanks! @jparicka

Comment by mcored, Sep 26, 2009

Hi, when will the API be updated for posting image data and getting a bit.ly or j.mp URL in one go. Thanks.

Comment by roberto....@gmail.com, Sep 29, 2009

I need to know the difference between

"userHash" and "hash"

in the XML return. Anyone? both link to my URL.

Comment by vrillusions, Oct 02, 2009

Since we're passing around login information, is there any chance we can get an ssl version at https://api.bit.ly/ ?

Comment by devicbojan, Oct 03, 2009

Is there any limitation of using API, some TOS for API usage.

Comment by utpalkrdeka, Oct 05, 2009

I used this feature in my site (http://goseedo.net/index.php?menu=showevents&id=3512&country=United%20States&state=New%20York&city=New%20York) ; now this issue is : I had to block your script like this

<script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=goseedo&apiKey=R_0d6e8a03b9f992777da49690f29db963"> </script> <script type="text/javascript" src="js/shorterurl.js"></script> <script type="text/javascript">

// BitlyClient?.call('shorten', {'longUrl': "http://goseedo.net/index.php?menu=showevents&id=3512&country=United%20States&state=New%20York&city=New%20York"}, 'BitlyCB.shortenResponse');
</script>

As in IE anything underneath get invisible if I run the script.

Can anyone advice how to fix it?

Comment by lehrburger, Oct 07, 2009

@roberto.valerio When a logged-in user shortens a link, and this link has not been shortened before, both a user hash and a (global) hash are created that redirect to that same long link. All non-logged-in users will receive that same global hash for shortening that link, but the user hash is specific to that user. Hopefully that helps answer your question, and please feel free to email support@bit.ly with more questions.

Comment by sullof, Oct 09, 2009

@spcghst440 Try my Twitt.it (http://twitt.it)

Comment by mscice, Oct 15, 2009

after using curl i get alot of info back anyone know how to grep out only the shorten url?

Comment by jauderho, Oct 15, 2009

Still hoping to get longURL returned as part of the stats api call. This will remove the need for me to have to separately call info() multiple times.

Comment by joshlizard, Oct 16, 2009

Kudos on the api super easy to use :)

Comment by tab...@gmail.com, Oct 19, 2009

thanks for an awesome api! however my long url contains a get param "&image=23" even though i am url encoding using rawurlencode, when i try accessing the returned json object, the &image param seems to have been converted to the following character: ℑ which makes me unable to access the results stdClass for my long url. :( anyone have any ideas?

Comment by tab...@gmail.com, Oct 19, 2009

edit: using the php function get_object_vars seems to have sidestepped this issue.

Comment by frann.leach, Oct 21, 2009

Having got the information in some strange format or other, how does one pick out the bit one wants? I'm assuming someone has already done this, or is there a php function to do it? Would be useful if there was a name for this type of format. then I could find something online most likely

Comment by Justin.M.Beasley, Nov 02, 2009

I agree that we need a way to connect over SSL, because the calls fail if the user clicks the wrong option in IE's mixed SSL content warning (that is, if they're on a page using HTTPS/SSL).

I'd even settle for a way to pass this an alternate API URL so that we can proxy the real API through a mod_rewrite rule. That's what I'm doing right now to avoid the initial error, but once I actually call the API it still throws the warning asking the user if they want to allow the non-SSL content.

What's worse is that in IE 6-7, the correct answer is "yes". In IE 8, it's "no" (the questions were all rephrased so that the more secure answer is always "yes"). This makes it really easy for the user to make a mistake, and once they do there's no way to make the calls work on that page.

Comment by WebsiteCMS, Nov 04, 2009

ok, just found the answer. you need to encode the parameter if it's likely to have reserved characters, before you encode the rest of the options like so (in php): $topUrl = "http://www.twindowpane.com/".urlencode("trend.php?trend=".urlencode($yy));

Comment by d3vianted, Nov 04, 2009

If you are using the PEAR class Services_ShortURL you will face the "bug" where bit.ly does not retain your history once you have shortened a URL.

The fix is easy.

Open Bitly.php in the Services/ShortURL/ directory. Go to line 93 in the "shorten" function.

Add the parameter 'history' => '1', to it making it look like this:

$params = array(
'version' => '2.0.1', 'format' => 'xml', 'longUrl' => $url,
'history' => '1',
'login' => $this->options'login'?, 'apiKey' => $this->options'apiKey'?
);

And its fixed!

Hope this helps..

Comment by d3vianted, Nov 04, 2009

The question marks should be open and close brackets..

Comment by smurphster, Nov 04, 2009

for those asking about UrlBarExt?...

API: http://api.bit.ly/shorten arguments: version=2.0.1&format=xml&longUrl=%?url%&login=yourlogin&apiKey=yourapikey filter: <shortUrl>(.+)<\/shortUrl>

add &history=1 if you want it in your history

you could probably do it with JSON too but parsing XML was easier for me to think through at the time :)


Sign in to add a comment
Hosted by Google Code