Chris Mendez in Audio, Music Industry, Javascript, NodeJS, For Developers, Radio

How to get a 30s preview from Apple's API

This tutorial will show you how to get the 30 second music preview using Apple's API. The goal of this is to help developers script a process for dynamically accessing data.

Step 1 - Pick a Track

As a starting point, let's pick a demo track. Visit Apple's iTunes link maker and select a track you want to preview. For this demo, I will use Tom Misch's "The Journey".

Your search result will end with a profile page with an embed code and direct link down below.

Tom Misch - The Journey

Step 2 - Find the Apple / iTunes ID (formerly Adam ID)

Almost every database assigns some sort of unique identifier to each track. For example, MusicBrainz uses mbid, GraceNote uses musicid and All Music uses amg.

Apple Music/iTunes uses an identifier called adamid. Sometimes newer documentation will call it an Apple ID or an iTunes ID.

iTunes Link Maker will provide you with an adamid. All you need to do is copy and paste the final numbers at the end of any iTunes track.

For example, using the Link Maker tool above, this is the Direct Link for Tom Misch's "The Journey".

https://geo.itunes.apple.com/us/album/the-journey/id1023678446?i=1023678453&mt=1&app=music  

The URL above contains two ID's.

  • 1023678446 is the album ID.
  • 1023678453 is the track ID.

Note: If you want to learn which other identifiers the Apple Web Service API accepts, visit this document.

Step 3 - Lookup

Now that we have the unique ID to this specific song, we can now run an iTunes lookup to get the other bits of metadata such as Album Art and 30 second previews.

https://itunes.apple.com/us/lookup?id=1023678453

Step 4 - Parse Your Results

The iTunes lookup will give you all the metadata you need including the 30 second preview.

You can access the 30 second preview URL like this:

query.results.json.results.previewUrl  
{
    "query": {
        "count": 1,
        "created": "2017-06-18T21:05:42Z",
        "lang": "en-US",
        "diagnostics": {
            "publiclyCallable": "true",
            "url": {
                "execution-start-time": "1",
                "execution-stop-time": "162",
                "execution-time": "161",
                "content": "http://itunes.apple.com/us/lookup?id=1023678453"
            },
            "user-time": "162",
            "service-time": "161",
            "build-version": "2.0.137"
        },
        "results": {
            "json": {
                "resultCount": "1",
                "results": {
                    "wrapperType": "track",
                    "kind": "song",
                    "artistId": "893237941",
                    "collectionId": "1023678446",
                    "trackId": "1023678453",
                    "artistName": "Tom Misch",
                    "collectionName": "Beat Tape 2",
                    "trackName": "The Journey",
                    "collectionCensoredName": "Beat Tape 2",
                    "trackCensoredName": "The Journey",
                    "artistViewUrl": "https://itunes.apple.com/us/artist/tom-misch/id893237941?uo=4",
                    "collectionViewUrl": "https://itunes.apple.com/us/album/the-journey/id1023678446?i=1023678453&uo=4",
                    "trackViewUrl": "https://itunes.apple.com/us/album/the-journey/id1023678446?i=1023678453&uo=4",
                    "previewUrl": "http://a1299.phobos.apple.com/us/r30/Music7/v4/3a/a3/01/3aa30184-78e2-7030-c6e0-d33f3d3e21b7/mzaf_3396140578378232458.plus.aac.p.m4a",
                    "artworkUrl30": "http://is5.mzstatic.com/image/thumb/Music69/v4/09/e9/f9/09e9f9fb-ff71-59c4-d8fb-47cd51c65397/source/30x30bb.jpg",
                    "artworkUrl60": "http://is5.mzstatic.com/image/thumb/Music69/v4/09/e9/f9/09e9f9fb-ff71-59c4-d8fb-47cd51c65397/source/60x60bb.jpg",
                    "artworkUrl100": "http://is5.mzstatic.com/image/thumb/Music69/v4/09/e9/f9/09e9f9fb-ff71-59c4-d8fb-47cd51c65397/source/100x100bb.jpg",
                    "collectionPrice": "8.99",
                    "trackPrice": "1.29",
                    "releaseDate": "2015-08-28T07:00:00Z",
                    "collectionExplicitness": "notExplicit",
                    "trackExplicitness": "notExplicit",
                    "discCount": "1",
                    "discNumber": "1",
                    "trackCount": "12",
                    "trackNumber": "1",
                    "trackTimeMillis": "266820",
                    "country": "USA",
                    "currency": "USD",
                    "primaryGenreName": "Alternative",
                    "isStreamable": "true"
                }
            }
        }
    }
}

Step 5 - Success!

That's it! You now have a process for accessing 30 second previews.

One thing to note, Apple has a 20req/min limit on the Search API. If you want to do more, you will need to become an affiliate partner and use the Enterprise Partner Feed


Resources

API

  • Music API's is a list of music API's you might need to leverage to create the music app of your dreams.

Docs

Tools

  • iTunes link maker - This helps you find the Adam ID on a single track.
  • JSON Formatter is a Google Chrome extension that will make it much easier to read JSON within your browser.
  • Yahoo YQL + iTunes queries can help developers create a proper web API that can execute without cross-domain origin errors. Yahoo YQL can also create a JSONP response.

Answers