Skip to content
Client
Server

Reference / @open-music-sdk/user-token

@open-music-sdk/user-token ​

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.

Everything around a listener's Music User Token except issuing it: asking Apple whether it works, taking it in from your page, and keeping it where core's forUser(userId) finds it. Only Apple's sign-in issues the token. This package starts once you hold the string. For your own account, the CLI's login gets one from the terminal.

Most apps install server, browser or react instead, which re-export what an app uses of this package. Use this one to build your own client.

Installation ​

pnpm add @open-music-sdk/user-token @open-music-sdk/core @open-music-sdk/developer-token

developer-token is for the example's developer token. Keep them all on the same version. See One version for every package.

Documentation ​

Example ​

On your server. sessionOf(req) and userId stand for your own session lookup:

ts
import { createClient } from "@open-music-sdk/core";
import { developerTokenMinter } from "@open-music-sdk/developer-token";
import { MemoryUserTokenStore, userTokenIntake } from "@open-music-sdk/user-token";

const store = new MemoryUserTokenStore();
const music = createClient({
  developerToken: developerTokenMinter({ pem: process.env.APPLE_MUSIC_PRIVATE_KEY!, teamId: process.env.APPLE_MUSIC_TEAM_ID!, keyId: process.env.APPLE_MUSIC_KEY_ID! }),
  userTokenStore: store,
});

// The route your page posts { "token": "..." } to. Apple checks the token, and only then does the store keep it.
export const POST = userTokenIntake(music, { store, userId: (req) => sessionOf(req) });

// In any later request, the listener's token is looked up in the store.
const playlists = await music.forUser(userId).request("v1/me/library/playlists");

What's in it ​

A verb does the thing once. A userToken… noun is something to hand on, such as a handler for your router. The stores are classes, which you give createClient as userTokenStore.

ExportWhat it does
validateUserToken(client, token, options?)Asks Apple whether the token works, and resolves to the listener's storefront
acceptUserToken(client, store, userId, token, options?)Checks the token with Apple, and only then stores it under userId
userTokenIntake(client, options)acceptUserToken behind a web-standard handler, (req: Request) => Promise<Response>
MemoryUserTokenStoreA store in the process's memory, gone on restart
KvUserTokenStore(kv, options?)A store over Workers KV, or anything with KV's get, put and delete
tValidateOptions, tIntakeOptions, tKvStoreOptionsThe options each of those takes
tUserTokenIntake, tKvNamespaceThe handler's type, and the part of a KV namespace the store uses
tUserTokenStorecore's interface for a store, for one of your own

Classes ​

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.