Skip to content

Latest commit

 

History

History
60 lines (38 loc) · 1.98 KB

Bitly_Create_Links.md

File metadata and controls

60 lines (38 loc) · 1.98 KB



Template request | Bug report | Generate Data Product

Tags: #bitly #link #shorten #url #create #python

Author: Florent Ravenel

Description: This notebook will show how to create short links with Bitly.

References:

Input

Import libraries

import requests
import json

Setup Variables

token = "<YOUR_TOKEN_HERE>"
long_url = "https://www.example.com/"

Model

Shorten URL

This function will use the Bitly API to shorten a given URL.

def shorten_url(token, long_url):
    url = "https://api-ssl.bitly.com/v4/shorten"
    headers = {"Authorization": "Bearer " + token}
    payload = {"long_url": long_url}
    response = requests.post(url, headers=headers, json=payload)
    response_json = response.json()
    return response_json["link"]

Output

Display result

short_url = shorten_url(token, long_url)
print(short_url)