Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/Bitly/Bitly_Create_Links.ipynb
2973 views
Kernel: Python 3

Bitly.png

Bitly - Create Links

Give Feedback | Bug report

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

Last update: 2023-04-12 (Created: 2023-02-16)

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

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)