Before we start
Bitly allows users to shorten, share, and track links (URLs).
It’s a way to save, share and discover links on the web.
Bitly provides public API’s which are intended to make it even easier for python programmers to use.
First Step
The first step is to head over to dev.bitly.com where you will find the API documentation, best practices, code libraries, and public data sets.
What is an API Key?
Many services on the Internet (such as Twitter, Facebook..) requires that you have an “API Key”.
An application programming interface key (API key) is a code passed in by computer programs calling an API to identify the calling program, its developer, or its user to the Web site.
API keys are used to track and control how the API is being used, for example to prevent malicious use or abuse of the API.
The API key often acts as both a unique identifier and a secret token for authentication, and will generally have a set of access rights on the API associated with it.
Get the Bitly API Key
To be able for us to shorten links (see below) we’ve to sign up for an API key.
The signup procedure is pretty much explaining itself, so I’ll not cover that in this post.
Create your Bitly API Key here
Bitly Code Libraries
A number of developers have written code libraries to interact with the bitly API in several different languages. Since we’re programming in Python, we’re of course interested in the Python libraries.
There are currently three different libraries to choose, which you can find here
In this post we will use the “bitly-api-python” library, which is also the official Python client.
Bitly API Python
The installation of Bitly API is very easy.
# Installation using PIP pip install bitly_api Downloading/unpacking bitly-api Downloading bitly_api-0.2.tar.gz Running setup.py egg_info for package bitly-api Installing collected packages: bitly-api Running setup.py install for bitly-api Successfully installed bitly-api Cleaning up...
Shorten a URL
We want to write a script that will reduce the URL length to make the sharing easier. Open up your favourite text editor and put in the code below.
Save the file as shortener.py
#!/usr/bin/env python # Import the modules import bitlyapi import sys # Define your API information API_USER = "your_api_username" API_KEY = "your_api_key" b = bitlyapi.BitLy(API_USER, API_KEY) # Define how to use the program usage = """Usage: python shortener.py [url] e.g python shortener.py http://www.google.com""" if len(sys.argv) != 2: print usage sys.exit(0) longurl = sys.argv[1] response = b.shorten(longUrl=longurl) print response['url']
Shortener.py Explained
We started the program with #!/usr/bin/env python
#!/usr/bin/env python
Importing the modules that we’re going to use in our program
import bitlyapi import sys
Defining our API information
API_USER = "your_api_username" API_KEY = "your_api_key" b = bitlyapi.BitLy(API_USER, API_KEY)
Defining how to use the the program
usage = """Usage: python shortener.py [url] e.g python shortener.py http://www.google.com""" if len(sys.argv) != 2: print usage sys.exit(0)
Creates a variable longurl and sets the value to the argument passed in
longurl = sys.argv[1]
Gives the Bitly API the longurlresponse = b.shorten(longUrl=longurl)
Prints out the the URL value
print response['url']