last.fm.api

0.1.3 • Public • Published

last.fm.api

Build Status

Wrapper for Last.FM API v2 with ES6 style classes and promises. Supports Desktop, Web and Mobile auth. http://www.last.fm/api

Prerequisites

You need an API key and secret:

Install

npm install last.fm.api --save

Usage

All API methods listed in the developer documentation http://www.last.fm/api are mapped to the API class. e.g. api.album.getTags() for the API method album.getTags. You do not need to supply api_sig and api_key to the methods, these will be generated automatically. You must supply sk (session key) for methods that require it.

'use strict';
 
const API = require('last.fm.api'),
    api = new API({ 
        apiKey: '<YOUR API KEY>', 
        apiSecret: '<YOUR API SECRET>'
    });

API

new API(options);

  • apiKey String - Your API key
  • apiSecret String - Your API secret
  • debug Boolean - If true the URL and Querystring/Form Body are written to the console. (default false)
  • username String - Your Last.FM username, required when using authentication for Mobile apps (optional)
  • password String - Your Last.FM password, required when using authentication for Mobile apps (optional)

Examples

There are a lot of examples in the /examples folder which I've tried to comment thoroughly. Likewise there are examples of Desktop, Mobile and Web Authorisation.

'use strict';
 
const API = require('last.fm.api'),
    api = new API({ 
        apiKey: '<YOUR API KEY>', 
        apiSecret: '<YOUR API SECRET>'
    });
 
api.album.getTags({
    artist: 'nirvana',
    album: 'nevermind'
})
    .then(tags => { console.log(tags); })
    .catch(err => { console.error(err); });
 
/*
// tags = ...
{
    "tags": {
        "tag": [{
            "name": "metal",
            "url": "http://www.last.fm/tag/metal"
        }, {
            "name": "punk",
            "url": "http://www.last.fm/tag/punk"
        }, {
            "name": "Grunge",
            "url": "http://www.last.fm/tag/Grunge"
        }, {
            "name": "noise",
            "url": "http://www.last.fm/tag/noise"
        }],
        "@attr": {
            "artist": "Nirvana",
            "album": "Nevermind"
        }
    }
*/

To get a session key for mobile apps

'use strict';
 
const API = require('last.fm.api'),
    api = new API({ 
        apiKey: '<YOUR API KEY>', 
        apiSecret: '<YOUR API SECRET>',
        debug: true,
        username: '<YOUR USERNAME>',
        password: '<YOUR PASSWORD>'
    });
 
// Get Mobile Session by supplying username and password into API constructor
api.auth.getMobileSession({})
    .then(json => json.session)
    .then(session => {
        console.log('session', session);
        process.exit();
    })
    .catch(err => {
        console.error('ERRORED!', JSON.stringify(err));
        process.exit();
    });
 
/*
// result = ...
{
    "subscriber": 0,
    "name": "<YOUR USERNAME>",
    "key": "<SESSION KEY>"
*/

Package Sidebar

Install

npm i last.fm.api

Weekly Downloads

304

Version

0.1.3

License

ISC

Last publish

Collaborators

  • leemm