- CLI and programmatic use
- Customizable
- Gets both short-lived and refresh tokens
Implements the authorization code flow according to the Spotify docs.
Use it for any application that interacts with the Spotify API and needs to run independently. The generated refresh token can be used to request new short-lived tokens on the fly.
This helper was mainly developed to simplify my Spotify history scrobbler.
The short-lived access_token
expires after 1 hour. The refresh_token
can be used to request new short-lived tokens whenever needed.
{
"access_token": "BQCmqcTBlB2uVBEzLhykD9dYi",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "AQB-B_Kg3Kn5nL3ByG1hQA",
"scope": "user-library-read",
"date_obtained": "Sat, 12 Feb 2022 10:25:41 GMT"
}
The simplest way to get a token is via npx
- no installation required.
npx spotify-auth-token@latest --clientId f40c6b --clientSecret 0199f38a
Optional arguments example:
npx spotify-auth-token@latest --clientId f40c6b --clientSecret 0199f38a --uri 8000 --scopes "user-library-read"
With short flags:
npx spotify-auth-token@latest -ci f40c6b -cs 0199f38a -u 8000 -s "user-library-read user-top-read"
Flag | (required?) Description |
---|---|
-ci | --clientId |
✅ Spotify client id |
-cs | --clientSecret |
✅ Spotify client secret |
-u | --uri |
❌ Redirect url. Default: https://localhost:3000 |
-s | --scopes |
❌ Spotify auth scopes, separated by a space. Default: 'user-read-email' |
-o | --outDir |
❌ Custom output directory relative to the current directory |
-f | --fileName |
❌ Custom file name for the token |
yarn add -D spotify-auth-token
or
npm install spotify-auth-token --save-dev
The options and defaults are the same as for the CLI plus noEmit
. If
noEmit
is set to true
, the token will not be saved to the file
system.
type Options = {
clientId: string;
clientSecret: string;
port?: number;
scopes?: string;
fileName?: string;
outDir?: string;
noEmit?: boolean;
};
- CommonJS
const { authorize } = require('spotify-auth-token/dist/authorize');
const token = await authorize({
clientId: 'clientId',
clientSecret: 'clientSecret',
uri: "http://localhost:3000",
scopes: 'user-read-email user-top-read',
noEmit: true,
});
- ES Modules
import authorize from 'spotify-auth-token';
const token = await authorize({
clientId: 'clientId',
clientSecret: 'clientSecret',
uri: "http://localhost:3000",
scopes: 'user-read-email user-top-read',
});
- TypeScript
import authorize, { UserConfig } from 'spotify-auth-token';
const config: UserConfig = {
clientId: 'clientId',
clientSecret: 'clientSecret',
uri: "http://localhost:3000",
scopes: 'user-read-email user-top-read',
};
const token = await authorize(config);