PyShorteners

raw JSON →
1.0.1 verified Fri May 01 auth: no python

A Python library that wraps and consumes the most popular URL shortener APIs, including TinyURL, Bitly, and others. Current version 1.0.1, released in 2021; development appears stalled but library is functional. Supports Python 3.6+.

pip install pyshorteners
error ModuleNotFoundError: No module named 'pyshorteners'
cause Library not installed.
fix
Run: pip install pyshorteners
error AttributeError: module 'pyshorteners' has no attribute 'Shortener'
cause Incorrect import pattern (e.g., from pyshorteners import Shortener). The correct import is 'import pyshorteners'.
fix
Replace 'from pyshorteners import Shortener' with 'import pyshorteners' and use pyshorteners.Shortener().
breaking Version 1.0.0 removed the old Shortener class pattern. The new API uses pyshorteners.Shortener() and provider-specific methods (e.g., .tinyurl.short()). No more global short() function.
fix Update imports: replace 'from pyshorteners import Shortener' with 'import pyshorteners' and use pyshorteners.Shortener().tinyurl.short(url).
deprecated Some providers like Google URL Shortener (goo.gl) are deprecated or shut down. Using them will raise errors.
fix Switch to active services like TinyURL (no API key) or Bitly (requires API key).
gotcha Many providers (Bitly) require an API key. If not set, they silently fail or raise an error. Always check provider docs.
fix Set API keys via environment variable or pass to Shortener constructor: pyshorteners.Shortener(bitly_token=os.environ.get('BITLY_TOKEN')).

Quickstart: import pyshorteners, create Shortener instance, call a provider's .short() method.

import pyshorteners

s = pyshorteners.Shortener()
# For services requiring API keys, set them via env or kwargs
# short_url = s.tinyurl.short('https://www.example.com')
# print(short_url)

# Example with valid URL
long_url = 'https://www.example.com'
short_url = s.tinyurl.short(long_url)
print(short_url)