# @scienta/axios-oauth2

> Cacheable token interceptor for oauth requests with axios

Latest version **1.3.0** (published 2025-05-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @scienta/axios-oauth2
pnpm add @scienta/axios-oauth2
yarn add @scienta/axios-oauth2
bun add @scienta/axios-oauth2
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2025-05-08 |
| First published | 2021-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 15.8 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 6 |
| Maintainers | harmenm, hikariii, scienta-bot, anner_, luukvhoudt |

## Links

- npm: https://www.npmjs.com/package/@scienta/axios-oauth2
- Repository: https://github.com/ScientaNL/axios-oauth2
- Homepage: https://github.com/ScientaNL/axios-oauth2#readme
- Issues: https://github.com/ScientaNL/axios-oauth2/issues
- npm.io page: https://npm.io/package/@scienta/axios-oauth2

## Dependencies (3)

- [qs](https://npm.io/package/qs.md) ^6.9.0
- [axios](https://npm.io/package/axios.md) ^0.21.1
- [@scienta/locking-cache](https://npm.io/package/@scienta/locking-cache.md) ^1.3.0

## Recent versions

- 1.3.0 (latest) — 2025-05-08
- 1.2.1 — 2023-05-16
- 1.2.1-beta.2 — 2023-05-16
- 1.2.1-beta — 2023-05-16
- 1.2.0 — 2021-04-14
- 1.1.2 — 2021-04-09
- 1.1.0 — 2021-04-09
- 1.0.3 — 2021-03-23
- 1.0.2 — 2021-03-23
- 1.0.1 — 2021-03-23

## README

# axios-oauth2
This library makes oauth with axios easy.
There is a oauth-client, a token interceptor and caching.

## Quickstart
You can use only the client, or combine it with the token interceptor and caching.

### Simple client usage
```ts
import axios from 'axios';
import {clientFactory} from '@scienta/axios-oauth';
const client = clientFactory(axios.create(), {
  url: 'https://oauth.com/2.0/token',
  grant_type: 'client_credentials',
  client_id: 'foo',
  client_secret: 'bar',
  scope: 'baz'
});

const auth = await client(); // => { "access_token": "...", "expires_in": 900, ... }
```

### Client usage with interceptor
```ts
import axios from 'axios';
import {clientFactory, interceptorFactory} from '@scienta/axios-oauth';
const axiosInstance = axios.create();
axiosInstance.interceptors.request.use(
  interceptorFactory(clientFactory(axiosInstance, {
    url: 'https://oauth.com/2.0/token',
    grant_type: 'client_credentials',
    client_id: 'foo',
    client_secret: 'bar',
    scope: 'baz'
  }))
);
axiosInstance.get('https://oauth.com/2.0/protectedResource')
```

### Client usage with token-caching interceptor
```ts
import axios from 'axios';
import {clientFactory, interceptorFactory, LockingTokenCache} from '@scienta/axios-oauth';

const axiosInstance = axios.create();
axiosInstance.interceptors.request.use(
  interceptorFactory(
    clientFactory(axiosInstance, {
      url: 'https://oauth.com/2.0/token',
      grant_type: 'client_credentials',
      client_id: 'foo',
      client_secret: 'bar',
      scope: 'baz'
    }),
    new LockingTokenCache()
  )
);

axiosInstance.get('https://oauth.com/2.0/protectedResource')
```

## Locking cache (used by LockingTokenCache)
The `LockingTokenCache` class uses a separate library for locking and caching.
Locking cache is a caching-library suited for simple and complex locking while caching.
Locking with caching ensures that only one request is executed to request a token.
Want to know more? [@scienta/locking-cache](https://www.npmjs.com/package/@scienta/locking-cache).

---
_Source: https://npm.io/package/@scienta/axios-oauth2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
