Last.Fm Node Client
- Consistent: Providing you a uniform set of abstractions that allows you to use the interface predictably and reliably.
- Versatile: Adapting it's communication with the API, complying with it's specifications without you needing to adapt your code.
- Low level: Delivering you the response as it comes directly from the API, using it's native
formatparameter, to deliver a consistent JSON response.
See the Last.Fm API web page for information about the Last.Fm API, including details on how to register an account and get your API key, shared secret, and session key.
$ npm install lastfm-node-client
Node.js 8.10.0 or later is required.
First, you must instantiate the LastFm Class with arguments containing the details of your API account.
apiKey is required, however since many endpoints of the API do not require authentication,
sessionKey are optional.
const LastFm = ;const lastFm = "API_KEY" "SECRET" "SESSION_KEY";
The Last.fm API is structured into packages and methods, accessed as
Package.method. The LastFm Class contains directly corresponding methods for each package method, written as
lastFm.packageMethod(). For example, endpoint
User.getRecentTracks is accessed as
Parameters can be passed to the API through the
params argument as an object that will be sent directly with the request, either as a query for a GET request, or a body for a POST request. The property names will not be transformed or abstracted, and so they must match the endpoint parameters exactly.
lastFm.tagGetTopTags()do not accept a
paramsargument, as endpoints
Tag.getTopTagsdo not require additional parameters.
lastFm.userGetInfo()params.user and params argument are optional and will default to the authenticated user in accordance with endpoint
Capturing Responses and Handling Errors
Every method returns a promise of the pending request by default. To access the response, you can chain
.then() to the method, or use
await. Errors thrown while making a request or errors returned by the API will reject the promise.
const data = await lastFm;console;
An optional callback can be passed as the last argument. It is invoked with conventional
(err, data) arguments;
err being any errors thrown while making a request or errors returned by the API,
data containing the JSON response of the API upon success.
When callback is passed, methods do not return a
promise, instead return the LastFm instance the method was called on. This allows you to chain requests.
These methods do not correspond to an exact API endpoint, but are abstractions of the already provided methods to provide easier usage.
This method takes an array of objects that match the
params parameter accepted by
lastFm.trackScrobble. It allows for multiple tracks to be submitted to the API in one request.