Building an Apple Music Playlist Creator for Billie Eilish

Using the Apple Music API, MusicKit JS, and Nuxt

Image for post
Image for post
import axios from 'axios'export default{
routes: function(callback) {
let apple = axios.create({
baseURL: 'https://api.music.apple.com/v1',
headers: {
'Authorization': 'Bearer YOUR_APPLE_MUSIC_DEVELOPER_TOKEN'
}
}
// Get all artist album ids from Apple Music
return apple.get('/catalog/us/artists/ARTIST_ID/albums')
.then(res => {
let ids = res.data.data.map(album => album.id)
// Get all albums (from ids) from Apple Music
return apple.get(`/catalog/us/albums?ids=${ids}`)
.then(res => {
// Map all tracks from albums into required format
let tracks = [].concat(...res.data.data.map(album => {
return album.relationships.tracks.data.map(track => {
return {
id: track.id,
name: track.attributes.name
}
}
})
})
callback(null, [{
route: '/',
payload: tracks
}])
})
}
}
async asyncData({ app, payload, env }) {
if (payload) {
return {
tracks: payload
}
}
}
computed: {
selected() {
return this.tracks.filter(track => track.selected)
.map(track => {
return {
id: track.id,
type: 'songs'
}
})
}
}
music.authorize()
.then(() => {
let apple = axios.create({
baseURL: 'https://api.music.apple.com/v1',
headers: {
'Authorization': `Bearer YOUR_APPLE_MUSIC_DEVELOPER_TOKEN`,
'Music-User-Token': `MUSIC_USER_TOKEN`
}
})
apple.post('/me/library/playlists', {
attributes: {
name: 'Billie Eilish',
description: 'Here are my favorite Billie Eilish tracks'
},
relationships: {
tracks: {
data: this.selected
}
}
})
.then(res => {
// successfully created playlist
})
.catch(error => {
// error creating playlist
})
})
.catch(error => {
// error authorizing
})

Written by

I develop websites for rock 'n' roll bands and get paid in sex and drugs. Previously Silva Artist Management, SoundCloud, and Songkick. Currently: Available

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store