RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Campaign Tracking with Bit.ly API integration

Learn how to integrate the Bit.ly API with your application to track clicks and automate analysis.


Although Bit.ly's URL shortening service is frequently used in conjunction with Twitter, its use is not limited to that. Bit.ly can be used to track any social network or e-mail marketing campaign, as it tracks information about the number of clicks and your audience. They also offer an API, so you can connect it with your application and automate the analysis process. Continue reading to learn how to integrate the Bit.ly API with your application.

API Overview

Bit.ly API uses two types of authentication. If you just want to shorten links on behalf of a single user or website, you can call the API shorten method using the generic OAuth token. On the other hand, if you are working with multiple users or want to access user-level data (Bit.ly history, analytics, etc.), you will need to register an app and use the usual OAuth authentication flow.

API responses are in JSON format by default. However, XML format is also available. When a request is successful, the HTTP status code will be 200. HTTP status codes that indicate errors are:

  • 403 — Rate limited
  • 503 — Service temporary unavailable
  • 404 — Page not found
  • 400 — Other invalid requests

A sample response would look like this:

// Successful response
{ "status_code": 200, "status_txt": "OK", "data" : ... }

// Unsuccessful response
{ "status_code": 403, "status_txt": "RATE_LIMIT_EXCEEDED", "data" : null }

The Bit.ly API has three endpoint URLs:

To make the integration easier, there are many Bit.ly API libraries for a variety of programming languages, that can be downloaded from here.

Link Shortening

As mentioned above, this is the simplest (but also very common) Bit.ly API use-case. It does not require the usual OAuth authentication flow, but just a generic OAuth access token, which can be created here.

Once you have done that, you will only have to request the following URL using the HTTP GET method:


Note that the https://api-ssl.bitly.com is the API URL, v3/shorten is the method that we are calling, and the parameters are passed as GET parameters. Also, the URL you are shortening should be encoded. A sample response would look like this:

               "status_code": 200, 
               "status_txt": "OK", 
               "data": { 
                                "long_url": "http:\/\/google.com\/", 
                                "url": "http:\/\/bit.ly\/1GBL67S", 
                                "hash": "1GBL67S",
                                "global_hash": "LmvF", 
                                "new_hash": 1

OAuth Authentication

OAuth authentication is a bit more difficult. It is used when you want to work with multiple users or access user-level data. After registering your app, the first step is to redirect a user to the following URL:


Client ID is obtained during the app registration, while the redirect URI is the location of the PHP script that will exchange a request token for an access token. So, once the user is redirected to the URL above, he will have to confirm that he allows your app to access his data. Once that is done, he will be redirected to the redirect URI. Bit.ly would send the request token (code) to this URI and then the request token will be exchanged for an access token. If everything goes fine, you can call API methods using the obtained access token.

The redirect URI PHP script would contain the code similar to this:

	// This is the request token (code) which will be exchanged for an access token
	$code = $_REQUEST['code'];
// Enter your Client ID, Client Secret and Redirect URI
	$client_id ="";
	$client_secret = "";
	$redirect_uri = "http://.../oauth.php"; // The redirect_uri is just pointing back to the location of this file
	// This is the access token endpoint
$uri = "https://api-ssl.bitly.com/oauth/access_token"; 
	// POST to the Bitly authentication endpoint
	$params = array();
    	$params['client_id'] = $client_id;
    	$params['client_secret'] = $client_secret;
    	$params['code'] = $code;
    	$params['redirect_uri'] = $redirect_uri;
	$output = "";
  	$params_string = "";
  	foreach ($params as $key=>$value) { $params_string .= $key.'='.$value.'&'; }
  	try {
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL, $uri);
		curl_setopt($ch,CURLOPT_POST, count($params));
		curl_setopt($ch,CURLOPT_POSTFIELDS, $params_string);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    	$output = curl_exec($ch);
catch (Exception $e) {


	// Returns a URL encoded string in the format of "access_token=%s&login=%s&apiKey=%s"
	// parse_str() creates variables $access_token and $login
// The access token for specified user
	echo $access_token;

	// The end-user's Bitly username
	echo $login;

	// Call API methods here

	// Store access token in session for later use

Next Steps

It is good practice to save the access token in a session, so you don't have to do the exchange each time you want to make an API call.

Also, read about other available API methods here. Calling them is the same as calling the v3/shorten method.

Vojislav is a web developer, designer and entrepreneur, based in Belgrade, Serbia. He has been working as a freelancer for more than 6 years, having completed more than 50 projects for clients from all over the worlds, specializing in designing and developing personal portfolios and e-commerce websites using Laravel PHP framework and WordPress content management system. Right now, he works as a full-time senior web developer in a company from Copenhagen.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date