Is there an Apple Music API which returns a users recently played tracks
Apple Music API - return a users recently played tracks
No, not it's job.
Use avfoundation/avplayer
https://developer.apple.com/documentation/avfoundation/avplayer
There is an endpoint now https://developer.apple.com/documentation/applemusicapi/get_recently_played_tracks