Registry / http-networking / lastfm-ts-api

lastfm-ts-api

JSON →
library2.6.2jsnpmunverified

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.

npm install lastfm-ts-api
INSTALL
IMPORT
SIG · LASTFM-TS-API
L
lastfm-ts-api
http-networkingjavascriptv2.6.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

LastFMTrack
import { LastFMTrack } from 'lastfm-ts-api';
const { LastFMTrack } = require('lastfm-ts-api');
The package became pure ESM in v2.0.0. CommonJS 'require' is not supported.
LastFMAuth
import { LastFMAuth } from 'lastfm-ts-api';
import LastFMAuth from 'lastfm-ts-api';
Classes are exposed as named exports, not default exports.
LastFMAlbum
import { LastFMAlbum } from 'lastfm-ts-api';
const album = new (require('lastfm-ts-api')).LastFMAlbum(...);
Directly destructure named exports for clarity and correct ESM usage.

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();
Debug
Known issues
breakingVersion 2.0.0 introduced a significant breaking change, making the package pure ESM. This means CommonJS `require()` statements will no longer work, and you must use ES module `import` syntax.
fix
Migrate all `require()` statements to `import` statements. Ensure your project's `package.json` has `"type": "module"` or uses `.mjs` file extensions for ESM files.
affects: >=2.0.0
breakingWith version 2.0.0, some internal types were renamed to improve consistency and clarity. This may cause type errors in existing TypeScript projects.
fix
Consult the library's official documentation or your IDE's type hints to update any affected type names in your codebase.
affects: >=2.0.0
gotchaThe response structure for the `track.scrobble` endpoint changed in v2.3.0 when scrobbling a single track. It now returns an object instead of a plain string or array of objects.
fix
Update your code to correctly parse the `track.scrobble` response, expecting an object for single track scrobbles.
affects: >=2.3.0
gotchaThe `User` API's `getInfo` method was updated in v2.2.0 to include the correct way of signing requests using the session secret. Previous implementations might fail authentication.
fix
Ensure that `user.getInfo` calls correctly pass the session secret for signing as per the updated API requirements.
affects: >=2.2.0
gotchaError handling was improved in versions 2.3.0 and 2.5.0 to account for API responses possibly containing an error object instead of a plain string. This changes how you should parse API errors.
fix
Adjust error parsing logic to check for both string and object error formats in API responses, especially when integrating with third-party Last.FM compatible services.
affects: >=2.3.0
gotchaVersion 2.6.2 noted that the album field within a track object can be missing in some Last.FM API responses, which might affect applications assuming its presence.
fix
Implement defensive programming by checking for the existence of the `album` property before attempting to access its sub-properties, e.g., `track.album?.title`.
affects: >=2.6.2
Errors
Common errors & fixes
ERR_REQUIRE_ESM: Must use import to load ES Module
Attempting to use `require()` to import `lastfm-ts-api` in a CommonJS context, but the package is ESM-only since v2.0.0.
fix
Change your import statements from `const { LastFMAlbum } = require('lastfm-ts-api');` to `import { LastFMAlbum } from 'lastfm-ts-api';`. Also, ensure your `package.json` contains `"type": "module"` or your file has a `.mjs` extension.
TypeError: Cannot read properties of undefined (reading 'track')
Incorrectly parsing the `track.scrobble` response, especially when scrobbling a single track, due to the response structure changing to an object in v2.3.0.
fix
Update your code to expect an object response from `track.scrobble` when scrobbling a single track, rather than assuming it's always an array or a different structure.
Property 'OldTypeName' does not exist on type 'NewTypeName'. Did you mean 'NewTypeName'?
Using old type names in a TypeScript project after upgrading to v2.0.0, where several types were renamed.
fix
Update your TypeScript code to use the new type names introduced in v2.0.0. Refer to the library's type definitions for the correct names.
Upgrade
Version history
2.6.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources