# How to create a proper custom axios instance for Orval?

**URL:** <https://community.auth0.com/t/how-to-create-a-proper-custom-axios-instance-for-orval/123196>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, auth0-react\
**Created:** [December 16, 2023, 7:57am UTC](https://community.auth0.com/t/how-to-create-a-proper-custom-axios-instance-for-orval/123196 "2023-12-16T07:57:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matamatanot](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/matamatanot/32/53317_2.png) [@matamatanot](https://community.auth0.com/u/matamatanot)\
**Post date:** [December 16, 2023, 7:57am UTC](https://community.auth0.com/t/how-to-create-a-proper-custom-axios-instance-for-orval/123196/1 "2023-12-16T07:57:52Z")

</div>

We are considering using Orval.

> **[Custom Axios instance](https://orval.dev/guides/custom-axios)**

We are currently using React and @auth0/auth0-react.

```auto
const useAuthBearerToken = () => {
  const { getAccessTokenSilently } = useAuth0();

  const getBearerToken = async () => {
    const token = await getAccessTokenSilently();
    return `Bearer ${token}` as const;
  };

  return { getBearerToken };
};

export const useAxiosWithAuthHeader = () => {
  const { getBearerToken } = useAuthBearerToken();
  const router = useRouter();

  const instanceWithAuthHeader = axios.create({
    baseURL: API_ROOT_DOMAIN,
    timeout: 10000,
  });

  instanceWithAuthHeader.interceptors.request.use(async (config) => {
    try {
      const headers = await getBearerToken();
      config.headers.Authorization = headers;
    } catch (error) {
      if ((error as Auth0Error).error === 'invalid_grant') {
        router.push('/session-expired');
        return config;
      }
      router.push('/401');
    }
    return config;
  });

  return instanceWithAuthHeader;
};

```

But, the `useAuth0` can only be used in React Component.  
I want to set a Bearer Token in the authorization header for all requests.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [December 22, 2023, 4:02pm UTC](https://community.auth0.com/t/how-to-create-a-proper-custom-axios-instance-for-orval/123196/3 "2023-12-22T16:02:01Z")

</div>

Hi @matamatanot,

Welcome to the Auth0 Community!

The Auth0 React SDK isn’t intended for use outside of components, but there is a workaround you can use to get the token outside of your component.

Check out this thread:

> [@Using auth0 outside of React Components](https://community.auth0.com/t/using-auth0-outside-of-react-components/38904):
>
> Hey there, I followed along with the React SDK quickstart and I have a couple of questions. The quickstart guide in part2 makes http requests directly in components, which is nice and easy because auth0 available via context. However I like to wrap API interactions into a service class that is react agnostic to promote separation of concerns for presentation vs infrastructure. To achieve this whilst maintaining a single auth0 instance I am pulling auth0 out of react context and constructor inj…

Specifically, this code:

> <https://gist.github.com/klequis/fd9c3cf5b025cc26a282ea30ef8ff6da>

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [January 5, 2024, 4:02pm UTC](https://community.auth0.com/t/how-to-create-a-proper-custom-axios-instance-for-orval/123196/4 "2024-01-05T16:02:31Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
