> rynn-k/gists
javascript

/genius.js

Search and get detailed song lyrics from Genius.

1320 bytes · Feb 27, 2026 · 1 revision
const axios = require('axios');

class Genius {
    constructor() {
        this.inst = axios.create({
            baseURL: 'https://api.genius.com',
            headers: {
                'user-agent': 'Genius/8.0.5.4987 (Android; Android 10; samsung SM-J700F)',
                'x-genius-app-background-request': '0',
                'x-genius-logged-out': 'true',
                'x-genius-android-version': '8.0.5.4987'
            }
        });
    }
    
    async search(query) {
        try {
            if (!query) throw new Error('Query is required.');
            
            const { data } = await this.inst.get('/search/multi', {
                params: {
                    q: query
                }
            });
            
            return data?.response?.sections?.find(s => s.type === 'song')?.hits || [];
        } catch (error) {
            throw new Error(error.message);
        }
    }
    
    async detail(id) {
        try {
            if (isNaN(id)) throw new Error('Song ID is required.');
            
            const { data } = await this.inst.get(`/songs/${id}`);
            return data?.response?.song || data;
        } catch (error) {
            throw new Error(error.message);
        }
    }
}

// Usage:
const g = new Genius();
g.detail('11513410').then(console.log);