Spotify Now Playing
A small glass card that shows what I'm listening to, for any website. One script tag, no dependencies.
Why I built it
The hero of this site has a small card that shows what I'm listening to on Spotify. People asked how it works, and every answer started with "well, first you need a server", because Spotify won't hand out your listening data to a browser without a secret key.
So I pulled it out into its own project. A tiny server holds the keys and answers one question, "what's playing?", and a script tag draws the card on any page: plain HTML, Next.js, Astro or Webflow.
<div data-spotify-widget data-variant="card" data-theme="auto"></div>
<script src="https://spotify.yourdomain.com/embed/spotify.js" defer></script>
What it handles
- Two looks: a card with album art, or a compact pill for a footer
- Light, dark, or follows the page (data-theme, a .dark class, or the system setting)
- An idle state when nothing is playing, or hide it with data-idle="hide"
- Polls every 30 seconds by default, never faster than every 10, and pauses while the tab is hidden
- Server caches Spotify's answer for 15 seconds, so a busy page makes one request, not hundreds
- Styles live in Shadow DOM: the card can't break your CSS and your CSS can't break the card
- Only https album art and song links are used, and every piece of text is escaped
- Demo mode (data-demo="playing" or "idle") for building a page without a Spotify account
~ $ git clone https://github.com/JShipps/spotify-widget.git
cd spotify-widget
npm run auth
npm start
How it works
- The server has no dependencies. Node.js 20+ with the built-in
httpmodule andfetch. It swaps the refresh token for an access token, keeps that token in memory until a minute before it expires, and asks Spotify what's playing. - One endpoint, one shape.
GET /api/now-playingalways returns the same JSON:connected,isPlaying, and when a song is on,title,artist,album,albumArt,songUrl,progressMsanddurationMs. Podcasts work too. - It fails quietly. If Spotify is slow or down, the server returns the last answer it had, and the card shows "Not playing" instead of an error.
- The embed is one file. About 13 KB of plain JavaScript, unminified. It reads its settings from data attributes and redraws when the page switches between light and dark.
- Runs anywhere Docker does. The Dockerfile uses a non-root user and a
/healthzcheck. There's also a Next.js kit: a route handler and a<SpotifyNowPlaying />component.
On this site
Here the widget doesn't run as a separate server. The same code lives inside the site: I connect my Spotify account once from the admin, the refresh token is stored in the site's settings, and the home page asks /api/spotify/now-playing for the latest track. The card in the hero is drawn by the site's own script so it matches the rest of the glass design.
The endpoint is open to other origins, which is how the standalone version started: jshipps.me/embed/spotify.js is the first version of the embed.