A serverless API that collects your Spotify listening statistics using the spotipy library and makes them available for display in GitHub READMEs or other applications. Built with Python, Flask, and deployed on Vercel.
To deploy this solution for your own needs, jump to THIS section
Use the /stats endpoint to access the images API
| Parameters | Description | Possible values | Default values |
|---|---|---|---|
type |
Select the stat to display (only one per query) | artists, top_songs or last_albums |
artists |
range |
Select the time range of selection | short_term or long_term |
short_term |
You can also use the /json endpoint to access the raw Spotify data as JSON.
stats?type=artists&range=short_term
stats?type=top_songs&range=long_term
stats?type=last_albums
Follow these steps to deploy your own instance with your Spotify data.
- Go to Spotify Developer Dashboard
- Log in with your Spotify account
- Click Create an App
- Fill in the app name and description, select Web API and accept the TOS
- Once created, note your Client ID and Client Secret
- Click Edit Settings
- Add
http://localhost:8888/callbackto Redirect URIs - Save the settings
The refresh token allows the API to access your Spotify data without requiring browser interaction.
- Clone this repository:
git clone https://github.com/yourusername/SpotifyStats.git
cd SpotifyStats- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Edit
getRefreshToken.pyand replace the placeholders:
client_id="YOUR_CLIENT_ID", # From Spotify Dashboard
client_secret="YOUR_CLIENT_SECRET", # From Spotify Dashboard- Run the script:
python getRefreshToken.py- A browser window may open asking you to authorize the app
- The script will print your refresh token to the terminal- save this securely!
- Fork this repository to your GitHub account
- Go to your forked repository settings
- Navigate to Secrets and variables → Actions
- Add the following secrets (these are for CI/CD testing):
SPOTIPY_CLIENT_ID: Your Spotify Client IDSPOTIPY_CLIENT_SECRET: Your Spotify Client SecretSPOTIPY_REDIRECT_URI:http://localhost:8888/callback
- Go to Vercel and sign up/log in
- Click Add New Project
- Import your forked GitHub repository
- Vercel will detect the Python project automatically
- Create a Github Personal Access Token (PAT) --> you can see the steps from this repo
- Before deploying, add Environment Variables:
SPOTIPY_CLIENT_ID: Your Spotify Client IDSPOTIPY_CLIENT_SECRET: Your Spotify Client SecretSPOTIPY_REDIRECT_URI:http://localhost:8888/callbackSPOTIFY_REFRESH_TOKEN: The refresh token from Step 2PAT_1: The created PAT
- Click Deploy
Once deployed, Vercel will give you a URL like https://your-project.vercel.app
Test your API:
curl https://your-project.vercel.app/jsonYou should get a JSON with your Spotify statistics!
Add the following to any GitHub README to display your stats:
<a href="https://github.com/JohanVerne/SpotifyREADMEStats">
<img src="https://YOUR_VERCEL_DOMAIN_vercel.app/stats?PARAMS" />
</a>Replace YOUR_VERCEL_DOMAIN with the name of your API, present in your Vercel project dashboard, and PARAMS with the parameters listed here
Create a .env file in the project root:
SPOTIPY_CLIENT_ID=your_client_id
SPOTIPY_CLIENT_SECRET=your_client_secret
SPOTIPY_REDIRECT_URI=http://localhost:8888/callback
SPOTIFY_REFRESH_TOKEN=your_refresh_token
Run the collector directly:
python -m dotenv run -- python -m statsCollectorOr run the Flask API locally:
python -m dotenv run -- python api/index.pyRunning Tests
python -m dotenv run -- python -m pytest -q