Skip to content
Related Articles

Related Articles

Python | How to shorten long URLs using Bitly API

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 22 Oct, 2018

Bitly is used to shorten, brand, share, or retrieve data from links programmatically.

In this article, we’ll see how to shorten URLs using Bitly API. Below is a working example to shorten a URL using Bitly API.

Step #1: Install Bitly API using git
git clone

Go inside the folder using:
cd bitly-api-python

Note: In case installation with pip command is not working:

File "/usr/local/lib/python3.5/dist-packages/bitly_api/", line 1, in 
from bitly_api import Connection, BitlyError, Error
ImportError: cannot import name 'Connection'

Uninstall bitly-api in case already installed using pip:

pip uninstall bitly_api


Step #2: Install in Python’s folder for modules:

python install

Note: Delete the source code using (since we don’t need it for now) :

rmdir bitly-api-python-master

Step #3: To get “Bitly username and API Key” login to account on and then go here and get it. In case of further assistance, one can go here.

Step #4: Now create a file and write this code:

import bitly_api
API_USER = "your_username"
API_KEY = "your_API_Key"
b = bitly_api.Connection(API_USER, API_KEY)
# Replace this with your Long URL Here
longurl =    
response = b.shorten(uri = longurl)
# Now let us print the Bitly URL

Step #5: Run the file ‘’.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!