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.
You should press the Sign up at the right upper corner.
Scroll down and press the Get Started for Free.
Then fill out the fields with your preferred 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):
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.
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.
That is it. See the full documentation of it here of how to use Bitly in Python.
Python for Finance: Unlock Financial Freedom and Build Your Dream Life
Discover the key to financial freedom and secure your dream life with Python for Finance!
Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.
Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.
Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.
Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!
Python for Finance a 21 hours course that teaches investing with Python.
Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.
“Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.
8 thoughts on “How to Setup an Automated Bitly URL-shortener in Python in 3 Easy Steps”
Wow, that’s what I was exploring for, what a material!
existing here at this weblog, thanks admin of this
Greɑt bloց heгe! Also your web site loads up very fast!
What hoѕt are yoս using? Can I get youг affiliate link to
your host? I wіsh my websitе loaded up aas fast as yours lol
It is siteground. You can see it here: https://www.siteground.com/recommended?referrer_id=7636763
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!
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.