Skip to content

jansanti/applemusic-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Important

This repository contains the server code that makes the API work. The Apple Shortcut is required to automatically send listening data from your device to the API—keep reading!

The Hacky Apple Music API

The in-depth story on why and how I built this is on this blog read.

This Deno API receives the Apple Music listening activity your device sends via Shortcut and allows you to use it anywhere. It provides the current track name, artist, album, album artwork, and duration.

There is no public deployment for the API as it is meant to be self-deployed for your privacy.

Setting up the Server

Make sure wherever you deploy can use Deno and has it installed.

You'll need to configure the following environment variables to run the server:

  • AMUSIC_HOST: The host the server will attempt to run on. Defaults to 0.0.0.0 if empty.
  • AMUSIC_PORT: The port the server will attempt to listen on. Defaults to 3000 if empty.
  • AMUSIC_API_KEY: You must set a secret key that will be used to authenticate requests to the /now-playing route. Anyone with this key can read and update listening data.
  • AMUSIC_ALLOWED_ORIGINS: Comma-delimited list of allowed request origins.

Here's an example .env file:

AMUSIC_HOST=0.0.0.0
AMUSIC_PORT=3000
AMUSIC_API_KEY="Your secret"
AMUSIC_ALLOWED_ORIGINS="http://localhost:3000,https://example.com"

Once these are set, you can start the server using the following command:

deno --allow-net --allow-env src/server.ts

You can test that everything is working with the /health route:

curl https://HOST:PORT/health

The optimal response is {ok:true}.

You can set this up easily on a provider like Railway or self-host on something like a Raspberry Pi as I did.

Setting Up the Shortcut

To get started, you'll first need to set up the Apple Shortcut and Automation on your iPhone.

Note

The shortcut requires that you have a Dropbox account to link. A free account works just fine.

First, follow this link to add to your Shortcuts library.

Next, you need to set your server information so the Shortcut can communicate with the API:

  • Open the Shortcut for editing.
  • Replace the text below the first comment with your server's URL.
    • Make sure to include the /now-playing route.
    • Example: https://music-api.example.com/now-playing.
  • Replace the text below the second comment with the secret key you set in your server environment.
  • Name the Shortcut something clear like "Update Now Playing".

This is all you need to change in the Shortcut.

Automating the Shortcut

To start sending listening data to the API, you need to automate the Shortcut:

  • In the Shortcuts app, go to the Automations tab.
  • Create a new Automation.
    • Set the condition to "App".
    • Choose the "Music" app.
    • Make sure to check both "Opened" and "Closed".
    • Change "Run after Confirmation" to "Run Immediately".
    • Turn off "Notify when Run".
  • In the Automation actions, set your "Update Now Playing" shortcut to run.

Getting Listening Data

Whenever you open or close Apple Music, the Shortcut will send what is currently playing to the API. If nothing is sent after the duration of the last track received, the API will treat it as if the track has ended and nothing is playing.

To get the data, make a GET request to your server's /now-playing route. You must pass your API key in the headers as x-api-key. Here's an example with CURL:

curl -X GET "https://HOST:PORT/now-playing" \
  -H "x-api-key: <your api key>"

This should result in something like this:

{
  "is_playing": true,
  "title": "Song",
  "album": "Album",
  "artist": "Artist",
  "image": "https://www.dropbox.com/scl/fi/....",
  "duration": "02:30",
  "duration_seconds": 150,
  "receivedAt": "<iso string>"
}

Contributing

Feel free to open issues and pull requests with new features or bug fixes.

If the change requires modifying the Shortcut, include a link to your version for review and addition to the main Shortcut.

About

🎧 A hacky Apple Music API for listening activity.

Topics

Resources

Stars

Watchers

Forks

Contributors