Skip to content

How to create a JWT Token for Apple Music Kit #158

Answered by panva
muuvmuuv asked this question in Q&A
Discussion options

You must be logged in to vote

Apple's documentation says encrypt, but they mean sign. ES256 is a DSA scheme, not encryption one. The tokens are signed, not encrypted.

You want to use the jose/jwt/sign module instead.

You'll also have to pass the private key through Node's crypto.createPrivateKey to end up with a KeyObject instance that is required as input.

const keyObject = crypto.createPrivateKey(privateKey); // cache this value, you'll be re-using it.

const jwt = new SignJWT({})
  .setProtectedHeader({
    kid: 'ABC123DEFG',
    alg: 'ES256',
  })
  .setIssuer('DEF123GHIJ')
  .setIssuedAt()
  .setExpirationTime('1 week')
  .sign(keyObject)

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@muuvmuuv
Comment options

@panva
Comment options

@muuvmuuv
Comment options

@panva
Comment options

Answer selected by muuvmuuv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants