In this tutorial, we will be learning how we can use bitly url Api service to create shortened links using python 3 programmatically.

Let’s talk about Bitly before we get into the coding part. Bitly is a link management platform that lets you harness the power of your links by shortening, sharing, managing, and analyzing links to your content. Although there are many services, it is widely known for its url shortening service where you can have a very long URL which can be unsightly, to copy manually and difficult to communicate. The analytics capabilities are also a very valuable feature as well.

How Bitly works?

  • Copy the link you want to shorten.
  • Visit bitly homepage
  • Scroll down and you find an input box to paste the link
  • And the click on Shorten to shorten the link

So instead of going to visit Bitly every time you want to shorten the urls we will be using Bitly Api and python to save some time get the shortened url or multiple urls in just a click. All we have to do is give input URL and our code will generate the Short URL using bitly API. We will go through two methods to perform this task.

Method 1: Using bitly_api package

Step 1: Create a bitly account

Visit bitly and sign up for a new account in case you don’t have one.

Step 2: Get the access token

  • Click on your profile.
  • Open profile settings.
  • Then click on the generic access token.
  • Enter your password and you will get an access token.

Step 3: Install bitly_api library

Click on this link and download the repository from GitHub. Once the download is complete, you can extract the zip file. After extraction open your terminal/cmd prompt and navigate to the bitly-api-python-master folder.

cd bitly-api-python-master

After you navigate to the folder, run the following command

python install

And the installation process is complete.

Step 4: Sample code for bitly_api package

Here is the code:

import bitly_api  
BITLY_ACCESS_TOKEN ="YOUR_ACCESS_TOKEN" access = bitly_api.Connection(access_token = BITLY_ACCESS_TOKEN)full_link = input("Enter a link") short_url = access.shorten(full_link)  print('Short URL = ',short_url['url']) 

Like most 3rd party packages, you will need to start with importing bitly_api in your code.

Next, we have keep to have a place to keep the API keys that were setup under your bitly accountwe will declare a variable BITLY_ACCESS_TOKEN and store the access token that we copied earlier. And then call the connection function with the access token in it.

We will then create another variable named accessand call the bitly_api.Connection(access_token = BITLY_ACCESS_TOKEN)to establish a connection with bitly_api.

Next, we will be taking input url that we need to shorten and store into full_link variable.

This is where the magic happens access.shorten(full_link) we will pass the url into shorten(full_link) which will shorten our url and save it into short_url varable.

let’s print our shortened url using print('Short URL = ',short_url['url']) funtion.

But why [‘url’] ?

because the short_url stores some additional data that we don’t need so we will passing [‘url’] to get our shortened url.

And now our url shortener is ready to use !!!


Enter a URL to short
Shortened URL :

Method 2: Using bitlyshortener package

Using bitlyshortener

bitlyshortener is a python package created by impredicative that helps us to achieve the same results but with a more easy and simple setup. Just run the following command in your cmd/terminal to install the bitlyshortener package.

pip install bitlyshortener

Once you have installed the package successfully we can move on to the coding part.

Instead of executing the program for every single url why don’t we try and write a program that could take multiple and urls as input and convert all urls into shortened urls at once. interesting right?

This is the code:

import bitlyshortener

access_tokens = ["YOUR_ACCESS_TOKEN"] 
shortener = bitlyshortener.Shortener(tokens=access_tokens)
n=int(input( "NO. of urls want shorten?"))

for i in range(n):
    li=input("Enter url")

long_urls= shortener.shorten_urls(long_urls)
print('Shortened url :',long_urls)

Firstly, import bitlyshortener then create a variable to store your access token. also, you can add multiple access tokens.

Use a variable shortener that will pass our tokens and create a connection.

Then, create a variablen that will store the count of urls to shorten.

create an empty list long_urls=[] which we will be using to store the links. taken from the user.

Now, to store these urls into long_urls=[] we will use the for loop.

for i in range(n):

li=input("Enter url") variable li is used to take input and the .append() method to store the li into list.

Once we append links in long_urls we will be calling shortener.shorten_urls(long_urls) function to shorten the url in bulk and store it back to the long_urls list.

And once the for loop is ended. we will print the list which has all the shortened links.

Output :

How many links you want shorten? 2
Enter url:
Enter url:
Shortened url : ['', '']


That’s it. In this article, we saw two methods to generate short links using bitly_api and python. in the first method, we created a program to accept a single link and generate the shorten link and in second method we created a program that will take ‘n’ number of links as input and generate the shorten link at once which also saves time and hustle to keep pasting every single link and generating the shorten link.

Get Notified Automatically Of New Articles

Error SendFox Connection: 403 Forbidden

403 Forbidden