Skip to main content

Auth Token

npm Codecov

Motivation

This package provides a simple and lightweight implementation of Spotify's Authorization Code Flow according to the Spotify docs. You authenticate with a Spotify application and get back a refresh token that can be used to access Spotify data in long-running applications. If you're not yet familiar with Spotify's authorization process, check out this quick summary or read the official docs.

Usually, a refresh token only needs to be generated once. Such a token only expires when the client secret of your Spotify application is reset and otherwise lives forever.

This helper was mainly developed to simplify my Spotify history scrobbler.

CLI Usage

Create your custom command using the query builder below and paste it into your terminal.

With NPX

npx @spotifly/cli@latest auth --port 3000 --scopes "user-read-email" --file-name spotify-token

With CLI

spotifly auth --port 3000 --scopes "user-read-email" --file-name spotify-token

Options

Flag(required?) Description
--client-id [string]✅ The client id of your Spotify application
--client-secret [string]✅ The client secret of your Spotify application
-s, --scopes [string]❌ Spotify authorization scopes. Multiple scopes need to be separated by a space. Default: "user-read-email"
-p, --port [number]❌ Port for the localhost redirect URL. Default: 3000
-o, --out-dir [string]❌ Custom relative output directory. Default: Current directory
-f, --file-name [string]❌ Custom name for the output JSON file. Default: "spotify-token"
--no-emit [boolean]❌ When set to true, does not save the token to the file system. Default: false

Programmatic Usage

Installation

npm install -D @spotifly/auth-token

Options

type Options = {
clientId: string;
clientSecret: string;
port?: number;
scopes?: string;
fileName?: string;
outDir?: string;
noEmit?: boolean;
};

Examples

  • CommonJS
const { authorize } = require('@spotifly/auth-token');

const token = await authorize({
clientId: 'clientId',
clientSecret: 'clientSecret',
port: 3000,
scopes: 'user-read-email user-top-read',
noEmit: true,
});
  • ES Modules
import { authorize } from '@spotifly/auth-token';

const token = await authorize({
clientId: 'clientId',
clientSecret: 'clientSecret',
port: 3000,
scopes: 'user-read-email user-top-read',
});
  • TypeScript
import { authorize, Options } from '@spotifly/auth-token';

const options: Options = {
clientId: 's',
clientSecret: 's',
noEmit: true,
};

const token = await authorize(options);