Shipping A Full Spotify Real-Time Status App
· 7 min read
I wanted a tiny page that answers one question, live: what am I listening to right now?
Built entirely with Next.js (App Router), this application provides a sleek, real-time look into your current Spotify playback state. Instead of reaching out to Spotify directly from the browser (and leaking your API secrets), we funnel everything through a secure, server-rendered Next.js backend.
What Spotify changed in 2026
The February Web API changelog still lists the three playback reads this kind of app needs. The portfolio widget reads the account’s current track using the first endpoint.
GET /me/player/currently-playingusesuser-read-currently-playingto read the item currently playing.GET /me/playerusesuser-read-playback-stateto read playback state and Spotify Connect device details.GET /me/player/recently-playedusesuser-read-recently-playedto read recent track history. It can supply a fallback when the player is idle.
These Web API calls return snapshots. I didn't find a documented account-wide stream of playback events for a status widget. The Web Playback SDK’s player_state_changed event describes the SDK player running in the browser. For a status widget that follows the account across devices, the implementation below still polls. If you add a history fallback, label it “Last played” and keep its timestamp separate from live progress. The history endpoint covers tracks and excludes podcast episodes. This walkthrough also skips episodes in the current item.
The bigger changes are in access and authorization.
- Development Mode. The app owner needs active Premium, and new apps are limited to five authorized users. In this build, the owner authorizes the Spotify account and visitors read the widget’s server response. Only the owner connects an account. Spotify postponed the endpoint access changes for existing integrations on March 9. If you extend this build beyond playback, check the migration guide for the library, catalog, and playlist changes that apply to your app.
- Refresh tokens expire. Spotify introduced a six-month lifetime on June 18 for new apps and July 20 for existing apps. The clock starts when the user authorizes the app. Exchanging the refresh token for an access token does not extend it. On
400 invalid_grant, run authorization again and replace the refresh token in your deployment. Access tokens still last about an hour. Spotify’s token refresh guide
Here is the file layout for the standalone polling demo below. The portfolio homepage uses its own server-rendered widget.
app/api/now-playing/route.ts- Polling endpoint
app/lib/spotify.ts- Server-only Spotify API wrapper
app/page.tsx- Page that renders the widget
components/ui/now-playing.tsx- Playback status component
.env.local- Local Spotify credentials
next.config.ts- Next.js configuration
That sounds simple, but doing it properly means solving token refresh, null states, no-cache data, and safe server boundaries. This scrawl is the build log of that journey, written so you can ship your own version without the usual OAuth pain.
What This App Actually Does
By the end of this build, we have:
- A strict
server-onlySpotify data layer. - A resilient
/api/now-playingendpoint. - A frontend that polls cleanly and never explodes on paused playback.
- A live embeddable experience you can drop into your portfolio.
Environment Variables
Use this .env.local baseline:
Optional fallback for temporary debugging:
Core Spotify Module (Exact Pattern)
This is the server-only module for the standalone demo:
Real-Time API Route
This is the route contract the client consumes. Notice how it keeps the response shape stable even when Spotify fails.
Why This Endpoint Is Reliable
force-dynamic+no-storekeeps data fresh for every request.204maps tonullcleanly, not an app crash.- Stable
{ song }output keeps client logic dead simple. - Spotify outages degrade gracefully to
song: null.
Client Polling Pattern
How To Get A Spotify Refresh Token (Without Guesswork)
For a personal widget, use the refresh token script from the portfolio repository. Add http://127.0.0.1:8888/callback to your Spotify app’s redirect URIs and put SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET in your ignored .env.local. From the repository root, run:
Open the printed authorization URL. The script checks the callback state and exchanges the authorization code on your machine. Copy the SPOTIFY_REFRESH_TOKEN line from your terminal into your local or deployment secrets. Run it again when you need to reauthorize. The default scope covers the current track. To enable history too, append user-read-currently-playing user-read-recently-played to the command.
The steps below show the same flow manually.
1) Create Spotify App
- Go to Spotify Developer Dashboard.
- Create an app. For Development Mode, use the Premium account that will own it.
- Copy
Client IDandClient Secret. - Register the exact callback URI your local listener serves, such as
http://127.0.0.1:3000/callbackfor the manual example. Spotify accepts HTTP for literal loopback addresses. Use HTTPS for deployed callbacks. Redirect URI rules
2) Scopes You Need
Request user-read-currently-playing for this walkthrough. Add the other scopes listed above only if you call those endpoints.
3) Build Authorization URL
Replace placeholders and open this in the browser:
Generate a fresh random state before opening the URL. After approval, Spotify redirects to your callback with the code and state. Confirm the returned state matches, then exchange the code. Authorization Code flow
4) Exchange Code For Refresh Token
Use curl and replace placeholders:
You should receive refresh_token and access_token. Save the refresh token securely.
5) Put Secrets In Env
The five-second example suits a local walkthrough. On a public page, each open tab can add requests against the same app limit. Reuse the access token until it’s close to expiry, coalesce simultaneous server reads, and pause polling when the page is hidden. A production route should surface upstream 429 and forward Retry-After so the poller can wait before trying again. Use the latest progress_ms to animate progress locally between snapshots while the track is playing. Spotify rate limits
Production Notes That Matter
- Never expose
CLIENT_SECRETorREFRESH_TOKENon client side. - Keep Spotify fetches server-only.
- Return a stable JSON shape even on failure.
- Choose a polling interval based on traffic and back off when Spotify rate limits you.
- Log HTTP status and a safe error code for debugging. If refresh returns
invalid_grant, replace the token through a fresh authorization instead of retrying it.
If you only remember one thing from this post: treat Spotify credentials like database credentials, never browser credentials.
Shipping Checklist
- Spotify app created with correct redirect URI.
- Required scopes granted.
- Refresh token captured and stored in deployment secrets.
/api/now-playingreturns live payload locally.- Polling UI updates progress and handles paused state.
- Embeds render cleanly across README and website.
Live Spotify status
I hope this scrawl saves you hours of frustration and gets your Spotify status live with confidence. Keep authorization on the server and make sure you can reconnect the account when the refresh token expires.