lastfm.ts
v1.0.1
Published
A typed Last.fm API client
Maintainers
Readme
Last.fm Client (Typed)
A robust, fully typed TypeScript client for the Last.fm API.
Installation
npm install lastfm.tsGetting Started
Initialize the client with your Last.fm API credentials.
import { LastFmClient } from "lastfm.ts";
const client = new LastFmClient({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
userAgent: "MyApp/1.0.0 ([email protected])", // Optional but recommended
});Features
Authentication
To scrobble or update "Now Playing", you need a user session.
Get Auth URL: Redirect the user to this URL to approve your app.
const authUrl = client.getAuthUrl("http://localhost:3000/callback"); console.log("Please visit:", authUrl);Get Session: Once the user returns with a
token, exchange it for a session key.const session = await client.getSession("TOKEN_FROM_CALLBACK"); const sessionKey = session.session.key;
User Methods
Get User Info
const user = await client.getUser("rj");
console.log(`User: ${user.user.realname}, Playcount: ${user.user.playcount}`);Get Recent Tracks & Now Playing
// Get recent tracks
const recent = await client.getRecentTracks("rj", 10);
// Check if user is currently listening to something
const nowPlaying = await client.getNowPlaying("rj");
if (nowPlaying) {
console.log(`Listening to: ${nowPlaying.name} by ${nowPlaying.artist["#text"]}`);
}Track Methods
Scrobbling (Record a listen)
Requires sessionKey.
await client.scrobble(
sessionKey,
"Daft Punk", // Artist
"One More Time", // Track
Math.floor(Date.now() / 1000), // Timestamp (in seconds)
"Discovery", // Album (Optional)
);Update Now Playing
Requires sessionKey.
await client.updateNowPlaying(sessionKey, "Daft Punk", "Harder, Better, Faster, Stronger");Search & Similar
const searchResults = await client.searchTrack("Believe");
const similarTracks = await client.getSimilarTracks("Cher", "Believe");Other Methods
The client also supports methods for Albums, Artists, and Tags:
getAlbumInfo,searchAlbumgetArtistInfo,getSimilarArtists,searchArtistgetTagInfo
License
ISC
