URL Shortener in Python – A Beginner’s Guide

Shorten Url In Python

Want to create a URL Shortener in Python? Look no further. Navigating and sharing long URLs is a pain. The reason behind so long URLs is the number of trackers in a link, loaded up content (multiple directories) of heavy sites, etc. We all use URL shorteners for that purpose to shorten the long URLs to a few characters, which makes them easier to share and navigate through and also look clean and elegant.

But have you ever wondered about coding a URL shortener yourself? In this tutorial, we will explain the process of coding a URL shortener in Python with very easy steps.

There are a lot of different ways using which you can shorten a URL, the majority of them requiring API, in which you need to get stick to only one type of shortened URL ex. bitly, tinyurl, etc.

Code for URL shortener

In this tutorial, you will learn about a python package that makes it extremely easy to shorten URL as compared to the traditional method.

1. Install the module

To start with, we need to install the required modules that would ease out our work considerably for coding a URL shortener. We begin with the installation of the python library using the pip package manager.

Pyshorteners is Python library to wrap and consume the most used URL shorteners APIs.

pip install pyshorteners

2. Import, Input and Initialize

In a python file, we begin with importing the required modules.

import pyshorteners

We take input from the user at this point, we could have done the inputting part later in our code, but that would let us change the basic/permanent structure of the code, which we are going to change for every URL shortener’s APIs.

long_url = input("Enter the URL to shorten: ")

Now we initialize the pyshortener library’s class object to start shortening our URLs.

type_tiny = pyshorteners.Shortener()

3. Shorten the URL – Tinyurl

Now, as we have initialized our library, we can start with the shortening of the URLs.

You are required to pass the name along with the location of the PDF if it’s not in the same directory as the python script.

short_url = type_tiny.tinyurl.short(long_url)

print("The Shortened URL is: " + short_url)

In the output, we get the shortened URL in a form like – “https://tinyurl.com/mbq3m”. And the TinyURL is because the URL shortener package – Pyshortener uses Tinyurl API by Default.

Final Code to short using TinyURL service –

import pyshorteners
long_url = input("Enter the URL to shorten: ")

#TinyURL shortener service
type_tiny = pyshorteners.Shortener()
short_url = type_tiny.tinyurl.short(long_url)

print("The Shortened URL is: " + short_url)

But we can change it, and that is what we’ll learn further in this tutorial.

4. Shorten URL – Bitly

Bitly is by far the most popular and widely used URL shortener service. Here, using our code we will now generate shortened URL using its API which is wrapped by the Pyshortener library.

Using the same above method, just now we need to pass in our API key in the Shortener method as shown:

type_bitly = pyshorteners.Shortener(api_key='01b6c587cskek4kdfijsjce4cf27ce2')
short_url = type_bitly.bitly.short('https://www.google.com')

You might be wondering, where would you get the API key now, so go to the Bitly website > Create an account > then go to settings > API (developers) option. The page would look like this:

Simply generate the Access token of your account by entering your password and copy the token to use in code.

Bitly API
Bitly API access token

Final Code to shorten the URL with Python using Bitly API:

import pyshorteners
long_url = input("Enter the URL to shorten: ")

#Bitly shortener service
type_bitly = pyshorteners.Shortener(api_key='01b6c587cskek4kdfijsjce4cf27ce2')
short_url = type_bitly.bitly.short('https://www.google.com')

print("The Shortened URL is: " + short_url)

The Bitly service also offers more functionalities such as URL Expanding, getting the total number of clicks on your shortened URL, etc.

expand_url = type_bitly.bitly.expand('https://bit.ly/TEST')
print (expand_url) # gives the url in expand or original form

count = type_bitly.bitly.total_clicks('https://bit.ly/TEST') #gives total no. of clicks.

Using an API key helps us to manage our links in a better way, as we can now check all the details and performance of our links in our Account section in that particular shortening service (website) dashboard.

5. Shorten URL using other Services

The Pyshortener library is wrapped around various shortening services, which you can have a look at, at their official documentation.

The sample code for various services would be like this:

import pyshorteners
s = pyshorteners.Shortener()

s.chilpit.short('http://www.google.com')    # gives output -> 'http://chilp.it/TEST'

# Adf.ly
s = pyshorteners.Shortener(api_key='YOUR_KEY', user_id='USER_ID', domain='test.us', group_id=12, type='int')
s.adfly.short('http://www.google.com')    # gives output -> 'http://test.us/TEST'

s = pyshorteners.Shortener(code='12345')
s.gitio.short('https://github.com/TEST')     # gives output -> 'https://git.io/12345'

#and many more services are supported


That’s It for the tutorial. Hope you have learned about the shortening of URLs and how you can create a URL shortener using Python, and with of multiple shortening service providers.