Giter Site home page Giter Site logo

adb-sh / spotify-api.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spotify-api/spotify-api.js

0.0 0.0 0.0 399 KB

A complete wrapper for spotify web api for deno, node.js and the browser.

Home Page: https://spotify-api.js.org

License: MIT License

TypeScript 100.00%

spotify-api.js's Introduction



About

Spotify-api.js is an alternative to work with spotify api with a typesafe environment and with camel cased objects. Make sure to read the documentation here.

This package or the documentation might have bugs, so kindly report us about that in the issues.

Features

  • Typesafe environment.
  • Has typings for api types here.
  • Object oriented with camel case object keys.
  • Works with caching too.
  • Easy to learn.
  • Works with browser and deno too.

Examples

Getting started

Installing the package!

npm i spotify-api.js@latest

Get your client id and client secret from here.

Setting up your spotify client.

const Spotify = require("spotify-api.js");
const client = new Spotify.Client({ token: 'token' });

console.log(await client.tracks.get('id'));

Or create a token directly from clientID and clientSecret,

const { Client } = require("spotify-api.js");
const client = new Client({ 
    token: { clientID: 'id', clientSecret: 'secret' },
    // Ready event is required if you are providing clientID and clientSecret fields.
    // As the client has to create the token first with it and then emits the ready event.
    onReady() {
        console.log(await client.tracks.get('id'));
    }
})

// More simpler code with asynchronous operations:
const client = await Client.create({ token: { clientID: 'id', clientSecret: 'secret' } });
console.log(await client.tracks.get('id'));

Setting up your user client.

Get a current user authorized token from the authenication details you got from the request or to refresh the token,

const { Client } = require('spotify-api.js');

const client = await Client.create({
    token: {
        clientID: 'id', // Your spotify application client id.
        clientSecret: 'secret', // Your spotify application client secret.
        code: 'code', // The code search query from the web redirect. Do not use this field if your aim is to refresh the token.
        refreshToken: 'refreshToken', // Use this field only if your aim is to refresh your token instead of getting new one put your refresh token here.
        redirectURL: 'url' // The redirect url which you have used when redirected to the login page.
    }
});

console.log(client.token); // The current user token. 
await client.artists.follow("SOME ARTIST ID"); // And can use the api methods which are for current user if you have the paticular scopes...

Surpassing ratelimits

Ratelimits are common with any api services to prevent spam but sometimes it might be annoying. The client has an options retryOnRateLimit. If it is set to true, it would refetch the same request after a paticular time interval sent by the spotify api in the headers Retry-After so you cannot face any obstacles. This is disabled by default...

const Spotify = require("spotify-api.js");
const client = new Spotify.Client({ 
    token: 'token',
    retryOnRateLimit: true
});

console.log(await client.tracks.get('id'));

Auto refreshing token.

The tokens of spotify are temporary so it is a trouble to refresh the token each and every interval of time. As an alternative you can use the refreshToken option.

  • Using clientID and clientSecret for api only token.
const client = await Client.create({
    refreshToken: true, // Set this to true.
    token: {
        clientID: 'id', // Your spotify application client id.
        clientSecret: 'secret', // Your spotify application client secret.
    },
    // This event is emitted whenever the token is refreshed by either 429 requests or [Client.refresh] method.
    onRefresh() {
        console.log(`Token has been refreshed. New token: ${client.token}!`);
    }
});
const client = await Client.create({
    refreshToken: true, // Set this to true.
    token: {
        clientID: 'id', // Your spotify application client id.
        clientSecret: 'secret', // Your spotify application client secret.
        code: 'code', // The code search query from the web redirect.
        redirectURL: 'url' // The redirect url which you have used when redirected to the login page.
    },
    // This event is emitted whenever the token is refreshed by either 429 requests or [Client.refresh] method.
    onRefresh() {
        console.log(`Token has been refreshed. New token: ${client.token}!`);
    }
});

NOTE: This option is useless if you just provided the token string and not the clientID and the clientSecret or the current user authorization options.

Caching

There is an inbuilt cache system for the module. By default the caching is disabled to prevent memory leaking and unwanted processing.

const { Client, Cache } = require('spotify-api.js');

const client = new Client({
    token: "token",
    // If you want to cache all the cache types, you can do it like
    // cacheSettings: true
    cacheSettings: {
        tracks: true // Only tracks will be cached.
    }
});

await client.tracks.get("ID"); // The track is now cached.
console.log(Cache.tracks.get("id")); // You should get the cached track.

await client.tracks.get("ID"); // Second time using the function will return cached one.
await client.tracks.get("ID", true); // Using second parameter as true will force fetch instead of returning from the cache. (This will force fetch directly if cacheSettings is disabled...)

Help

If any doubts, bugs or reports regarding the module or the documentation you can create an issue in github.

spotify-api.js's People

Contributors

scientific-dev avatar dependabot[bot] avatar adb-sh avatar arakmar avatar venables avatar peterjuras avatar cutiecat6778 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.