# How to call my external API?

**URL:** <https://community.auth0.com/t/how-to-call-my-external-api/117354>\
**Category:** Get Help\
**Tags:** authentication-sessions, sessions\
**Created:** [September 28, 2023, 3:40pm UTC](https://community.auth0.com/t/how-to-call-my-external-api/117354 "2023-09-28T15:40:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roalkege](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/roalkege/32/51230_2.png) [@Roalkege](https://community.auth0.com/u/Roalkege)\
**Post date:** [September 28, 2023, 3:40pm UTC](https://community.auth0.com/t/how-to-call-my-external-api/117354/1 "2023-09-28T15:40:36Z")

</div>

1. I am trying to fetch data from my NextJS frontend from my backend running with fastapi. Both are using auth0 for authentication. Now the NextJS with package creates a session cookie but the API wants a JWT Token. Cant I use `credentials: "include"`?  
I found the [docs](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route) and do I really have to fetch data from the next.js API which fetch data from my external api? Normally I use useSWR for my fetches.

I did the following according to:

```auto
import { getAccessToken } from "@auth0/nextjs-auth0";
const defaultFetcherGet = async (url: string) => {
  const { accessToken } = await getAccessToken();
  return fetch(url, {
    method: "GET",
    headers: {
      authorization: `Bearer ${accessToken}`,
      "Content-Type": "application/json",
    },
  }).then((r) => r.json());
};
export default defaultFetcherGet;

```

But I am getting:  
`Module not found: Can't resolve 'fs'`

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [September 28, 2023, 5:40pm UTC](https://community.auth0.com/t/how-to-call-my-external-api/117354/3 "2023-09-28T17:40:16Z")

</div>

Hi @Roalkege,

Welcome to the Auth0 Community!

> [@Roalkege](#):
>
> I found the [docs](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route) and do I really have to fetch data from the next.js API which fetch data from my external api? Normally I use useSWR for my fetches.

That’s an API protected route, so you must get your App’s access token to access the API route and then fetch the data from your external API.

According to the `Module not found: Can't resolve 'fs'` error, it looks like the module may not have been included in your node-modules or packages in your app. Have you made sure that the ‘fs’ module was added to your app’s node-modules and packages?

---

<div class="post-metadata">

**Author:** ![Roalkege](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/roalkege/32/51230_2.png) [@Roalkege](https://community.auth0.com/u/Roalkege)\
**Post date:** [September 28, 2023, 6:34pm UTC](https://community.auth0.com/t/how-to-call-my-external-api/117354/4 "2023-09-28T18:34:26Z")

</div>

Hey I dont use `fs`. I added `const { accessToken } = await getAccessToken();` to my code and now this error pop up.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [September 29, 2023, 3:59pm UTC](https://community.auth0.com/t/how-to-call-my-external-api/117354/5 "2023-09-29T15:59:08Z")

</div>

Hi @Roalkege,

Thanks for following up.

> [@Roalkege](#):
>
> I dont use `fs`. I added `const { accessToken } = await getAccessToken();` to my code and now this error pop up.

I have not been able to reproduce the issue you mentioned.

Can you confirm if removing the code you added fixes the error?

In any case, I noticed that your code does not have error handling to handle errors that might occur during the `getAccessToken()` or `fetch` operation. You can consider adding try-catch blocks to handle potential errors.

Thanks,  
Rueben

---

<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:** [October 13, 2023, 3:59pm UTC](https://community.auth0.com/t/how-to-call-my-external-api/117354/6 "2023-10-13T15:59:44Z")

</div>

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