# Calling Protected Endpoints using Axios

**URL:** <https://community.auth0.com/t/calling-protected-endpoints-using-axios/119402>\
**Category:** Get Help\
**Tags:** auth0-spa-js, sdks-quickstarts\
**Created:** [October 25, 2023, 11:05pm UTC](https://community.auth0.com/t/calling-protected-endpoints-using-axios/119402 "2023-10-25T23:05:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FreshPineapples](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/freshpineapples/32/51958_2.png) [@FreshPineapples](https://community.auth0.com/u/FreshPineapples)\
**Post date:** [October 25, 2023, 11:05pm UTC](https://community.auth0.com/t/calling-protected-endpoints-using-axios/119402/1 "2023-10-25T23:05:02Z")

</div>

Hello, I’m a new user of Auth0 and I’m also learning how to build a MERN stack application at the moment. I’m currently using Axios to call the protected backend API, however I keep getting 401 unauthorized when I try to call it and it doesn’t let me access the backend. I’m not sure if I’m passing the Authorization header correctly in my get request. Ex. below

```auto
const getAll = async (accessToken) => {
  const request = await axios.get(baseUrl, placeholder, {
    headers: {
      "content-type": "application/json",
      Authorization: `Bearer ${accessToken}`,
    },
  });
  return request.then((response) => response.data);
};

```

This is the useEffect function used to generate the access token.

```auto

  const { getAccessTokenSilently } = useAuth0();

  useEffect(() => {
    const getData = async () => {
      const accessToken = await getAccessTokenSilently();
      noteService.getAll(accessToken).then((initialNotes) => {
        setNotes(initialNotes);
      });
    };
    getData();
  }, []);

```

---

<div class="post-metadata">

**Author:** ![spoudel](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/spoudel/32/51376_2.png) [@spoudel](https://community.auth0.com/u/spoudel)\
**Post date:** [October 26, 2023, 6:54pm UTC](https://community.auth0.com/t/calling-protected-endpoints-using-axios/119402/3 "2023-10-26T18:54:22Z")

</div>

Hey!!

If I were you I would make sure that the client is allowed to access the API resource. Also, I would make sure that the credentials I pass to get the access tokens, for instance, the audience value, are set properly.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 26, 2023, 7:34pm UTC](https://community.auth0.com/t/calling-protected-endpoints-using-axios/119402/4 "2023-10-26T19:34:55Z")

</div>

Exactly what @spoudel said!

You can configure the audience in `Auth0ProviderOptions` → [AuthorizationParams](https://auth0.github.io/auth0-react/interfaces/AuthorizationParams.html)

---

<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:** [November 22, 2023, 9:36pm UTC](https://community.auth0.com/t/calling-protected-endpoints-using-axios/119402/5 "2023-11-22T21:36:18Z")

</div>

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