MusicBrainz API Client
The `musicbrainz-api` package provides a TypeScript-first client for interacting with the MusicBrainz web service, enabling both reading detailed metadata and submitting new information to the database. Currently at stable version 1.2.0, the library maintains an active release cadence with updates typically occurring every few weeks or months. Key differentiators include its robust support for retrieving various entity types, comprehensive TypeScript definitions for enhanced developer experience, and intelligent request throttling that automatically adheres to MusicBrainz API rate limits, including retries for rate-limit hits. It simplifies the required application identification (User-Agent) by prompting for `appName`, `appVersion`, and `appContactInfo` during client configuration. This library exclusively uses ECMAScript Modules (ESM) since version 8, requiring Node.js 16 or higher for usage.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import the `musicbrainz-api` package in a CommonJS module, but the library is pure ESM.fixChange your import statement to `import { MusicBrainzApi } from 'musicbrainz-api';` and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`). -
Error searching for artist: MusicBrainzApiError: Bad Request (status: 400)
cause The MusicBrainz API requires a User-Agent header, which is constructed from `appName`, `appVersion`, and `appContactInfo`. This error indicates one of these was likely missing or invalid.fixWhen initializing `MusicBrainzApi`, ensure all three fields (`appName`, `appVersion`, `appContactInfo`) are provided and valid, e.g., `new MusicBrainzApi({ appName: 'MyApp', appVersion: '1.0.0', appContactInfo: 'me@example.com' })`. -
UnhandledPromiseRejectionWarning: MusicBrainzApiError: Too Many Requests (status: 429)
cause Your application has sent too many requests in a short period, exceeding MusicBrainz's rate limits.fixReview your application's request frequency. While the library implements retries, sustained high volume can still trigger this. Ensure `appContactInfo` is valid. Consider spacing out requests or implementing custom back-off strategies if necessary. -
Cannot find name 'Artist'.
cause In TypeScript, the `Artist` type was imported using a regular `import` statement instead of `import type`, or the import path was incorrect.fixFor type-only imports, use `import type { Artist } from 'musicbrainz-api';`. If `Artist` is used as a value, ensure it's exported as such (though typically it's only a type).
Warnings
- breaking Starting from version 8.0.0, `musicbrainz-api` is a pure ECMAScript Module (ESM) and no longer supports CommonJS `require()`. This also elevates the minimum required Node.js version to 16 or higher.
- gotcha MusicBrainz API clients are required to identify their application via the User-Agent header. Failure to provide `appName`, `appVersion`, and `appContactInfo` when initializing `MusicBrainzApi` will result in HTTP 400 Bad Request errors.
- breaking Error reporting for `400 Bad Request` responses was improved in `v1.1.0`. Code relying on specific error message formats or structures for 400-level errors in older versions may behave differently.
- gotcha While the library implements intelligent throttling and retries on rate limit hits, excessive or misconfigured requests can still lead to temporary IP bans or persistent `429 Too Many Requests` errors from the MusicBrainz server. Ensure `appContactInfo` is valid for communication.
- breaking Prior to version 0.24.0, the `browse` function was more restrictive, only allowing a single MusicBrainz ID (MBID) per entity. Version 0.24.0 introduced new overloads allowing more flexible usage with optional `inc` parameters.
Install
-
npm install musicbrainz-api -
yarn add musicbrainz-api -
pnpm add musicbrainz-api
Imports
- MusicBrainzApi
const MusicBrainzApi = require('musicbrainz-api')import { MusicBrainzApi } from 'musicbrainz-api' - Artist
import { Artist } from 'musicbrainz-api'import type { Artist } from 'musicbrainz-api' - ReleaseIncludes
import type { ReleaseIncludes } from 'musicbrainz-api'
Quickstart
import { MusicBrainzApi } from 'musicbrainz-api';
import type { Artist } from 'musicbrainz-api';
const client = new MusicBrainzApi({
appName: 'MyAwesomeApp',
appVersion: '1.0.0',
appContactInfo: 'mailto:me@example.com' // Required by MusicBrainz API
});
async function searchArtist(query: string) {
try {
console.log(`Searching for artist: ${query}...`);
const result = await client.searchArtist(query);
if (result.artists && result.artists.length > 0) {
const firstArtist: Artist = result.artists[0];
console.log(`Found artist: ${firstArtist.name} (MBID: ${firstArtist.id})`);
if (firstArtist.area) {
console.log(` Area: ${firstArtist.area.name}`);
}
if (firstArtist['life-span']) {
console.log(` Life Span: ${firstArtist['life-span'].begin || 'Unknown'} - ${firstArtist['life-span'].end || 'Present'}`);
}
} else {
console.log('No artists found.');
}
} catch (error) {
console.error('Error searching for artist:', error);
}
}
searchArtist('Radiohead');
searchArtist('Pink Floyd');