Last.FM TypeScript API Client

2.6.2 · active · verified Tue Apr 21

lastfm-ts-api is a TypeScript-first API client for the Last.FM API, currently stable at version 2.6.2. It provides strongly typed interfaces for interacting with various Last.FM API endpoints, including Album, Artist, Auth, Chart, Geo, Library, Tag, Track, and User APIs. The library maintains an active release cadence, frequently adding new features, improving error handling, and enhancing type definitions. A key differentiator is its comprehensive TypeScript support, ensuring parameter and response types are well-defined. It also supports integration with third-party scrobbling services like Libre.FM since version 2.3.0 and offers user-configurable request timeouts. The library transitioned to a pure ESM module in version 2.0.0, aligning with modern JavaScript practices.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing the LastFMTrack client and fetching the top tracks for an artist, handling API key retrieval from environment variables.

import { LastFMTrack } from 'lastfm-ts-api';

const API_KEY = process.env.LASTFM_API_KEY ?? '';
const SECRET = process.env.LASTFM_SHARED_SECRET ?? ''; // Optional for some endpoints
const SESSION_KEY = process.env.LASTFM_SESSION_KEY ?? ''; // Optional for some endpoints

if (!API_KEY) {
  console.error('LASTFM_API_KEY environment variable is required.');
  process.exit(1);
}

async function getTopTracks() {
  try {
    const trackClient = new LastFMTrack(API_KEY, SECRET, SESSION_KEY);
    const topTracks = await trackClient.getTopTracks({
      artist: 'Radiohead',
      limit: 5
    });
    console.log(`Top tracks by Radiohead:`);
    topTracks.tracks.track.forEach((track: any) => {
      console.log(`- ${track.name} (Playcount: ${track.playcount})`);
    });
  } catch (error) {
    console.error('Error fetching top tracks:', error);
  }
}

getTopTracks();

view raw JSON →