← Back to list

Node.js Tutorial: Detect BPM and Musical Key from Songs

Music applications are becoming increasingly intelligent. From playlist generators to DJ software and recommendation engines, understanding…

SoundNet · 2026-06-04 17:58 · 0 claps · 3.1 min read
#javascript #nextjs #dsp #audio-engineering #data-science
Open on Medium ↗
Wiki topics: ML · Machine Learning GEN · Genomics & Sequencing 🌐 · Web Development 🔬 · Science · General 🎵 · Music & Audio

Node.js Tutorial: Detect BPM and Musical Key from Songs

Music applications are becoming increasingly intelligent. From playlist generators to DJ software and recommendation engines, understanding a song’s musical properties unlocks powerful features for developers.

In this tutorial, you’ll learn how to detect a song’s BPM (tempo) and musical key using Node.js and the Track Analysis API by SoundNet.

By the end, you’ll be able to:

  • Detect a track’s BPM
  • Identify its musical key and mode
  • Retrieve advanced audio features
  • Build music-aware applications with Node.js

What Is BPM and Musical Key?

Before jumping into code, here’s a quick breakdown.

BPM (Beats Per Minute)

BPM measures the tempo of a song.

  • 60 BPM = slow
  • 120 BPM = dance/pop tempo
  • 160+ BPM = high-energy electronic or metal

Tempo is useful for:

  • DJ mixing
  • Workout playlists
  • Beat synchronization
  • Music visualization

Musical Key

A musical key defines the tonal center of a track.

Examples:

  • C Major
  • A Minor
  • G# Minor

Key detection helps with:

  • Harmonic mixing
  • Mashups
  • Sample matching
  • AI music generation

The API We’ll Use

The Track Analysis API by SoundNet provides:

  • BPM detection
  • Musical key detection
  • Camelot notation
  • Energy and danceability scores
  • Loudness and acousticness
  • Popularity and metadata

The API is available through RapidAPI.

Project Setup

Create a new Node.js project:

mkdir music-analysis
cd music-analysis
npm init -y

We’ll use the built-in fetch() available in modern Node.js versions.

If you’re using an older version of Node, install:

npm install node-fetch

Getting Your API Key

Create an account on RapidAPI and subscribe to the Track Analysis API.

You’ll receive an API key that looks like this:

YOUR_RAPIDAPI_KEY

Example 1: Detect BPM and Key from a Song Name

Create a file called analyze.js.

Create a file called analyze.js.

const song = "Respect";
const artist = "Aretha Franklin";
const params = new URLSearchParams({
  song,
  artist,
});
const url = `https://track-analysis.p.rapidapi.com/pktx/analysis?${params.toString()}`;
async function analyzeTrack() {
  const response = await fetch(url, {
    method: "GET",
    headers: {
      "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
      "x-rapidapi-host": "track-analysis.p.rapidapi.com",
    },
  });
  const data = await response.json();
  console.log(data);
}
analyzeTrack();

Run the script:

node analyze.js

Example Response

The API returns detailed musical information:

{
  "id": "6fc5ea2bdd4820c407ccd75fae23b86e",
  "key": "C",
  "mode": "major",
  "camelot": "8B",
  "tempo": 115,
  "duration": "2:28",
  "popularity": 77,
  "energy": 56,
  "danceability": 81,
  "happiness": 97,
  "acousticness": 16,
  "instrumentalness": 0,
  "liveness": 5,
  "speechiness": 4,
  "loudness": "-5 dB"
}

Understanding the Results

Here’s what some of these values mean:

PropertyDescriptionkeyMusical key of the trackmodeMajor or minor scaletempoBPM of the songcamelotDJ-friendly harmonic mixing notationenergyIntensity and activity leveldanceabilityHow suitable the track is for dancingacousticnessProbability the track is acousticinstrumentalnessLikelihood of no vocals

These metrics can power recommendation systems, smart playlists, and generative music apps.

Example 2: Faster Key & BPM Detection

If you only need tempo and key, use the optimized endpoint:

const params = new URLSearchParams({
  song: "Respect",
  artist: "Aretha Franklin",
});
const url = `https://track-analysis.p.rapidapi.com/pktx/key-bpm?${params.toString()}`;
async function getKeyAndBpm() {
  const response = await fetch(url, {
    method: "GET",
    headers: {
      "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
      "x-rapidapi-host": "track-analysis.p.rapidapi.com",
    },
  });
  const data = await response.json();
  console.log(data);
}
getKeyAndBpm();

Example response:

{
  "id": "6fc5ea2bdd4820c407ccd75fae23b86e",
  "key": "C",
  "mode": "major",
  "tempo": 115
}

This endpoint is ideal for:

  • DJ software
  • Real-time analysis
  • Lightweight music apps
  • BPM sorting tools

Example 3: Analyze Tracks Using Spotify IDs

If your application already uses Spotify data, you can query tracks directly using Spotify Track IDs.

const spotifyTrackID = "7s25THrKz86DM225dOYwnr";
const url = `https://track-analysis.p.rapidapi.com/pktx/spotify/${spotifyTrackID}`;
async function analyzeSpotifyTrack() {
  const response = await fetch(url, {
    method: "GET",
    headers: {
      "x-rapidapi-key": "YOUR_RAPIDAPI_KEY",
      "x-rapidapi-host": "track-analysis.p.rapidapi.com",
    },
  });
  const data = await response.json();
  console.log(data);
}
analyzeSpotifyTrack();

This method is typically faster and more accurate because it skips track search resolution.

Real-World Use Cases

Here are some practical ways developers use music analysis APIs:

Smart Playlist Generation

Group songs by:

  • Energy
  • BPM
  • Danceability
  • Mood

Harmonic Mixing

Use Camelot notation and musical keys to create smooth DJ transitions.

AI Music Recommendations

Recommend tracks with similar:

  • Tempo
  • Energy
  • Musical key
  • Acoustic profile

Audio-Reactive Visualizers

Sync graphics and effects to BPM and loudness values.

Music Discovery Apps

Create recommendation engines based on audio similarity instead of genre tags alone.

Final Thoughts

Music metadata is no longer just for streaming platforms. Developers can now build intelligent music tools using real-time audio analysis APIs with only a few lines of code.

Using Node.js and the Track Analysis API, you can quickly retrieve:

  • BPM
  • Musical key
  • Camelot notation
  • Energy and danceability
  • Advanced audio features

Whether you’re building a DJ app, recommendation engine, or AI music project, audio analysis opens the door to far richer music experiences.

Happy building 🎵


메타데이터
post_id
2c1bfbdd0acf
slug
node-js-tutorial-detect-bpm-and-musical-key-from-songs-2c1bfbdd0acf
url
https://medium.com/@soundnet717/node-js-tutorial-detect-bpm-and-musical-key-from-songs-2c1bfbdd0acf
canonical_url
https://medium.com/@soundnet717/node-js-tutorial-detect-bpm-and-musical-key-from-songs-2c1bfbdd0acf
author_url
https://medium.com/@soundnet717
status
ok
fetched_at
2026-06-10 22:22:12