MusicKit TypeScript Types

1.2.4 · active · verified Tue Apr 21

This package provides TypeScript type definitions for Apple's MusicKit JS library. It enables developers to use TypeScript with the MusicKit JS API, offering compile-time type checking and improved IDE autocompletion, which is crucial as the official MusicKit JS library might lack comprehensive native types. The current stable version is 1.2.4. As a type definition package, its release cadence is typically tied to updates or community needs concerning the MusicKit JS library itself rather than following a fixed schedule. It serves as a community-driven effort to enhance the developer experience for those integrating Apple Music into web applications using TypeScript.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install the types and configure `tsconfig.json`. It then shows how to access the global `MusicKit` object and use its types for initialization and basic player interaction, assuming the MusicKit JS library is loaded in the browser.

npm install --save-dev musickit-typescript

// Then, update your tsconfig.json:
// Make sure 'node_modules/musickit-typescript' is included in 'typeRoots'
// alongside 'node_modules/@types' to ensure TypeScript finds the definitions.
{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/musickit-typescript",
      "node_modules/@types"
    ],
    "target": "es2018",
    "module": "esnext",
    "lib": ["dom", "esnext"]
  },
  "include": ["src/**/*.ts"]
}

// Example usage in your TypeScript file (assuming MusicKit JS is loaded in HTML):
// Make sure to define your developer token or get it from your backend
declare const MusicKit: MusicKit.MusicKitInstance;
const developerToken = process.env.APPLE_MUSIC_DEV_TOKEN ?? 'YOUR_DEVELOPER_TOKEN_HERE';

async function initMusicKit() {
  try {
    await MusicKit.configure({
      developerToken: developerToken,
      app: {
        name: 'My Awesome App',
        build: '1.0.0'
      }
    });
    const music = MusicKit.getInstance();
    console.log('MusicKit initialized successfully:', music);
    if (music.isAuthorized) {
      console.log('User is authorized.');
    } else {
      console.log('User is not authorized. Requesting authorization...');
      await music.authorize();
      console.log('User authorized!');
    }
    const player: MusicKit.Player = music.player;
    console.log('Current playback state:', player.playbackState);
  } catch (error) {
    console.error('Failed to initialize MusicKit:', error);
  }
}

initMusicKit();

view raw JSON →