Getting Started
Prerequisites
Section titled “Prerequisites”You need a paid Apple Developer Program membership ($99/yr).
Generate a MusicKit Key
Section titled “Generate a MusicKit Key”-
Go to the Apple Developer Certificates, Identifiers & Profiles page.
-
Click Keys → Create a new key.
-
Enable MusicKit under Services.
-
Download the
.p8file. You can only download it once, so keep it safe. -
Note the Key ID and your Team ID (found in your account’s membership details).
Installation
Section titled “Installation”npm install node-musickit-apiyarn add node-musickit-apibun add node-musickit-apiKey Security
Section titled “Key Security”Never hardcode your credentials. Use environment variables or a gitignored file:
// ✅ Recommended: environment variablesimport { MusicKit } from "node-musickit-api"
const musicKit = new MusicKit({ key: { id: process.env.MUSICKIT_KEY_ID!, teamId: process.env.MUSICKIT_TEAM_ID!, p8: process.env.MUSICKIT_P8! }})// ✅ Alternative: read from a gitignored fileimport { MusicKit } from "node-musickit-api"
const p8 = await Bun.file("./AuthKey_XXXXXXXX.p8").text()
const musicKit = new MusicKit({ key: { id: "YOUR_KEY_ID", teamId: "YOUR_TEAM_ID", p8 }})Add the key file path to .gitignore:
AuthKey_*.p8Authenticate
Section titled “Authenticate”await musicKit.auth()Call auth() before making any requests. It generates a JWT signed with your private key that expires every 180 days. Call auth() again to refresh it.
Test Authentication
Section titled “Test Authentication”Verify your token is valid without making a catalog request:
const status = await musicKit.testAuth()// Returns 200 if the token is validtestAuth() hits the Apple Music API’s /test endpoint. A 200 response means your credentials are working.
Custom Base URL
Section titled “Custom Base URL”The client sends requests to https://api.music.apple.com/v1 by default. Override baseUrl to route through a proxy or local server:
const musicKit = new MusicKit({ key: { id, teamId, p8 } })musicKit.baseUrl = "http://localhost:8080/v1"This is useful for debugging, caching proxies, or environment-specific setups.