Finding lyrics and facts about your favorite song or an artist can be a tedious job. You may have to dig through various web pages, just to find the correct lyrics. If you are developing an app for the music industry or building tools to visualize music data – you would require a ‘cleaner data-set’ that requires minimal sanitization. And you certainly don’t want to iterate through various sources or perform scrapping on web pages with old looking CSS and poorly organized HTML structure.
Meet Genius – which brings music knowledge to the masses, and that too, free of cost.
Genius is an online media platform that allows users to annotate & interpret song lyrics and has become the world’s biggest collection of song lyrics and crowdsourced musical knowledge as it serves over 100 million people each month.
Genius also provides REST APIs which can be integrated with your mobile or desktop applications, using the language of your choice: such as Python, PHP, Ruby, and JavaScript.
What is Genius API?
The Genius API allows developers to access the platform’s annotation functionality programmatically and also give them ability to retrieve musical information and metadata.
Below are some important functions that Genius API provides:
- Search Musical Metadata – Search through Genius’s vast repository of musical metadata about artists, albums, and songs.
- Programmatically access artist information including details such as social media handles, followers count, relevant images. etc.
- Retrieve information on Songs and leverage the community-powered annotations available on Genius.com.
How it Works?
The Genius API follows the REST API Principle and works in a similar manner..
The Client requests the resources using its endpoints. It does so by issuing a GET request to Genius.com.
The Audience
Third-Party App Developers:
Genius API allows third-party app developers to build and extend the functionalities of Genius.com. There can be many real-world use cases; from creating a simple list of top-performing artists (based on various filters) to creating a lyrical analysis app (powered by an AI/ML) – The Sky is the limit.
Music Integration:
With the rise of mobile applications, APIs are getting extremely popular among the developers’ community. Mobile Application Developers can make use of Genius API to provide various features out of the box in their Music Apps. This allows developers to focus more on the business aspects of their apps and they can simply let Genius API do the heavy lifting for them.
Data Science and Artificial Intelligence:
Genius has a vast database of musical knowledge and metadata. This can be super helpful for the developers building AI-powered tools for the music industry.
The Genius API is well suited for data scientists and can help them in analyzing the particular trend in music through a data lens. And as part of that, they will be performing metadata analysis – which requires collecting all the metadata from all the songs that they are interested in. Genius API can be a good start to automate this “Data Collection” process and building the right data models for AI/ML apps.
How to Connect to Genius API Tutorial – Step by Step
Step 1: Signup for a Free Account on RapidAPI
RapidAPI lets developers manage all API integration from one place and also provides real-time performance metrics. You can signup for a free account using this link.
After successful registration, you need to subscribe to the API. You need to follow the below steps:
- Navigate to the Genius API and select Pricing Tab
- Click “Connect Now” to Subscribe to the API
Note: You can also manage your subscription and usage using the Subscription and usage page available in RapidAPI Developer Dashboard.
Step 3. Test the API Subscription
After subscribing to the Genius API, the next step is to make sure all the endpoints are working as intended.
To do this, select Endpoint Tab.
The Genius API has 4 endpoints as displayed in the image below.
You can test any endpoint and select the code snippet for any programming language, including Python, JavaScript, PHP, and Ruby.
Note: API Key and other required parameters like (Artist/Song ID) will automatically be generated by RapidAPI Console.
A successful test will show the result with a status code of 200 OK. As shown below.
Note: An unsuccessful test may return one of the following responses:
- 400 Bad Request – Client sent an invalid request (missing required parameters).
- 401 Unauthorized – Failed to authenticate with the API server.
- 403 Forbidden – Client authenticated but doesn’t have permission to access the required resource.
- 404 Not Found – The requested resource (endpoint) doesn’t exist.
- 500 Internal Server Error – A generic error occurred on the API server.
- 503 Service Unavailable – The API service is not available.
Explanation of Genius Endpoints
As shared in the above image, the Genius API has four endpoints. In this section, we will discuss them in detail.
The Artist Endpoint:
This endpoint allows users to lookup an Artist by ID. You can get artist information including the social media handles, followers count, related images, and also the annotations.
It has an optional parameter called “text-format” which specifies the format for the text bodies related to the document. The default value is “dom” which can be changed to “plain” or “HTML” formats based on the application’s requirements.
Note: This endpoint assumes that you have Artist ID, which can be fetched using one of the following ways:
1) Search the artist using search endpoint, Grab a song from the results and then use the song endpoint for that song’s information which includes Artist ID as well e.g (Primary_artist, featured_artists, etc).
2) If you know the song you may directly hit the song endpoint to fetch the associated Artist ID.
If the Artist ID is found in the database, the API will return the Artist details in response as shown below:
The Songs Endpoint:
This endpoint allows users to find meta-data about the songs (hosted on genius) including lyrics and annotations. You can find a lot of useful information about the song such as its Release Date, Full Title, Song Art Image, Album Information, Producer and Artist Info, Page Views, Verified Annotations, etc.
The Search Endpoint
This is a very powerful endpoint that allows the user to search through all content hosted on genius. It takes a required query parameter which can be artist name or any song title and it will return the detailed response if there were matching results.
Genius API – Search Endpoint Example (Search by Song Title)
Genius API – Search Endpoint Example (Search by Artist).
The Artist Songs Endpoint
This endpoint allows the users to iterate through the list of songs for the specified artist and uses Artist ID as a required parameter.
It has three optional parameters:
Per page
: It represents the number of results to return per requestPage
: It represents the paginated offsetSort
: It uses the title as default but can be changed to popularity as well
Note: By default, it returns 20 items for each request (as shown in the image below).
How to Use Genius API with Python
Please make sure that you have installed the Python application.
The code below is for the search endpoint. We are invoking search API using Artist title “Brian Adams”.
import requests url = "https://genius.p.rapidapi.com/search" querystring = {"q":"Brian Adams"} headers = { 'x-rapidapi-key': "ce19d0164fmsh3d383efc0e85ce5p16dcb1jsnb1a4a3c79541", 'x-rapidapi-host': "genius.p.rapidapi.com" } response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
How to Use Genius API with PHP
Before running the sample code shared below, please make sure that you have installed PHP. The sample code uses unirest-php. You may follow this guide on how to install this correctly.
This sample code also invokes the search endpoint with the term “Brian Adams”.
<?php $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://genius.p.rapidapi.com/search?q=Brian%20Adams", CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => [ "x-rapidapi-host: genius.p.rapidapi.com", "x-rapidapi-key: ce19d0164fmsh3d383efc0e85ce5p16dcb1jsnb1a4a3c79541" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; }
How to Use Genius API with Ruby
The below example uses Ruby. Please make sure that you have installed Ruby on your system.
require 'uri' require 'net/http' require 'openssl' url = URI("https://genius.p.rapidapi.com/search?q=Brian%20Adams") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) request["x-rapidapi-key"] = 'ce19d0164fmsh3d383efc0e85ce5p16dcb1jsnb1a4a3c79541' request["x-rapidapi-host"] = 'genius.p.rapidapi.com' response = http.request(request) puts response.read_body
How to Use Genius API with Javascript
To use the API in JavaScript, you need to first set up the JavaScript environment. You can download text editors such as Atoms, Visual Studio Code, NotePad++, etc.
The following code snippet invokes the Search endpoint by providing the artist name as Search Keyword and It will return the list of results in an array.
fetch("https://genius.p.rapidapi.com/search?q=Brian%20Adams", { "method": "GET", "headers": { "x-rapidapi-key": "ce19d0164fmsh3d383efc0e85ce5p16dcb1jsnb1a4a3c79541", "x-rapidapi-host": "genius.p.rapidapi.com" } }) .then(response => { console.log(response); }) .catch(err => { console.error(err); });
After successful execution, you will be able to see the JSON response as shown below:
Benefits of Genius API
Great Source of Crowdsourced data:
Genius has great crowdsourced data for music. It’s super easy to find metadata about any song or an artist which is why it is being used by the number of AI-powered music apps to build a reliable dataset.
Speed:
The Genius API latency is around 373ms so it returns the desired results in no time, making it an ideal candidate to build real-time music and data analysis applications.
Accuracy:
With Genius API, you don’t have to deal with spam (thanks to its huge community) which can downvote and edit any lyric or annotation that is not correct. So you don’t have to spend hours performing ‘Data Cleaning’.
Access to Social Media Stats:
Genius API also allows users to view artists’ social media handles, which include: Facebook, Twitter, and the Instagram handle. One of the key factors of gauging the popularity of any artist is the ‘followers count’, which is also included in the API response and is updated in near real-time.
Alternatives to Genius API
There are some APIs at RapidAPI that can also be used to fetch musical knowledge and lyrics.
- Scrapesoft Music Lyrics This API allows users to find lyrics of any song using Google’s lyric database.
- Artist Info: This API can be used to find information about any artist.
- Mourits Lyrics: This API provides song lyrics using a variety of sources.
- SongMeanings: This API also allows users to know about song lyrics and it’s meanings.
- Shazam: This API allows users to identify any song and discover artists and lyrics.
Summary
The Genius API provides crowdsourced lyrics and music knowledge, making it easy for third-party app developers, Music Enthusiast, and Data Scientists to build a variety of apps ranging from Music Apps to Visualizing Music Data through Artificial Intelligence and Machine Learning. This guide will help them to use and integrate Genius API for various platforms and apps.
Leave a Reply