Using various Python APIs to shorten URLs, List of Services Below:
Bitly URL Shortener
Bitly URL Shortener is simple to use but you must first Make an account – then go to settings – click on Advanced settings – you will then find the API option – click on OAuth option – generate the OAuth Token and Copy
Next, install ” bitly_api ” through there GitHub repository [ https[:]//github.com/bitly/bitly-api-python ] – unzip, and then move inside the folder (change directory [cd] | bitly-api-python-master) – After inside the folder install the setup file ( pyhton3 setup.py install )
Now we all done with the setup work and bitly_api is now installed on your machine.
import bitly_api
bit_access_token = " insert access token here "
api_access = bitly_api.Connection(access_token = "insert access token here"
URL = input('Enter link to shorten: ')
short_url = access.shorten(URL)
print('SHORT URL: ', short_url['url']
Cuttly URL Shortener
Cuttly URL Shortener is simple to use but you must first make an account – then go to edit profile – click on generate New API Keys
Only installation required for cuttly is “requests” which most machines already have :
pip install requests
Code for Cuttly after requests is installed in machine :
import requests
api_key = ' insert access token here '
url = input('Enter links to shorten: ')
api_url = f"https://cutt.ly?api/api.php?key={api_key}&short={url}"
data = requests.get(api_url).json()["url"]
if data ['status'] == 7:
short_url = data['shortlink']
print('SHORT URL: ', short_url)
else:
print('!! ERROR SHORTENING URL :', data)
” Start by importing requests into our code. Next, we enter our API key. Then we ask the user for input URL. Also, we need to specify the api_url parameters. Then, we sent it to requests to get data.
If data is valid, then we take the shortLink part of the data which is the shortened URL and we print it. If invalid, we return an error .”
PyShortener