A modern React application demonstrating Spotify Web API integration using Vite. This example shows how to:
- Authenticate with Spotify using OAuth 2.0
- Fetch currently playing track information
- Display track details and playback status
- Update information in real-time
- Modern build tool - Uses Vite for fast development and building
- Minimal dependencies - Only React and Vite
- Educational focus - Clean, readable code following React best practices
- Real-time updates - Polls Spotify API every 5 seconds
- Modern React patterns - Uses functional components and hooks
-
Create a Spotify App:
- Go to Spotify Developer Dashboard
- Create a new app
- Add
http://localhost:3000/redirectto Redirect URIs
-
Configure the app:
- Open
src/config/spotify.js - Replace the client ID with your Spotify app's Client ID (already configured!)
- Open
-
Install dependencies and run:
npm install npm run dev
-
Open your browser:
- Vite will automatically open
http://localhost:3000 - Click "Login to Spotify"
- Authorize the app
- Start playing music in Spotify!
- Vite will automatically open
- User clicks "Login to Spotify"
- Redirects to Spotify's authorization endpoint
- User authorizes the app
- Spotify redirects back with access token in URL fragment
- App extracts token and stores it
- Uses Spotify Web API
/me/playerendpoint - Fetches currently playing track information
- Updates UI with track name, artist, album art, and progress
- Polls every 5 seconds for real-time updates
- OAuth 2.0 Implicit Grant Flow - Client-side authentication
- Fetch API - Modern JavaScript for HTTP requests
- React Hooks - useState, useEffect, useCallback for state management
- Error Handling - Proper try/catch and user feedback
- Real-time Updates - Polling pattern for live data
├── index.html # Main HTML template
├── vite.config.js # Vite configuration
├── package.json # Dependencies and scripts
├── src/
│ ├── main.jsx # Entry point that renders the app
│ ├── App.jsx # Main App component with authentication logic
│ ├── index.css # Global styles
│ ├── components/
│ │ └── Player.jsx # Player component for displaying track info
│ ├── config/
│ │ └── spotify.js # Spotify API configuration
│ └── utils/
│ └── hash.js # URL hash parsing utility
└── README.md # This file
- React 18 - Modern UI framework
- Vite - Fast build tool and dev server
- @vitejs/plugin-react - React support for Vite
That's it! No complex build configuration needed.
This minimal example teaches:
- Spotify Web API authentication
- React functional components and hooks
- Modern JavaScript (async/await, fetch)
- OAuth 2.0 flow implementation
- Real-time data polling
- Error handling patterns
Perfect for understanding how to integrate external APIs with React applications!
Each file has a specific purpose following modern React best practices:
src/config/spotify.js- Contains all Spotify API configuration (endpoints, client ID, scopes)src/utils/hash.js- Utility function to parse OAuth tokens from URL fragmentssrc/components/Player.jsx- React component that displays track information and progresssrc/App.jsx- Main component handling authentication, API calls, and state managementsrc/main.jsx- Entry point that renders the React appindex.html- HTML template for the application
This structure follows modern React conventions:
- Components in separate files with
.jsxextension - Configuration separated from components
- Utilities in dedicated folders
- Clear separation of concerns
- Troubleshooting Guide - Comprehensive guide for resolving common issues
- API Endpoints Guide - Examples of additional Spotify API endpoints to explore
- Contributing Guide - How to contribute to this educational project
- Blog Post - Original tutorial: "How to Build A Spotify Player with React in 15 Minutes"
- Start Here: Follow the Quick Start guide above
- Understand the Code: Read the detailed comments in
src/App.jsxandsrc/components/Player.jsx - Troubleshoot Issues: Check the Troubleshooting Guide if you encounter problems
- Expand Your Skills: Explore additional endpoints in the API Endpoints Guide
- Build More Features: Use the examples to add playlists, search, or recommendations
- Spotify Web API Documentation - Official API reference
- Spotify Developer Dashboard - Manage your app settings
- React Documentation - Learn React fundamentals
- OAuth 2.0 Guide - Understanding Spotify's authentication
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
Joe Karlsson
- 🐦 Twitter: @JoeKarlsson1
- 💻 GitHub: @JoeKarlsson
- 💼 LinkedIn: /in/joekarlsson
- 🌐 Website: joekarlsson.com
⭐ Star this repository if you found it helpful!
