npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-apple-music

v1.2.2

Published

Simple library to fetch Apple Music data without an account

Downloads

389

Readme

node-apple-music

Simple, powerful and flexible library with no dependencies to search and fetch music data from Apple Music, no account needed.

Installing

npm i node-apple-music

Usage

Authenticating

Before interacting with the library, you need to either fetch or provide an access token.

// Fetch and remember an anonymous access token
const token = await fetchToken();

// Provide an access token
setToken(token);

You only need to call one or the other, and the library will use the token for all subsequent requests.

In theory you could also use an official Apple Music developer token, but I haven't tested this.

Fetching by ID

await fetchSong(576655098);

{
    id: "576655098",
    type: "songs",
    href: "/v1/catalog/us/songs/576655098?l=en-US",
    attributes: {
        name: "The Lazy Song",
        url: "https://music.apple.com/us/album/the-lazy-song/576654788?i=576655098",
        artistName: "Bruno Mars",
        albumName: "Doo-Wops & Hooligans",
        durationInMillis: 189110,
        releaseDate: "2010-10-04",
        artwork: {
            url: "https://is5-ssl.mzstatic.com/image/thumb/Music114/v4/52/b1/45/52b1452b-229e-78db-231b-7b43fa0077cc/075679956491.jpg/{w}x{h}bb.jpg",
            width: 1425,
            height: 1425,
            // ...
        },
        discNumber: 1,
        trackNumber: 5,
        isrc: "USAT21206919",
        genreNames: ["Pop", "Music"],
        // ...and more!
    },
    relationships: {
        albums: {
            href: "/v1/catalog/us/songs/576655098/albums?l=en-US",
            data: [{...}] // album object
        },
        artists: {
            href: "/v1/catalog/us/songs/576655098/artists?l=en-US",
            data: [{...}] // artist objects
        }
    }
}

Too see the complete list of available fields, check out the Apple Music API docs.

fetchAlbum(), fetchArtist() and fetchPlaylist() work the same way.

These functions can also take a second options parameter, with the following attributes:

  • fetchArtists: include additional artist data. For fetchSong() and fetchAlbum() only, defaults to true.
  • fetchAlbum: include additional album data. For fetchSong() only, defaults to true.

To fetch data about other object types, such as music videos or record labels, use fetchMetadata().

To fetch by ISRCs or UPCs, you can use fetchIsrc() and fetchUpc().

To fetch additional data using an href, use doRequest().

Searching

await search("the lazy song");

{
    top: [{...}, ...],
    song: [{...}, ...],
    album: [{...}, ...],
    artist: [{...}, ...],
    playlist: [{...}, ...]
}

You can also pass an options object as the second argument, with the following properties:

  • types: a comma-separated list of types to search for, defaults to "songs,albums,artists,playlists"
  • limit: how many of each type to fetch, defaults to 25
  • includeTop: fetch top results across all types, defaults to true

suggestions() can be used to fetch search suggestions:

await suggestions("the lazy");

[
    {kind: "terms", searchTerm: "the lazy song"},
    {kind: "terms", searchTerm: "bruno mars the lazy song"},
    {kind: "terms", searchTerm: "the lazys"},
    {kind: "topResults", content: {...}}, // track object
    {kind: "topResults", content: {...}},
    // ...
]

Additional info

Artwork URLs are not ready to use as-is, you need to pass the artwork object to formatArtworkUrl():

formatArtworkUrl(song.attributes.artwork);
formatArtworkUrl(song.attributes.artwork, {size: 1000, format: "webp"});

Most functions take an options parameter. In addition to function-specific options, here are the options that they all support:

  • countryCode: country code to use. Defaults to "us".
  • lang: request language. Doesn't seem to change anything, use countryCode instead. Defaults to "en-US".
  • noAuth: Don't throw an error when trying to do a request without having an access token. Defaults to false.
  • returnReq: return the raw request object, with the HTTP status code, headers and unparsed body.
  • returnJson: return the unmodified JSON sent by Apple Music.
  • params: dictionary of additional url-encoded parameters to include in the request
  • headers: dictionnary of additional headers to include in the request