Last.fm Top Track API

A simple HTTP API that returns the top track for the most recent week from Last.fm.

Files

  • lastfm.ts - HTTP endpoint that fetches and returns the top track from the past 7 days
  • main.ts - Empty placeholder file

API Endpoint

GET /

Returns the most played track for user "pinjasaur" from the past 7 days.

Response Format:

{ "track": { "name": "Track Name", "artist": "Artist Name", "playcount": 42, "url": "https://www.last.fm/music/Artist/_/Track", "image": "https://lastfm.freetls.fastly.net/i/u/300x300/image.png" }, "period": { "from": "2026-01-09T16:50:28.000Z", "to": "2026-01-16T16:50:28.000Z" }, "user": "pinjasaur" }

Error Responses:

  • 500 - API key not configured or Last.fm API error
  • 404 - No tracks found for the past week

Configuration

Requires the LAST_FM_API_KEY environment variable to be set with a valid Last.fm API key.

Features

  • Uses Last.fm's predefined "7day" period for better reliability
  • Falls back to custom date range if needed
  • Returns the largest available album artwork
  • Includes caching headers (1 hour cache)
  • Proper error handling and TypeScript types