spotify-uri

raw JSON →
1.0.3 verified Sat May 09 auth: no python

A Python port of @TooTallNate/spotify-uri for parsing and formatting Spotify URIs and URLs. Current version 1.0.3, actively maintained with low release cadence.

pip install spotify-uri
error AttributeError: 'SpotifyUri' object has no attribute 'id'
cause Parsed a URI that does not have an 'id' attribute (e.g., a playlist URI with only owner). Check type first.
fix
if uri.type == 'track': print(uri.id) else: handle other types.
error spotify_uri.uri.SpotifyUri
cause Incorrect import path; the correct module is spotify_uri.
fix
Use 'from spotify_uri import SpotifyUri' or 'from spotify_uri import parse'.
gotcha The parse function returns a SpotifyUri object. Accessing attributes before confirming type may raise AttributeError for incompatible types.
fix Check uri.type first, or use hasattr before accessing type-specific attributes.
deprecated Some older examples show using 'spotify_uri.uri.SpotifyUri' or 'spotify_uri.spotify_uri.SpotifyUri'. These are internal paths and may change.
fix Use from spotify_uri import SpotifyUri or from spotify_uri import parse.

Parse Spotify URIs and URLs to extract type and ID.

from spotify_uri import parse

uri = parse('spotify:track:4iV5W9uYEdYUVa79Axb7Rh')
print(uri.type)  # track
print(uri.id)    # 4iV5W9uYEdYUVa79Axb7Rh

url = parse('https://open.spotify.com/track/4iV5W9uYEdYUVa79Axb7Rh')
print(url.type)  # track
print(url.id)    # 4iV5W9uYEdYUVa79Axb7Rh