How I Built a Live YouTube Music Widget for My Portfolio
A look inside the Chrome extension and real-time pipeline that carries the song playing in my browser to my portfolio.
This started as a small portfolio idea: I wanted the site to show what I was listening to while I worked. The card itself was easy. The question that took me longer was where the track data could come from.
With Spotify, there is a documented “currently playing” endpoint. You authorize the account and request its active track. YouTube Music has a public Data API too, but its published resources cover things like videos and playlists; I could not find a documented endpoint for the current session inside an open YouTube Music tab. That is a narrower claim than “YouTube has no API”: the missing piece is a supported way for my portfolio server to ask what this browser is playing. I compared Spotify's current-track endpoint with Google's YouTube Data API reference.
That changed the shape of the project. The browser already had the answer; my server did not. So I made the browser the source of truth and used a Chrome extension to carry a small amount of track metadata to the site. It never captures audio. It sends the title, artist, artwork, playback state, and timing information that the player exposes.

The browser is the source of truth for what is playing. The server stores a small, authenticated snapshot so visitors can see that state without connecting to the music tab themselves.
- The extension reads track details in the browser and sends metadata to the portfolio API; it does not capture or upload audio.
- The API stores the latest state in Redis so production instances can share it, then the portfolio refreshes through server events and polling.
1. I started with the browser, not the server
My first source was the browser's Media Session metadata. When a site sets it, the browser can expose a track title, artist, album, artwork, and playback state. It is the cleanest place to start, and it is a standard browser feature. YouTube Music does not always populate every field consistently, though, so I treated it as the best source rather than the only source. The Media Session API reference describes the metadata surface.
The extension tries sources in order: Media Session first, then YouTube Music's player data and player-bar DOM, with the video element and page title as fallbacks. That ordering matters. The internal player object and DOM selectors are private implementation details; they can change when YouTube Music ships an update. I keep them behind the standard Media Session path so a selector change does not define the whole feature.
YouTube Music is a single-page app, so changing a track does not reload the tab. I watch player-bar mutations and video play/pause/end events, then do a light periodic check for changes the events miss. I also reject values such as “Home” or “Library”—otherwise a navigation label can briefly masquerade as a song title.
2. I crossed Chrome's two JavaScript worlds
A Chrome content script has an isolated JavaScript world. That is good for security, but it means it cannot directly read page-owned objects on YouTube Music. I split the work:bridge.ts runs in the page's MAIN world and reads player data;content.ts runs in the extension world, checks the result, and can use Chrome extension APIs.
The bridge passes a small message through window.postMessage. This is a handoff, not authentication: page messages can be observed or imitated. The content script validates the payload, and only the extension's service worker sends the authenticated request to my API.
// Page MAIN world: publish the player snapshot
window.postMessage({
source: "YTM_NOW_PLAYING_BRIDGE",
payload: track,
}, "*");
// Extension content world: accept only our bridge message
window.addEventListener("message", (event) => {
if (event.data?.source !== "YTM_NOW_PLAYING_BRIDGE") return;
latestBridgeTrack = event.data.payload;
checkAndUpdate();
});3. I kept network access in the extension worker
The content script sends a typed message to the Manifest V3 service worker. The worker loads the saved endpoint and secret from chrome.storage.sync, adds a Bearer token, and posts JSON to /api/now-playing. Keeping the request here gives the browser extension one clear place for connection status and local-versus-live routing.
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
body: JSON.stringify(track),
});I do not send a request on every metadata check. A new song or play/pause change is sent right away; while a track is playing, the extension refreshes its state every four seconds. That refresh doubles as a liveness signal. If the browser closes before it can say “stopped,” the server can still tell that the last update is old.
4. I made one small API the meeting point
The site's POST /api/now-playing route checks the token, validates the payload, normalizes the track fields, saves the latest state, and notifies visitors. The visitor page fetches the current state on load, listens for server-sent events, and polls periodically as a fallback. It is a simple shape: one current track, many readers.
const saved = await saveNowPlayingState(newState);
broadcastNowPlaying(saved);
return NextResponse.json({ success: true, data: saved });There is one extra metadata repair path worth calling out. If the browser sends an unusable title but includes a video ID, the API first tries YouTube's oEmbed response for that video. If that still cannot resolve a title, the current implementation tries to read a title from YouTube search-result HTML. That second fallback is deliberately not the source of live playback state—and it is the least stable part of the pipeline, because page HTML is not a supported API contract. The live state still comes from the open player in the browser.
// Repair incomplete metadata from a known video ID
const url =
"https://www.youtube.com/oembed?url=" +
encodeURIComponent("https://www.youtube.com/watch?v=" + videoId) +
"&format=json";
const response = await fetch(url);
if (response.ok) {
const video = await response.json();
title = cleanSongTitle(video.title, artist);
}These calls solve a different problem from playback detection. oEmbed can describe a known video; it cannot tell my server what is playing in my browser right now. The search-page fallback is even more fragile: it depends on YouTube's HTML shape and can stop matching after a site change. Because the API waits for these lookups before saving an invalid update, a slow response can also delay that repair request. I keep this path as best-effort metadata cleanup; it is not the source of the live track.
A map of the files
I kept the extension and website as separate pieces. The extension gathers and sends the update; the Next.js app accepts it, stores it, and renders it. Here are the files I touched most often:
apps/
├── extension/
│ ├── manifest.json
│ └── src/
│ ├── bridge.ts
│ ├── content.ts
│ ├── background.ts
│ ├── popup.ts
│ └── types.ts
└── web/app/
├── page.tsx
├── components/now-playing.tsx
├── api/now-playing/route.ts
├── api/now-playing/stream/route.ts
└── lib/now-playing/
├── storage.ts
├── broadcaster.ts
└── types.ts
manifest.json is what wires the browser side together: Chrome loads the compiled bridge.js in the page's main world and content.js as the isolated content script. The build turns the TypeScript files in src into those JavaScript files in dist. The small types.ts module is imported by the extension scripts so the track shape and message names stay consistent.
On the website, app/page.tsx imports the storage function and the player component. It reads the first state on the server, then passes it into the client component so the page has something to render before the live connection starts:
import { NowPlaying } from "./components/now-playing";
import { getNowPlayingState } from "./lib/now-playing/storage";
const initialData = await getNowPlayingState();
// In the page JSX:
<NowPlaying initialData={initialData} />After that, now-playing.tsx fetches updates from the API and listens to /api/now-playing/stream. The route imports storage.ts to read or save the track and broadcaster.ts to notify connected visitors. That separation made it much easier to trace a bug: I could check whether it started in the browser, during the POST, in shared storage, or in the page that displays the result.
// apps/web/app/api/now-playing/route.ts
import {
getNowPlayingState,
saveNowPlayingState,
} from "../../lib/now-playing/storage";
import { broadcastNowPlaying } from "../../lib/now-playing/broadcaster";5. Production changed the storage and delivery assumptions
Locally, it is easy to mistake a process variable or a file for a database: one development server handles the requests, so everything appears to share state. In production, requests can land on different serverless instances. An update written into one instance's memory may not exist in the instance serving a visitor's next request. I configured Redis as the shared store so every instance reads the same latest track.
Real-time delivery has a similar wrinkle. The server-sent event broadcaster is in-process, so it can immediately reach visitors connected to that same instance; it is not a shared event bus across all serverless instances. The page therefore also polls the API every 2.5 seconds. Redis provides the shared truth; polling lets a visitor pick it up even when the update and the visitor are connected to different instances.
I also had to treat “playing” as temporary state. The extension refreshes while music plays, and the API marks a track as no longer live if it has not been updated for five minutes. That covers the cases where a laptop sleeps, a tab crashes, or the browser disappears before a stop message gets through. The last song can stay visible as recently played without claiming it is still playing.
6. I used the same extension for local work and the live site
The endpoint changes between environments; the data flow does not. For local development, I run the Next.js app on port 1234, build the extension, load apps/extension/dist from chrome://extensions, and choose the Local preset in the popup. The API and popup must use the same update token:
# apps/web/.env.local
NOW_PLAYING_UPDATE_TOKEN=your-local-secret
# Terminal 1
cd apps/web && bun run dev
# Terminal 2
cd apps/extension && bun run buildFor the deployed site, I set NOW_PLAYING_UPDATE_TOKEN in the Vercel environment and redeploy, then select the Production preset pointing to https://sahilkhedkar.com/api/now-playing. The local .env.local file is only for my machine; it does not set the production secret. If the extension gets a 401, I check that both sides have the same token. If the request cannot connect, I use Test Connection to check the chosen URL before investigating playback detection.
The production app also needs the shared-store credentials configured: this code reads UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN (or a REDIS_URL). Without a reachable Redis store, the development fallbacks can keep a track in local memory or a file, but those fallbacks cannot share state across production instances. The token lets the extension update the API; Redis lets every API instance and visitor read the same track. They solve different problems.
Dual Sync is useful while I am developing: the worker can post to production and localhost in parallel. Each endpoint is reported separately in the popup, and one successful target is enough for sync to count as successful. That lets the live site keep updating while the local app is stopped or being rebuilt.
The small card hides a useful lesson
The part I enjoyed most was tracing one song all the way through the system: from a player running in my browser, across Chrome's extension boundary, into an authenticated API, and back out to everyone visiting the site. The finished card is tiny; getting each handoff clear and dependable was the real project.
It is still a browser-side integration, not a connection to a YouTube account in the cloud. The extension has to be running where the music plays, and YouTube can change the private player details the fallbacks rely on. If I revisit this, I would first remove the search-page HTML fallback and keep metadata repair on supported interfaces. For now, the standard browser metadata is the first choice, the fragile paths are contained, and stale tracks stop pretending to be live.
Final Words
A small live widget can teach you a lot about boundaries: the browser knows what is playing, the extension carries only the details the site needs, and the server gives every visitor one shared view.
If you build something similar, start by deciding which side owns each piece of state. That one decision makes the API, storage, and failure cases much easier to reason about.
“True education combines theoretical knowledge with realization, so what we learn becomes skillful living and freedom from ignorance.”
— Bhagavad Gita