How to Setup an Automated Bitly URL-shortener in Python in 3 Easy Steps

What will we cover in this tutorial?

  • To get you started from scratch to use Bitly in Python.
  • How to register at Bitly and get your Authentication token to access the Bitly API v4.
  • To get the Python library to connect to the API.
  • A walk-through of the API functionality available.

Step 1: Register at Bitly and get your Access Token

The first thing you need is to register at Bitly to use their functionality.

You can do that be follow this link.

Press the Sign up at Bitly

You should press the Sign up at the right upper corner.

Scroll down and press the Get Started for Free.

Press Get Started for Free

Then fill out the fields with your preferred credentials.

Fill out with your credentials

You should receive an email in the inbox of the email address you provided. Click the link in that email.

You should be forwarded to Bitly and then you should find your profile settings.

When clicking the Profile Settings you should find the Generic Access Token.

You will be asked to fill out your password, then press Generate Token.

Then you should get an access token like this one (this one is not valid, so you cannot use it):

d9742171bc53c4847c76ec2d02f7b83466aedd8d

Step 2: The URL-shortener library in Python

Luckily someone else did all the complicated work to use the Bitly API. All you need to do in Python is to get the library. The library is called pyshortener, which is a library to wrap and consume the most used shorteners APIs. Hence, you can use it with many URL shorteners. Please notice, that the access token you have generated in this tutorial is only valid for Bitly.

To install that library simply type the following command in your shell.

pip install pyshortener

You get a full list of the supported URL-shorteners in their documentation.

Step 3: Use the URL-shortener library

You get access to your URL-shortener functionality at Bitly by the following setup.

import pyshorteners
s = pyshorteners.Shortener(api_key='YOUR_KEY')

You should obviously use replace ‘YOUR_KEY’ with the key you obtained in Step 1.

Then you can create a Bitly shortened URL with the following call.

import pyshorteners

ACCESS_TOKEN= "YOUR_KEY"
s = pyshorteners.Shortener(api_key=ACCESS_TOKEN)
url = s.bitly.short("http://www.learnpythonwithrune.org/how-to-setup-an-automated-bitly-url-shortener-in-python-in-3-easy-steps/")
print(url)

Which in my example gave me the following link.

https://bit.ly/2NLc1Zq

This should also populate your web API at Bitly.

You can also retrieve how many clicks have been made on your link.

import pyshorteners

ACCESS_TOKEN= "YOUR_KEY"
s = pyshorteners.Shortener(api_key=ACCESS_TOKEN)
clicks = s.bitly.total_clicks("https://bit.ly/2NLc1Zq")
print(clicks)

Which in this case (surprisingly) returned 0 clicks.

Finally, you can also expand on a URL you already created a Bitly link on.

import pyshorteners

ACCESS_TOKEN= "YOUR_KEY"
s = pyshorteners.Shortener(api_key=ACCESS_TOKEN)
expand = s.bitly.expand("https://bit.ly/2NLc1Zq")
print(expand)

Which will result in the URL you created it with.

http://www.learnpythonwithrune.org/how-to-setup-an-automated-bitly-url-shortener-in-python-in-3-easy-steps/

That is it. See the full documentation of it here of how to use Bitly in Python.

Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

8 thoughts on “How to Setup an Automated Bitly URL-shortener in Python in 3 Easy Steps”

  1. Hey would you mind sharing which blog platform you’re using?

    I’m planning to start my own blog soon but I’m having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems
    different then most blogs and I’m looking for something unique.
    P.S Apologies for getting off-topic but I had to ask!

    Reply
    • Hey, This is built using WordPress.
      I don’t really have experience with anything else. I chose it because I felt, at the time, that it would be easy to administrate.
      If you have any specific questions, you are welcome to ask, but I cannot really compare it, as I didn’t use the other alternatives you write about.
      Rune

      Reply

Leave a Comment