Skip to content
J. Shipps

Spotify Now Playing

A small glass card that shows what I'm listening to, for any website. One script tag, no dependencies.

"The Spotify widget's card: album art, track title and artist, a green three-bar equaliser

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.

index.html
<div data-spotify-widget data-variant="card" data-theme="auto"></div>
<script src="https://spotify.yourdomain.com/embed/spotify.js" defer></script>

The whole embed. The script finds every element with data-spotify-widget and draws a card inside it.

A representation of the widget functioning on JShipps.me
The card variant: album art, title and artist, and an equaliser while a song is playing.

What it handles

  • 01Two looks: a card with album art, or a compact pill for a footer
  • 02Light, dark, or follows the page (data-theme, a .dark class, or the system setting)
  • 03An idle state when nothing is playing, or hide it with data-idle="hide"
  • 04Polls every 30 seconds by default, never faster than every 10, and pauses while the tab is hidden
  • 05Server caches Spotify's answer for 15 seconds, so a busy page makes one request, not hundreds
  • 06Styles live in Shadow DOM: the card can't break your CSS and your CSS can't break the card
  • 07Only https album art and song links are used, and every piece of text is escaped
  • 08Demo 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
npm run auth walks you through Spotify's sign-in once and gives you a refresh token. npm start opens a playground at 127.0.0.1:3000 with light, dark, live and demo states.

How it works

  • The server has no dependencies. Node.js 20+ with the built-in http module and fetch. 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-playing always returns the same JSON: connected, isPlaying, and when a song is on, title, artist, album, albumArt, songUrl, progressMs and durationMs. 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 /healthz check. There's also a Next.js kit: a route handler and a <SpotifyNowPlaying /> component.
A representation of light and dark color schemes.
Light and dark, side by side.

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.