{"id":15532,"library":"axios-oauth-client","title":"Axios OAuth Client","description":"axios-oauth-client is a utility library designed to simplify the implementation of various OAuth 2.0 grant types when using the Axios HTTP client. Currently at version 2.2.0, it provides dedicated functions for common flows such as Authorization Code, Owner Credentials (Resource Owner Password Credentials), Client Credentials, and Refresh Token grants. The library integrates directly with Axios, allowing developers to instantiate OAuth client functions with an Axios instance, a token endpoint URL, and client credentials. It streamlines the process of obtaining access tokens by handling the underlying HTTP requests and OAuth 2.0 specific parameter encoding. A key differentiator is its straightforward, function-based API specifically tailored for Axios users, focusing on providing the primitives for token acquisition rather than a comprehensive OAuth client state management system. This means users are responsible for token storage, renewal logic, and attaching tokens to subsequent requests. It's built for Node.js environments (engines >= 14) and ships with TypeScript types, ensuring type safety for its users.","status":"active","version":"2.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/compwright/axios-oauth-client","tags":["javascript","oauth","oauth2","axios","interceptor","typescript"],"install":[{"cmd":"npm install axios-oauth-client","lang":"bash","label":"npm"},{"cmd":"yarn add axios-oauth-client","lang":"bash","label":"yarn"},{"cmd":"pnpm add axios-oauth-client","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer dependency for HTTP requests.","package":"axios","optional":false}],"imports":[{"note":"The library primarily uses ES Modules, though CommonJS might be supported via transpilation.","wrong":"const { authorizationCode } = require('axios-oauth-client')","symbol":"authorizationCode","correct":"import { authorizationCode } from 'axios-oauth-client'"},{"note":"Functions are exported as named exports, not default.","wrong":"import clientCredentials from 'axios-oauth-client'","symbol":"clientCredentials","correct":"import { clientCredentials } from 'axios-oauth-client'"},{"note":"Ensure your environment supports ES Module imports (Node.js >=14 with appropriate configuration, or bundlers).","wrong":"const refreshToken = require('axios-oauth-client').refreshToken","symbol":"refreshToken","correct":"import { refreshToken } from 'axios-oauth-client'"}],"quickstart":{"code":"import axios from 'axios';\nimport { clientCredentials, refreshToken } from 'axios-oauth-client';\n\nconst tokenEndpoint = 'https://oauth.com/2.0/token';\nconst clientId = process.env.OAUTH_CLIENT_ID ?? 'YOUR_CLIENT_ID';\nconst clientSecret = process.env.OAUTH_CLIENT_SECRET ?? 'YOUR_CLIENT_SECRET';\n\nasync function authenticateAndRefresh() {\n  const axiosInstance = axios.create();\n\n  // Client Credentials Grant\n  console.log('Attempting Client Credentials grant...');\n  const getClientCredentials = clientCredentials(\n    axiosInstance,\n    tokenEndpoint,\n    clientId,\n    clientSecret\n  );\n  const initialAuth = await getClientCredentials('read write');\n  console.log('Initial access token acquired:', initialAuth.access_token);\n\n  // Simulate refreshing the token\n  if (initialAuth.refresh_token) {\n    console.log('Attempting Refresh Token grant...');\n    const getRefreshToken = refreshToken(\n      axiosInstance,\n      tokenEndpoint,\n      clientId,\n      clientSecret\n    );\n    const refreshedAuth = await getRefreshToken(initialAuth.refresh_token, 'read write');\n    console.log('Refreshed access token acquired:', refreshedAuth.access_token);\n  } else {\n    console.log('No refresh token available from client credentials grant. Skipping refresh example.');\n  }\n}\n\nauthenticateAndRefresh().catch(error => {\n  console.error('Authentication error:', error.response?.data || error.message);\n});","lang":"typescript","description":"Demonstrates how to obtain an access token using the Client Credentials grant and then refresh it."},"warnings":[{"fix":"Ensure 'axios' is installed: `npm install axios` or `yarn add axios`.","message":"This library requires 'axios' as a peer dependency. It must be installed separately alongside 'axios-oauth-client' and meet the specified version range (e.g., '^1.2.1').","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Implement an Axios interceptor or manual logic to store tokens, check expiry, refresh tokens, and attach the current access token to outbound requests.","message":"axios-oauth-client provides functions for acquiring tokens but does not handle token storage, automatic refreshing before expiry, or attaching tokens to subsequent requests. Users must implement their own token management logic.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Developers must explicitly call the appropriate grant function (e.g., `clientCredentials`, `refreshToken`) based on their application's current authentication state.","message":"The library directly exposes functions for different OAuth 2.0 grant types. There is no unified client instance that manages state across different grant types or automatically uses a refresh token if available from a previous grant.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Install axios: `npm install axios` or `yarn add axios`.","cause":"The peer dependency 'axios' is not installed or not resolvable by the module system.","error":"Cannot find module 'axios'"},{"fix":"Ensure you have a compatible version of Axios (e.g., `^1.2.1`) and that you are importing `axios` as a default import: `import axios from 'axios'`.","cause":"An incompatible or incorrect version of Axios is installed, or Axios was not imported correctly.","error":"TypeError: axios.create is not a function"},{"fix":"Verify all OAuth client credentials, endpoint URLs, and grant-specific parameters (like the authorization code or refresh token) are correct and match the configuration on your OAuth provider. Check the response body for specific error details from the OAuth server.","cause":"Incorrect OAuth client ID, client secret, token endpoint URL, grant type parameters (e.g., authorization code), or scopes were provided to the OAuth server.","error":"Request failed with status code 400 (or other HTTP error) when calling an OAuth grant function."}],"ecosystem":"npm"}