Node.js Client for Apple Music
The Node.js client for Apple Music is a client library for Apple Music API built with TypeScript.
Currently this library provides only a feature to fetch a single resource such as album, artists, songs, playlists, music videos, or stations.
Note that this does not related to MusicKit JS, which is the official library of Apple Music for web apps.
Requirements
You need to be a member of the Apple Developer Program and obtain your Apple Music developer token with some tools such as apple-music-token-node.
Installation
Install the package with:
npm install --save @yujinakayama/apple-music
Type definitions for TypeScript are also included in the package.
Usage
You need to instantiate a client with your developer token:
import { Client } from "@yujinakayama/apple-music";
const client = new Client({ developerToken: 'YOUR_DEVELOPER_TOKEN' });
Fetching Single Resource
You can fetch album, artists, songs, playlists, music videos, or stations with client.RESOURCES.get()
:
async function main() {
const client = new Client({ developerToken: 'YOUR_DEVELOPER_TOKEN' });
const playlistID = 'pl.5ee8333dbe944d9f9151e97d92d1ead9';
const response = await client.playlists.get(playlistID, { storefront: 'us' });
const playlist = response.data[0];
console.log(playlist.attributes!);
}
main();
Handling Errors
You can handle API errors as:
async function main() {
const client = new Client({ developerToken: 'YOUR_DEVELOPER_TOKEN' });
try {
const response = await client.playlists.get('nosuchplaylist', { storefront: 'us' });
} catch (error) {
console.log(error.httpStatusCode);
console.log(error.response.errors[0])
}
}
}
main();
Specifying Storefront
You must specify storefront either:
defaultStorefront
parameter in the constructorstorefront
parameter in get()
const client = new Client({
developerToken: 'YOUR_DEVELOPER_TOKEN',
defaultStorefront: 'us'
});
const response = await client.playlists.get(playlistID);
const client = new Client({
developerToken: 'YOUR_DEVELOPER_TOKEN',
});
const response = await client.playlists.get(playlistID, { storefront: 'us' });
Specifying Language Tag
You can specify language tag either:
- Unspecify for the storefront's default language
defaultLanguageTag
parameter in the constructorlanguageTag
parameter in get()
const client = new Client({
developerToken: 'YOUR_DEVELOPER_TOKEN',
defaultStorefront: 'jp'
});
const response = await client.playlists.get(playlistID);
const client = new Client({
developerToken: 'YOUR_DEVELOPER_TOKEN',
defaultStorefront: 'jp',
defaultLanguageTag: 'en-US'
});
const response = await client.playlists.get(playlistID);
const client = new Client({
developerToken: 'YOUR_DEVELOPER_TOKEN',
defaultStorefront: 'jp',
});
const response = await client.playlists.get(playlistID, { languageTag: 'en-US' });
License
Copyright (c) 2020 Yuji Nakayama
See the LICENSE.txt for details.