npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

musixmatch-api-js

v1.0.2

Published

`musixmatch-api-js` is a JavaScript library that allows you to easily interact with the MusixMatch API. It provides methods to fetch artist information, track details, lyrics, and more. This library is perfect for use in web applications, including React

Downloads

5

Readme

MusixMatch API JS

musixmatch-api-js is a JavaScript library that allows you to easily interact with the MusixMatch API. It provides methods to fetch artist information, track details, lyrics, and more. This library is perfect for use in web applications, including React projects.

Features

  • Fetch artist information
  • Search for tracks and artists
  • Retrieve track details and lyrics
  • Get top charts for artists and tracks
  • Easy-to-use and browser-compatible

Installation

You can install the package via npm:

npm install musixmatch-api-js

Usage

Here's a simple example of how to use musixmatch-api-js in a React project to fetch and display track lyrics.

Importing and Initializing

First, import the package and create an instance of MusixMatchAPI:

import MusixMatchAPI from 'musixmatch-api-js/dist/musixmatchAPI.bundle.js';

const musixmatch = new MusixMatchAPI();

Fetching Track Lyrics

To fetch track lyrics, use the getTrackLyrics method. This method requires a trackId to identify the track:

async function fetchTrackLyrics(trackId) {
    try {
        const lyrics = await musixmatch.getTrackLyrics(trackId);
        console.log(lyrics);
    } catch (error) {
        console.error('Error fetching track lyrics:', error);
    }
}

fetchTrackLyrics('123456');

Using in a React Component

Below is an example of a React component that fetches and displays lyrics for a given track ID.

import React, { useState, useEffect } from 'react';
import MusixMatchAPI from 'musixmatch-api-js/dist/musixmatchAPI.bundle.js';

const musixmatch = new MusixMatchAPI();

const TrackLyrics = ({ trackId }) => {
    const [lyrics, setLyrics] = useState(null);
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState(null);

    useEffect(() => {
        const fetchLyrics = async () => {
            setLoading(true);
            setError(null);
            try {
                const response = await musixmatch.getTrackLyrics(trackId);
                setLyrics(response.message.body.lyrics.lyrics_body);
            } catch (err) {
                setError('Error fetching lyrics');
            } finally {
                setLoading(false);
            }
        };

        fetchLyrics();
    }, [trackId]);

    return (
        <div>
            {loading && <p>Loading...</p>}
            {error && <p>{error}</p>}
            {lyrics && <pre>{lyrics}</pre>}
        </div>
    );
};

export default TrackLyrics;

Adding the Component to Your App

Finally, use the TrackLyrics component in your main application:

import React from 'react';
import ReactDOM from 'react-dom';
import TrackLyrics from './TrackLyrics';

const App = () => {
    return (
        <div>
            <h1>Track Lyrics</h1>
            <TrackLyrics trackId="123456" />
        </div>
    );
};

ReactDOM.render(<App />, document.getElementById('root'));

Methods

searchTracks(trackQuery, page = 1)

Search for tracks by a query string.

Parameters:

  • trackQuery (string): The track name or search query.
  • page (number, optional): The page number for pagination (default is 1).

Returns: A promise that resolves to the search results.

getTrack(trackId)

Get detailed information about a track by its ID.

Parameters:

  • trackId (string): The ID of the track.

Returns: A promise that resolves to the track details.

getTrackLyrics(trackId)

Get lyrics for a track by its ID.

Parameters:

  • trackId (string): The ID of the track.

Returns: A promise that resolves to the track lyrics.

searchArtist(query, page = 1)

Search for artists by a query string.

Parameters:

  • query (string): The artist name or search query.
  • page (number, optional): The page number for pagination (default is 1).

Returns: A promise that resolves to the search results.

getArtist(artistId)

Get detailed information about an artist by their ID.

Parameters:

  • artistId (string): The ID of the artist.

Returns: A promise that resolves to the artist details.

getArtistAlbums(artistId, page = 1)

Get albums by an artist ID.

Parameters:

  • artistId (string): The ID of the artist.
  • page (number, optional): The page number for pagination (default is 1).

Returns: A promise that resolves to the artist's albums.

getAlbum(albumId)

Get detailed information about an album by its ID.

Parameters:

  • albumId (string): The ID of the album.

Returns: A promise that resolves to the album details.

getAlbumTracks(albumId, page = 1)

Get tracks in an album by the album ID.

Parameters:

  • albumId (string): The ID of the album.
  • page (number, optional): The page number for pagination (default is 1).

Returns: A promise that resolves to the album's tracks.

Contributing

If you'd like to contribute to musixmatch-api-js, please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.