# Why does calling external API need to be proxied through Nextjs API route?

**URL:** <https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, nextjs-auth0\
**Created:** [December 22, 2022, 9:52pm UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997 "2022-12-22T21:52:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineetd](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vineetd/32/27086_2.png) [@vineetd](https://community.auth0.com/u/vineetd)\
**Post date:** [December 22, 2022, 9:52pm UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997/1 "2022-12-22T21:52:37Z")

</div>

I’m migrating an existing React app that uses auth0 react SDK to NextJs.

In the original app, the frontend makes a direct api call to the backend by passing access token in the authorization header that it obtained from the react auth0 sdk (using getTokenSilently api)

I came across this article, which seems to imply that with NextJS SDK calling external api would require proxying the api call through nextjs’s API route. That is [frontend] → nextjs api route → actual backend api gateway. This is because the frontend no longer has access to the access\_token, so the request is sent to the nextjs’s api route. Where it uses the session to look up the access token and then make another api call to the api gateway by passing the access token.

> **[How to Authenticate with Next.js and Auth0: A Guide for Every Deployment Model](https://auth0.com/blog/ultimate-guide-nextjs-authentication-auth0/)**
>
> Learn how and where to authenticate your user in the different deployment models that exist for Next.js. This guide explores the Custom S...

Is my understanding correct?

I’m concerned with this extra hop introduced by the proxy (latency/performance rason) but I also feel like I would need to write nextjs’s api route corresponding to every single route on the backend that is authenticated (I have about 70-80 apis endpoints).

---

<div class="post-metadata">

**Author:** ![petrgazarov](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/petrgazarov/32/46188_2.png) [@petrgazarov](https://community.auth0.com/u/petrgazarov)\
**Post date:** [April 10, 2023, 3:26am UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997/3 "2023-04-10T03:26:09Z")

</div>

I also have this question but haven’t found any documentation. I experimented with the encrypted cookie “appSession” set by Auth0. If your external API is on the same domain (or a subdomain), technically you can read this cookie and get the access token from there. To do this:

1. Call external API directly from the browser.
2. Decrypt the cookie on the server. Encryption key is AUTH0\_SECRET variable in Next.JS. The cookie contains user data, session token and id token.

Interested to see an example on how the whole flow would look like. Proxying requests through the Next.JS server feels like unnecessarily complicating things.

---

<div class="post-metadata">

**Author:** ![aleguern](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aleguern/32/45174_2.png) [@aleguern](https://community.auth0.com/u/aleguern)\
**Post date:** [April 24, 2023, 9:34pm UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997/4 "2023-04-24T21:34:25Z")

</div>

Hello, any news on this subject ?

---

<div class="post-metadata">

**Author:** ![justinechang39](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/justinechang39/32/46811_2.png) [@justinechang39](https://community.auth0.com/u/justinechang39)\
**Post date:** [May 1, 2023, 1:35pm UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997/5 "2023-05-01T13:35:40Z")

</div>

Looking for an answer to this as well, I also find it weird that I need to route everything through the api.

---

<div class="post-metadata">

**Author:** ![aleguern](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/aleguern/32/45174_2.png) [@aleguern](https://community.auth0.com/u/aleguern)\
**Post date:** [May 1, 2023, 7:02pm UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997/6 "2023-05-01T19:02:00Z")

</div>

My solution for now is calling the access token in get server side props, and calling my api on the client side of nextjs, not sure of this, I’ve read some solution with middlewares but it adds some latency to each api call

---

<div class="post-metadata">

**Author:** ![felix3](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/felix3/32/48057_2.png) [@felix3](https://community.auth0.com/u/felix3)\
**Post date:** [June 14, 2023, 4:21pm UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997/7 "2023-06-14T16:21:35Z")

</div>

For what it’s worth, we have a workaround by passing the accessToken to the frontend.  
I have a brief example code here: [Next.js & External API without Proxying requests through Next.js backend](https://community.auth0.com/t/next-js-external-api-without-proxying-requests-through-next-js-backend/109244)

I’m just looking for some kind of official confirmation that this is safe to do.

---

<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 3, 2023, 10:28pm UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997/8 "2023-10-03T22:28:37Z")

</div>

Thanks for sharing @felix3 !

Sharing this issue against nextjs-auth0 that goes into a bit more detail on this for future reference:

> <https://github.com/auth0/nextjs-auth0/issues/358>
>
> Loving this sdk and it meets our needs pretty closely!
> 
> We're hoping to have t…wo access paths to our API endpoint:
> 1. Server-side rendering path - API requests go \[nextjs on server\] -\> \[api server\]
> 2. Client-side rendering path - API requests go directly \[nextjs in browser\] -\> \[api server\]
> 
> 
> For the client-side rendering path - is there anything wrong with exposing a nextjs API (/pages/api/accessToken) that just returns the value of \`await getAccessToken()\`? The idea is to make the access token available to the browser so it can make requests directly to our API server.
> 
> 
> Why don't we want to use the Auth0/React SDK for single page apps? Because we want to support server-side rendering where the data is loaded by the server side of nextjs.
> 
> Why don't we want to write an API wrapper so requests are proxied from \[nextjs\]/api/endpoint to our actual API server? So we don't push all data access unnecessarily through nextjs and require lock-step scaling between nextjs and our API endpoint servers.
> 
> 
> Is this a feasible approach? Am I headed down a dark path? Thanks y'all!

---

<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 17, 2023, 10:28pm UTC](https://community.auth0.com/t/why-does-calling-external-api-need-to-be-proxied-through-nextjs-api-route/96997/9 "2023-10-17T22:28:54Z")

</div>

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