# Next.js & External API without Proxying requests through Next.js backend

**URL:** <https://community.auth0.com/t/next-js-external-api-without-proxying-requests-through-next-js-backend/109244>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, nextjs-auth0\
**Created:** [June 14, 2023, 4:19pm UTC](https://community.auth0.com/t/next-js-external-api-without-proxying-requests-through-next-js-backend/109244 "2023-06-14T16:19:28Z")\
**Posts on this page:** 4\
**Page:** 1

<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:19pm UTC](https://community.auth0.com/t/next-js-external-api-without-proxying-requests-through-next-js-backend/109244/1 "2023-06-14T16:19:28Z")

</div>

We are developing a Next.js frontend and an backend API that accepts Bearer Tokens (JWTs). We keep the backend separate for infrastructure and code management / framework reasons.  
How is it possible to securely access the backend API from the Next.js frontend in the browser side?  
I see in the [official docs](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route) they only talk about “Access an External API from an API Route” (server-side) but **what if I want to access an External API from a Page (browser-side)?**

We have come up with the idea creating a NextJS backend endpoint just to pass the accessToken to the NextJS frontend so that it can directly call the External API without proxying through the NextJS backend.  
This is our example code on the NextJS backend side:

```auto
import { getAccessToken, withApiAuthRequired } from '@auth0/nextjs-auth0'

export default withApiAuthRequired(async function products(req: any, res: any) {
    const { accessToken } = await getAccessToken(req, res, {
      scopes: ['openid', 'profile', 'email', 'refresh_token']
    })
    res.status(200).json({ accessToken, message: 'Ready to call api with accessToken' })
  }
})

```

**Is this safe?** - or better: **Under what circumstances is this safe?**  
We understand the Access Token is a JWT with a fixed duration, that we intend to configure to be quite short (5-15min) so that they need to be frequently refreshed using the same method.

---

<div class="post-metadata">

**Author:** ![alex.brett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex.brett/32/60157_2.png) [@alex.brett](https://community.auth0.com/u/alex.brett)\
**Post date:** [November 21, 2024, 7:56pm UTC](https://community.auth0.com/t/next-js-external-api-without-proxying-requests-through-next-js-backend/109244/3 "2024-11-21T19:56:40Z")

</div>

Hello @felix3! Welcome to the community.

This is safe because the Next.js SDK is used for Regular Web App (RWA), which are private clients and can securely store secrets, specifically the client\_secret. This is completely safe.

We do recommend short lived access tokens, but be cautious of hitting [rate limits](https://auth0.com/docs/troubleshoot/customer-support/operational-policies/rate-limit-policy/rate-limit-configurations). It is possible to cache API Access Tokens which I’ll link here: [Caching Management API Access Tokens in Login Action](https://community.auth0.com/t/caching-management-api-access-tokens-in-login-action/95855). This can help with using the same token instead of always getting a new one and can be done using [actions](https://auth0.com/docs/customize/actions/explore-triggers/machine-to-machine-trigger/credentials-exchange-api-object).

Here are some more docs that may help:  
[  
Get Management API Access Tokens for Production](https://auth0.com/docs/secure/tokens/access-tokens/management-api-access-tokens/get-management-api-access-tokens-for-production)

> **[getAccessToken | @auth0/nextjs-auth0](https://auth0.github.io/nextjs-auth0/functions/index.getAccessToken-1.html)**
>
> Documentation for @auth0/nextjs-auth0

I hope this helps!

Best,  
Alex

---

<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:** [December 5, 2024, 7:57pm UTC](https://community.auth0.com/t/next-js-external-api-without-proxying-requests-through-next-js-backend/109244/4 "2024-12-05T19:57:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dawid.matuszczyk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dawid.matuszczyk/32/74550_2.png) [@dawid.matuszczyk](https://community.auth0.com/u/dawid.matuszczyk)\
**Post date:** [December 9, 2024, 5:19pm UTC](https://community.auth0.com/t/next-js-external-api-without-proxying-requests-through-next-js-backend/109244/5 "2024-12-09T17:19:09Z")

</div>

Hey team!

If you’re curious about the new Auth0 Next.js SDK and how it works with Next.js 15 and Turbopack, now’s a great time to get involved!

We’re hosting an [Ask Me Anything](https://community.auth0.com/t/december-10-auth0-community-ask-me-anything-unlocking-the-power-of-the-auth0-and-next-js/152331) session with Kevin Lillybridge, Auth0’s Group Product Manager, on December 10, from 9 AM to 11 AM PST. You’ll receive detailed, written answers straight from the experts - plus earn a special badge. Don’t miss this opportunity to gain valuable insights and supercharge your Next.js development!

[Auth0 by Okta Community Ask Me Anything: Unlocking the Power of the Auth0 and Next.js](https://community.auth0.com/t/december-10-auth0-community-ask-me-anything-unlocking-the-power-of-the-auth0-and-next-js/152331)
