# spotify-auth-token

> A simple utility for retrieving Spotify authentication and refresh tokens. Supports programmatic and CLI use

Latest version **4.3.2** (published 2022-06-08) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install spotify-auth-token
pnpm add spotify-auth-token
yarn add spotify-auth-token
bun add spotify-auth-token
```

Provides the command `spotify-auth-token`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 4.3.2 |
| Published | 2022-06-08 |
| First published | 2021-12-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 2 |
| Unpacked size | 25.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Eric Egli |
| Maintainers | eeric |
| Keywords | spotify, auth, token, refresh token, cli, typescript |

## Links

- npm: https://www.npmjs.com/package/spotify-auth-token
- Repository: https://github.com/eegli/spotify-auth-token
- npm.io page: https://npm.io/package/spotify-auth-token

## Dependencies (2)

- [fs-extra](https://npm.io/package/fs-extra.md) ^10.0.1
- [@eegli/tinyparse](https://npm.io/package/@eegli/tinyparse.md) ^0.4.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 4.3.2 (latest) — 2022-06-08
- 4.3.1 — 2022-02-28
- 4.3.0 — 2022-02-24
- 4.2.2 — 2022-02-22
- 4.2.1 — 2022-02-22
- 4.2.0 — 2022-02-12
- 4.1.0 — 2022-02-12
- 4.0.0 — 2022-01-09
- 3.2.0 — 2022-01-02
- 3.1.0 — 2021-12-30
- 2.1.1 — 2021-12-24
- 2.1.0 — 2021-12-24
- 2.0.1 — 2021-12-24
- 2.0.0 — 2021-12-24
- 1.2.3 — 2021-12-23
- … 6 more at https://npm.io/package/spotify-auth-token/versions

## README

# Spotify Auth Token

![npm](https://img.shields.io/npm/v/spotify-auth-token) ![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/eegli/spotify-auth-token/ci-unit-tests/main) [![codecov](https://codecov.io/gh/eegli/spotify-auth-token/branch/main/graph/badge.svg?token=2GK6L7KXTD)](https://codecov.io/gh/eegli/spotify-auth-token)

### A simple and lightweight implementation of Spotify's Authorization Code Flow.

- CLI and programmatic use
- Customizable
- Gets both short-lived and refresh tokens

Implements the authorization code flow according to the [Spotify docs](https://developer.spotify.com/documentation/general/guides/authorization/code-flow/).

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](https://github.com/eegli/spotify-history).

## Example result

The short-lived `access_token` expires after 1 hour. The `refresh_token` can be used to request new short-lived tokens whenever needed.

```json
{
  "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"
}
```

## CLI Usage

The simplest way to get a token is via `npx` - no installation required.

```bash
npx spotify-auth-token@latest --clientId f40c6b --clientSecret 0199f38a
```

Optional arguments example:

```bash
npx spotify-auth-token@latest --clientId f40c6b --clientSecret 0199f38a --port 8000 --scopes "user-library-read"
```

With short flags:

```bash
npx spotify-auth-token@latest -ci f40c6b -cs 0199f38a -p 8000 -s "user-library-read user-top-read"
```

### Options

| Flag                      | **_(required?)_** Description                                                                                                                                  |
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `-ci` \| `--clientId`     | ✅ Spotify client id                                                                                                                                           |
| `-cs` \| `--clientSecret` | ✅ Spotify client secret                                                                                                                                       |
| `-p` \| `--port`          | ❌ Port for localhost redirect url. Default: `3000`                                                                                                            |
| `-s` \| `--scopes`        | ❌ [Spotify auth scopes](https://developer.spotify.com/documentation/general/guides/authorization/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                                                                                                                              |

## Programmatic

### Installation

```bash
yarn add -D spotify-auth-token
```

or

```bash
npm install spotify-auth-token --save-dev
```

### Options

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.

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

### Examples

- CommonJS

```js
const { authorize } = require('spotify-auth-token/dist/authorize');

const token = await authorize({
  clientId: 'clientId',
  clientSecret: 'clientSecret',
  port: 3000,
  scopes: 'user-read-email user-top-read',
  noEmit: true,
});
```

- ES Modules

```js
import authorize from 'spotify-auth-token';

const token = await authorize({
  clientId: 'clientId',
  clientSecret: 'clientSecret',
  port: 3000,
  scopes: 'user-read-email user-top-read',
});
```

- TypeScript

```ts
import authorize, { UserConfig } from 'spotify-auth-token';

const config: UserConfig = {
  clientId: 'clientId',
  clientSecret: 'clientSecret',
  port: 3000,
  scopes: 'user-read-email user-top-read',
};

const token = await authorize(config);
```

---
_Source: https://npm.io/package/spotify-auth-token · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
