Skip to content
Client
Server

Reference / @open-music-sdk/lib-playlists

@open-music-sdk/lib-playlists ​

You probably don't want this package. Most apps install one integration and import everything from it: see Which package to install in the repository's README.

Apple's API can make a playlist in a listener's library and add tracks to its end. It can't take a track out, move one, or delete a playlist. This package does the work every app does around that: it finds tracks known from somewhere else in Apple's catalog, reads a playlist whole, works out what it's missing, and adds that without adding anything twice.

Most apps install server, browser or react instead, which put these verbs on music.playlists. Use this package to build your own client.

Installation ​

pnpm add @open-music-sdk/lib-playlists @open-music-sdk/core

Keep the two on the same version. See One version for every package.

Documentation ​

Example ​

On a server or in a page, where developerToken and musicUserToken are your tokens:

ts
import { createClient } from "@open-music-sdk/core";
import { playlists } from "@open-music-sdk/lib-playlists";

const lists = playlists(createClient({ developerToken, storefront: "us" }).as(musicUserToken)); // for one listener

// Find tracks known from elsewhere in Apple's catalog: by ISRC, or by name and artist.
const { tracks, skipped } = await lists.resolve([{ isrc: "GBAYE0601498" }, { name: "Yesterday", artistName: "The Beatles" }]);

// Work out what the playlist is missing. Nothing is asked of Apple: looking at the plan is the dry run.
const plan = lists.plan({ name: "Imported", description: "from my-app" }, tracks);

// Find or make the playlist, and add what it's missing to its end.
const { id, created, appended, verified } = await lists.apply(plan);

What's in it ​

One function, playlists(client), which gives a frozen object of four verbs. Each verb can be handed on by itself.

VerbWhat it doesWhat it asks Apple
resolve(descriptions, options?)Finds a song in the catalog for each description of a track, and hands back the ones it couldn't match with the songs its search foundThe catalog, with the developer token alone
read(id, options?)Reads a playlist of the listener's library whole, as plain JSON you can save as an exportThe library, with both tokens
plan(playlist, tracks)Works out what the playlist is missing, and what can't be doneNothing
apply(plan, options?)Finds or makes the playlist and adds what it's missing, sending each write onceThe library, with both tokens

read and apply need a client that holds the listener's Music User Token. resolve works with a client that has none, as long as the client or the call names a storefront.

TypesFor
tPlaylistsWhat playlists(client) gives
tTrackDescription, tResolvedTracks, tSkippedTrack, tResolveOptionsresolve
tPlaylistSnapshot, tPlaylistSnapshotTrack, tReadPlaylistOptionsread
tNewPlaylist, tPlaylistPlan, tPlaylistTrackplan
tAppliedPlan, tApplyOptions, tSettleOptionsapply
tAskOptions, tPaceOptions, tLimitOptions, tProgress, tStorefrontOptionThe options the verbs share

Interfaces ​

Functions ​

MIT licensed. Not affiliated with, endorsed by, or sponsored by Apple Inc. Apple Music and MusicKit are trademarks of Apple Inc.