As part of WWDC 2018, Apple has announced the release of MusicKit JS for the web, allowing developers to play full songs from Apple Music with a little bit of JavaScript. Here’s a demo of that. Their simple guide does a decent job of outlining the core functionality of the library from configuring your player to authenticating users. In order to make any of this work, you must first create an Apple Music API token. This is where their documentation takes a turn for the complicated… at least for me. And while I quickly found a few libraries that could generate this token for me, it was the Node script I found below that actually worked for API calls and authentication.

This script requires three elements:

Team Id — available on the Developer console by clicking “Membership” in the left column Private Key — downloadable as a .p8 file after following these instructions to create your MusicKit identifier and private key Key Id — also available by following the instructions above or accessible anytime in “Certificates, Identifiers & Profiles” on the Developer console. It should also be part of the file name of your private key. 👌🏻

Once you have these three elements ready. Download or compose the following code as musickit-token-encoder.js and make sure it’s in the same directly as your .p8 file. Make sure the filename of privateKey matches that of your .p8 file. Then simply adjust the teamId and keyId to reflect your elements above. The script will then spit out an API token you can use to power your MusicKit JS app.

Hopefully this saves you a bit of time. I’ve also created a demo player and demo custom player on Codepen you should check out. Happy hacking!

Update 6/6:

I’ve noticed a cookie issue when arriving to a previously authenticated experience and have reported it on the Apple Developer forums. Try using an incognito window if you face the same issue.