A small Python utility to generate a Last.fm session key using the official web authentication flow.
This script is intended as a one-time setup helper for applications or scripts that interact with the Last.fm API and require authenticated user actions (such as loving tracks).
This script walks you through the Last.fm authentication process and retrieves a session key tied to your Last.fm account.
It uses the pylast library to:
- Create a temporary authentication request
- Generate a browser authorization URL
- Wait for you to approve the application on Last.fm
- Retrieve and print your personal session key
The session key can then be copied and reused in other scripts without repeating the authentication process.
Others scripts for mine that use the session key are:
-
Create an Unauthenticated Network
The script initializes aLastFMNetworkusing only your API key and API secret (no session key yet). -
Generate Authorization URL
A web authentication URL is generated and printed to the terminal. This URL must be opened in your browser so you can authorize the application on Last.fm. -
User Authorization
You log into Last.fm (if needed) and approve the app’s access to your account. -
Retrieve Session Key
After authorization, the script retrieves your session key and prints it to the console.
This key can be reused indefinitely unless revoked.
| Name | Description | Suggested Value |
|---|---|---|
API_KEY |
Your Last.fm API key | xxxxxxxxxxxxxxxx |
API_SECRET |
Your Last.fm API secret | xxxxxxxxxxxxxxxx |
You can obtain an API key and secret from the
Last.fm API authentication page
python3 -m venv ~/lastfm_auth_venv
source ~/lastfm_auth_venv/bin/activate
pip install --upgrade pip
pip install pylast/activatepython get_session_key.py- Open the printed URL in your browser
- Authorize the application on Last.fm
- Return to the terminal and press Enter
- The script will then print your SESSION KEY.
Output Example
Open this URL in your browser and authorize the app:
https://www.last.fm/api/auth/?api_key=...
After you've authorized, press Enter here...
Your Last.fm SESSION KEY is:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Keep it secret! You can now paste it into your script.
🔐 Keep your session key private — it grants access to your Last.fm account
✅ This script only needs to be run once
❌ If you revoke access on Last.fm, you’ll need to generate a new session key
You can find a console command tutorial on this reddit comment.