Skip to content
Client
Server

Reference / @open-music-sdk/server

@open-music-sdk/server ​

The Apple Music API for code that runs on a server: Node, Cloudflare Workers, Bun, Deno, a command-line tool or an Electron main process. One import gives you a client that signs its own developer token, a route that hands your pages a token, and everything else an app uses from the other packages.

Installation ​

pnpm add @open-music-sdk/server

For a web page, use @open-music-sdk/browser. For a React app, use @open-music-sdk/react. A Next.js app uses this package on the server and react in its client components.

Documentation ​

Example ​

On your server:

ts
import { createServerClient, isAppleMusicError } from "@open-music-sdk/server";

// Create the client once, when the module loads. Load the key the way your app loads its other secrets.
const music = createServerClient({
  pem: process.env.APPLE_MUSIC_PRIVATE_KEY!,
  teamId: process.env.APPLE_MUSIC_TEAM_ID!,
  keyId: process.env.APPLE_MUSIC_KEY_ID!,
  storefront: "us",
});

const album = await music.catalog.getAlbum("1613600183"); // needs the developer token only

const listener = music.as(musicUserToken); // one listener's client, with the token your page sent you or the CLI printed
try {
  for await (const playlist of listener.user.listLibraryPlaylists()) console.log(playlist.attributes?.name);
} catch (e) {
  if (isAppleMusicError(e, "UserTokenInvalid")) askListenerToSignInAgain(); // Apple answered 403
  else throw e;
}

A listener who isn't signed in, or has no subscription, can get a 401 instead, which arrives as DeveloperTokenInvalid. Handle errors shows how to tell the two apart.

What's in it ​

ExportWhat it does
createServerClient(options)A client that signs its own developer token, with catalog, user and playlists on it. as(token) and forUser(userId) give you the same for one listener.
tokenEndpoint(options)A route handler that gives your pages a short-lived developer token
Everything elseWhat an app uses from the other packages, listed below

It re-exports these, under the names each package gives them:

FromNames
coreAppleMusicError, isAppleMusicError, createRateLimiter, parseToken, and the types
developer-tokenmintDeveloperToken, developerTokenMinter, developerTokenFetcher
user-tokenvalidateUserToken, acceptUserToken, userTokenIntake, MemoryUserTokenStore, KvUserTokenStore
util-artworkartworkImage, artworkUrl, artworkSrcSet, FORMATS
client-catalog, client-userEvery function, and catalog and user
lib-playlistsplaylists
validateEvery validator, under schemas, such as schemas.song
typesEvery generated type, such as tSong and tLibraryPlaylist

core's createClient and the tools the clients are built with aren't re-exported. To build your own client from the parts, install the packages themselves.

Namespaces ​

Interfaces ​

Type Aliases ​

Functions ​

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