Skip to content

Instantly share code, notes, and snippets.

@chand1012
Created March 18, 2021 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chand1012/96581b90c575d70bbf671ed00a74adde to your computer and use it in GitHub Desktop.
Save chand1012/96581b90c575d70bbf671ed00a74adde to your computer and use it in GitHub Desktop.
How to use TinyURL as an API for shortened links.

Parameters

Returns

Format: text

  • Plaintext shortened URL

Examples

Python 3.6+

import requests

def shorten(link):
  resp = requests.get(f'https://tinyurl.com/api-create.php?url={link}')
  return resp.content.decode()

Go

func shorten(link string) (string, error) {
	response, err := http.Get("https://tinyurl.com/api-create.php?url=" + link)

	if err != nil {
		return "", err
	}

	output, err := ioutil.ReadAll(response.Body)

	if err != nil {
		return "", err
	}

	response.Body.Close()
	return string(output), err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment