Important
Artwork inspired by ElysiaJS.
npm install @andreafspeziale/mxm-clientyarn add @andreafspeziale/mxm-clientpnpm add @andreafspeziale/mxm-clientNote
To effectively interact with the Musixmatch API you need a valid API key.
The SDK implements most of the Musixmatch API endpoints, providing a strongly typed and easy-to-use interface for developers. It handles authentication, request construction and response parsing allowing you to focus on building your application.
import { MxmClient } from '@andreafspeziale/mxm-client';
const mxmClient = new MxmClient();
const track = await mxmClient.trackGet({
query: { track_isrc: 'USUM72005901', apiKey: 'your-api-key' },
});import { MxmClient } from '@andreafspeziale/mxm-client';
const mxmClient = new MxmClient({
config: {
apiKey: 'your-api-key',
},
});
const track = await mxmClient.trackGet({
query: { track_isrc: 'USUM72005901' },
});A Pino basic logger instance is created internally when
enableLogis set totrue.
import { MxmClient } from '@andreafspeziale/mxm-client';
const mxmClient = new MxmClient({
config: {
apiKey: 'your-api-key',
enableLog: true,
defaultLoggerConfig: {
formatters: {
level: (label) => {
return { level: label };
},
},
},
},
});
const track = await mxmClient.trackGet({
query: { track_isrc: 'USUM72005901' },
});A Pino logger instance is provided and logging can be enabled/disabled via configuration.
import { pino } from 'pino';
import { MxmClient } from '@andreafspeziale/mxm-client';
const logger = pino({
formatters: {
level: (label) => {
return { level: label };
},
},
});
const mxmClient = new MxmClient({
logger,
config: {
apiKey: 'your-api-key',
enableLog: true,
},
});
const track = await mxmClient.trackGet({
query: { track_isrc: 'USUM72005901' },
});By default the client targets
https://api.musixmatch.com. UsebaseUrlto point the client at a different host (e.g. staging environment or a custom Kubernetes entrypoint).
import { MxmClient } from '@andreafspeziale/mxm-client';
const mxmClient = new MxmClient({
config: {
apiKey: 'your-api-key',
baseUrl: 'https://staging.musixmatch.com',
},
});
const track = await mxmClient.trackGet({
query: { track_isrc: 'USUM72005901' },
});When
disableStatusCodeValidationis set totrue, the client will not throw on unexpected HTTP status codes allowing the response body validation to still run. Musixmatch APIs always return a200status code. In case of an error, it will still be200and the error details will be included in the response body. This option can be useful if for any odd reason the status code is not200.
import { MxmClient } from '@andreafspeziale/mxm-client';
const mxmClient = new MxmClient({
config: {
apiKey: 'your-api-key',
disableStatusCodeValidation: true,
},
});
const track = await mxmClient.trackGet({
query: { track_isrc: 'USUM72005901' },
});import { MxmClient } from '@andreafspeziale/mxm-client';
const mxmClient = new MxmClient({
config: { apiKey: 'your-api-key' },
});
const track = await mxmClient.trackGet({
query: { track_isrc: 'USUM72005901' },
options: { disableStatusCodeValidation: true },
});The SDK supports extending input types via generics for advanced use cases where the API accepts fields beyond the documented ones.
import { MxmClient, type TrackGetQuery } from '@andreafspeziale/mxm-client';
interface MyTrackGetQuery extends TrackGetQuery {
custom_param: string;
}
const mxmClient = new MxmClient({
config: { apiKey: 'your-api-key' },
});
const track = await mxmClient.trackGet<MyTrackGetQuery>({
query: {
track_isrc: 'USUM72005901',
custom_param: 'value',
},
});import {
MxmClient,
type TrackLyricsFingerprintPostQuery,
type TrackLyricsFingerprintPostBody,
} from '@andreafspeziale/mxm-client';
interface MyFingerprintPostBody extends TrackLyricsFingerprintPostBody {
settings: { algorithm: string };
}
const mxmClient = new MxmClient({
config: { apiKey: 'your-api-key' },
});
const result = await mxmClient.trackLyricsFingerprintPost<
TrackLyricsFingerprintPostQuery,
MyFingerprintPostBody
>({
body: {
text: "Fratelli d'Italia...",
settings: { algorithm: 'raw' },
},
});For full runtime safety with extended response types, you can provide a custom schema via options.responseSchema. The schema describes only the response body β the client handles the legacy response wrapper (message.header + message.body) internally. The return type is automatically inferred from your schema.
Note
The responseSchema option accepts any schema implementing the Standard Schema interface (Zod, Valibot, ArkType, etc.).
Important
TypeScript does not support partial generic inference. If you need to specify generics for query or body extension, you must also pass typeof yourSchema as the last generic parameter.
import { z } from 'zod';
import {
MxmClient,
type TrackGetQuery,
mxmClientTrackGetResponseSchema,
} from '@andreafspeziale/mxm-client';
// Extend the base body schema with custom fields
const myTrackGetResponseSchema = mxmClientTrackGetResponseSchema.extend({
my_custom_field: z.string(),
});
interface MyTrackGetQuery extends TrackGetQuery {
custom_param: string;
}
const mxmClient = new MxmClient({
config: { apiKey: 'your-api-key' },
});
// Pass typeof schema as the last generic when extending query/body types
const track = await mxmClient.trackGet<MyTrackGetQuery, typeof myTrackGetResponseSchema>({
query: { track_isrc: 'USUM72005901', custom_param: 'value' },
options: { responseSchema: myTrackGetResponseSchema },
});
track.message.body.my_custom_field; // typed AND validated at runtime
track.message.body.track.track_name; // base fields are still accessibleThe SDK provides an .unsafe accessor that skips response body validation, allowing you to extend output types via generics for cases where the API returns fields not covered by the built-in schemas.
Caution
In unsafe mode, the TypeScript type is not guaranteed at runtime. Use this when you know the API returns additional fields for your account or use case.
import {
MxmClient,
type TrackGetQuery,
type MxmClientTrackGetResponse,
} from '@andreafspeziale/mxm-client';
interface MyTrackGetResponse extends MxmClientTrackGetResponse {
my_custom_field: string;
}
const mxmClient = new MxmClient({
config: { apiKey: 'your-api-key' },
});
const track = await mxmClient.unsafe.trackGet<TrackGetQuery, MyTrackGetResponse>({
query: { track_isrc: 'USUM72005901' },
});
track.message.body.my_custom_field; // typed, not validated at runtime
track.message.body.track_name; // inherited from base typeimport {
MxmClient,
type TrackGetQuery,
type MxmClientTrackGetResponse,
} from '@andreafspeziale/mxm-client';
interface MyTrackGetQuery extends TrackGetQuery {
custom_param: string;
}
interface MyTrackGetResponse extends MxmClientTrackGetResponse {
custom_output: number;
}
const mxmClient = new MxmClient({
config: { apiKey: 'your-api-key' },
});
const track = await mxmClient.unsafe.trackGet<MyTrackGetQuery, MyTrackGetResponse>({
query: {
track_isrc: 'USUM72005901',
custom_param: 'value',
},
});matcherLyricsGet(matcher.lyrics.get)matcherSubtitleGet(matcher.subtitle.get)matcherTrackGet(matcher.track.get)
trackGet(track.get)trackSearch(track.search)trackLyricsGet(track.lyrics.get)trackSubtitleGet(track.subtitle.get)trackRichSyncGet(track.richsync.get)trackSnippetGet(track.snippet.get)
trackLyricsTranslationGet(track.lyrics.translation.get)trackSubtitleTranslationGet(track.subtitle.translation.get)languagesGet(languages.get)
artistGet(artist.get)artistSearch(artist.search)artistAlbumsGet(artist.albums.get)
albumGet(album.get)albumTracksGet(album.tracks.get)
chartTracksGet(chart.tracks.get)chartArtistsGet(chart.artists.get)musicGenresGet(music.genres.get)
trackLyricsFingerprintPost(track.lyrics.fingerprint)
trackLyricsAnalysisGet(track.lyrics.analysis.get)trackLyricsAnalysisSearch(track.lyrics.analysis.search)
For more comprehensive usage examples including TypeScript and JavaScript (ESM/CJS), check the examples/ directory.
Note
To keep consistency over time and to be sure each endpoint returns the expected result, e2e tests are run against the actual Musixmatch APIs when a valid API key is provided (environment variable MXM_API_KEY). Instead e2e will be skipped.
pnpm test
- Author - Andrea Francesco Speziale
mxm-client MIT licensed.
