LastFm

LastFm

  • Category: Music


Using Last.fm API in JavaScript

Last.fm provides a public API that allows developers to access information about music, artists, tags, and more. In this blog post, we'll explore some of the basic API requests using JavaScript.

Getting Started

Before we can make API requests, we need to obtain an API key from Last.fm. We can do this by creating an account on the Last.fm website and then applying for an API key. Once we have the API key, we can start making requests.

Making API Requests

Last.fm API requests can be made using any programming language that can communicate with web APIs. In this example, we'll use JavaScript to make API requests using the fetch method.

Search for Songs

To search for songs, we can use the track.search method. Here's an example code snippet:

const apiKey = '<your-API-key>';
const searchText = 'love';

fetch(`http://ws.audioscrobbler.com/2.0/?method=track.search&track=${searchText}&api_key=${apiKey}&format=json`)
  .then(response => response.json())
  .then(data => console.log(data));

In this code, we construct the API URL by building a query string with the API method (track.search), the search text (love), our API key, and the response format (json). We then use fetch to make a GET request to the Last.fm API with this URL.

Get Song Info

To get information about a specific song, we can use the track.getInfo method. Here's an example code snippet:

const apiKey = '<your-API-key>';
const trackArtist = 'The Beatles';
const trackName = 'Let It Be';

fetch(`http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=${apiKey}&artist=${trackArtist}&track=${trackName}&format=json`)
  .then(response => response.json())
  .then(data => console.log(data));

In this code, we construct the API URL by building a query string with the API method (track.getInfo), our API key, the song artist (The Beatles), the song name (Let It Be), and the response format (json). We then use fetch to make a GET request to the Last.fm API with this URL.

Get Artist Info

To get information about a specific artist, we can use the artist.getInfo method. Here's an example code snippet:

const apiKey = '<your-API-key>';
const artistName = 'The Beatles';

fetch(`http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&api_key=${apiKey}&artist=${artistName}&format=json`)
  .then(response => response.json())
  .then(data => console.log(data));

In this code, we construct the API URL by building a query string with the API method (artist.getInfo), our API key, the artist name (The Beatles), and the response format (json). We then use fetch to make a GET request to the Last.fm API with this URL.

Conclusion

Using the Last.fm API in JavaScript is a great way to access information about music and artists. By constructing API URLs and making requests using fetch, we can easily retrieve data and integrate it into our own applications. This was just a brief introduction to some of the basic API requests, but the Last.fm API offers many more methods and parameters to explore.

Visit to LastFm website

Similar APIs of Music

Audio DB API

Audio DB API

Music

With the Audio DB API, you can get albums, artist, specific tracks data, youtube music videos, popularity of an artist and images for 1000s of musicians. For example, on free plan, you can fetch the artist's entire biography. Such as year of birth, style & genre of music, mood of the star, his social media websites, thumbnail, logo, gender, complete description in nearly 6 languages.

popularmusicyoutube

Songsterr API

Songsterr API

Music

Allows you to search, get, and present songs and guitar, bass, and drums tabs and chords from the Songsterr database. Songsterr has an open Application Programming Interface (API for short). This means that anyone can write their own program to present public Songsterr data in new and different ways. The Songsterr API allows you to call methods that respond in REST style xml. We also support json and plist - just substitute ".xml" with ".json" or ".plist" Individual methods are detailed below.

guitarbassdrums

Genrenator API - Binary Jazz

Genrenator API - Binary Jazz

Music

Fetch a random genre or genre story. Much like Binary Jazz itself, the genrenator is the product of a bunch of us chatting and tossing ideas around and something sticks. In this case, it came from a discussion about obscure genres we didn't know existed, veered to the Every Noise At Once database that Spotify uses to inform their genres and playlists, and ended with the question "what if we could create an API that served random genres?" We used Every Noise At Once as a base to take parts of genres from, then created internal catalogs of each genre "fragment". These catalogs are broken into instruments, beats, adjectives, prefixes and suffixes, regions, and genres. We also used Every Noise to inform our list of patterns which are used to combine the above fragments in unique ways. The real fun of the Genrenator is the public API hosted on binaryjazz.us. If you're a developer (particularly one in need of a random genre or story about a genre), you can use the endpoint to fetch a random genre or genre story at https://binaryjazz.us/wp-json/genrenator/v1.

genrespotifyprefixes

Jaxsta Music Credits API

Jaxsta Music Credits API

Music

Jaxsta is a music company using technology to develop the world’s most comprehensive resource of official music credits. Founded in 2015, and debuting on the Australian Securities Exchange in December 2018, Jaxsta ensures those who create music – be they an artist, producer, songwriter, collaborator, guest performer or engineer – receive credit where credit is due. Jaxsta’s head office is based in Sydney, with representatives in New York, London and Los Angeles. The Jaxsta Commercial API is being developed to offer programmatic access to the data in the Jaxsta database - an invaluable tool companies can use to enhance their existing content. A couple of examples of this could be: - A streaming service using the API to add full album details to their existing information. - A music company using the API to identify new relationships between data to create a smarter recommendation engine. Version 1 of the API will be launched in 2021. Verified, deep linked metadata can streamline any business that uses music data. We are not just restricted to the traditional musical industry business formats. If you can think of any other ways your company may be able to utilise our API, get in touch and let us know!

musiccredits

Cyanite

Cyanite

Music

Analyze emotions in music. Cyanite supports players in the music industry to make a smooth transition into the age of AI. Its API mainly aims at production music libraries, publishers and digital music stores to level up their databases. It's open for anyone to create new solutions. It lets you analyze music on: • Genre • Mood • Mood waves • BPM • Key • Instruments • Energy & Mood Levels and lets you create Similarity Searches in your own music databases. Made with love in Berlin & Mannheim, Germany Also visit us at: https://cyanite.ai/ Or shoot us an email: mail (@) cyanite.ai

ArtificialMachineMood

KKBOX

KKBOX

Music

Get music libraries, playlists, charts, and perform out of KKBOX's platform. You can put a corner box on your existing website to give a bright corner or you can make a music directory from this API.

musiclibraryapi

FreeSound

FreeSound

Music

With the Freesound API you can browse, search, and retrieve information about Freesound users, packs, and the sounds themselves of course. You can find similar sounds to a given target (based on content analysis) and retrieve automatically extracted features from audio files.

searchfreesounds

Audiomack

Audiomack

Music

Audiomack Is a Streaming Music Hub for Artists and Fans. Search for artists, songs and albums. Take control of what you get back by filtering data with music fields, favourites, artists, title and id.

audiomacartist

Openwhyd

Openwhyd

Music

Openwhyd is a free and open-source music curation service that allows users to create playlists of music tracks from various streaming platforms (YouTube, SoundCloud, Vimeo, Deezer…) and to discover the music posted by other users.

collectionmusicyoutube

Spotify

Spotify

Music

Fetch data from the Spotify music catalog, manage users' playlists and saved music, get recommendations, control Spotify Connect, and more. Based on simple REST principles, the Spotify Web API endpoints return JSON metadata about music artists, albums, and tracks, directly from the Spotify Data Catalogue.

spotifyfreeapi