Getting Started
Requirements
Section titled “Requirements”- Node.js:
≥ 18.0.0
Installation
Section titled “Installation”Install @arxyf/fm using your preferred package manager:
npm install @arxyf/fmpnpm add @arxyf/fmyarn add @arxyf/fmbun add @arxyf/fmInitialization
Section titled “Initialization”Environment Variables
Section titled “Environment Variables”If LASTFM_API_KEY, LASTFM_API_SECRET, and LASTFM_API_USER_AGENT are set in your environment, initialization is zero-config:
import { FMClient } from "@arxyf/fm";
const fm = new FMClient();Custom Settings
Section titled “Custom Settings”Alternatively, pass explicit configuration options:
import { FMClient } from "@arxyf/fm";
const fm = new FMClient({ api: { key: "YOUR_KEY", secret: "YOUR_SECRET", }, rateLimit: { bucketMax: 3, refillIntervalMs: 300, maxQueueSize: 200, backOffBaseMs: 5000, backOffOutageBaseMs: 10_000, }, network: { retries: 1, abortTimeoutMs: 4000, retryStrategy: { onRateLimit: true, onServiceOutage: true, onTimeout: true, }, }, behavior: { autoCorrectByDefault: true, emitRequestFailedOnReject: true, },});Usage Examples
Section titled “Usage Examples”Fetching User Info
Section titled “Fetching User Info”const res = await fm.user.getLovedTracks({ user: "username" });
if (!res.success) return console.warn(`(${res.errorCode}) ${res.errorMsg}`);
console.info(res.lovedTracks);const res = await fm.user.getNowPlaying("username");
if (!res.success) return console.warn(`User is currently offline.`);
console.info(`Listening to: ${res.track.name} by ${res.track.artist.name}`);Scrobbling
Section titled “Scrobbling”Scrobble a single track, or submit several at once with scrobbleBatch.
const res = await fm.track.scrobble({ sk: "SESSION_KEY", track: "Aja", artist: "Steely Dan", timestamp: Math.floor(Date.now() / 1000), meta: { userId }, // optional});
if (!res.success) return console.warn(`(${res.errorCode}) ${res.errorMsg}`);const now = Math.floor(Date.now() / 1000);const tracks = [ { track: "Aja", artist: "Steely Dan", timestamp: now }, { track: "Keith Don't Go", artist: "Nils Lofgren", timestamp: now - 120 },];
const res = await fm.track.scrobbleBatch({ sk: "SESSION_KEY", tracks, meta: { userId }, // optional});
if (!res.success) { return console.warn(`(scrobbled: ${res.scrobbledCount}) (${res.errorCode}) ${res.errorMsg}`);}Event Handling
Section titled “Event Handling”Listen to internal client events to monitor network behavior or handle session states.
// Failed requestsfm.on("requestFailed", (payload) => { const { apiMethod, attempt, message, queueSize, willRetry } = payload; console.warn(`[${apiMethod}]: (Attempt: ${attempt}, Retry: ${willRetry}) (queueSize: ${queueSize}) ${message}`);});
// Expired sessionsfm.on("sessionExpire", (message, meta) => { console.info(`[sessionExpire]: ${message}`, meta);});