# Next.js 13 how to Access an External API from an API Route using App Router api route?

**URL:** <https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459>\
**Category:** Get Help\
**Tags:** rotating-refresh-tokens, sessions\
**Created:** [September 18, 2023, 8:06am UTC](https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459 "2023-09-18T08:06:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arundas.tc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/arundas.tc/32/49957_2.png) [@arundas.tc](https://community.auth0.com/u/arundas.tc)\
**Post date:** [September 18, 2023, 8:06am UTC](https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459/1 "2023-09-18T08:06:25Z")

</div>

I have this code that supposedly accesses an external API using the Next.js SDK.

```ts
// app/api/test/route.ts (Typescript)
import { NextResponse } from 'next/server'
import { getAccessToken, withApiAuthRequired } from '@auth0/nextjs-auth0'

export const GET = withApiAuthRequired(async () => {
  // If your access token is expired and you have a refresh token
  // `getAccessToken` will fetch you a new one using the `refresh_token` grant
  const { accessToken } = await getAccessToken()
  const response = await fetch('https://my-api.my.com/test', {
    headers: {
      Authorization: `Bearer ${accessToken}`,
    },
  })
  const data = await response.text()
  return NextResponse.json({ data })
})

```

The problem is, it doesn’t refresh the access token and rotate the refresh token. It is always trying to refresh with the expired token. I think it works exactly once, then from second time I get the error (in server):

```ts
- error AccessTokenError: The request to refresh the access token failed. CAUSE: invalid_grant (Unknown or invalid refresh token.)
    at NodeClient.refresh (webpack-internal:///(rsc)/./node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/node-client.js:158:19)

```

The only way to refresh the access token is to logout and log in again, which is obvouisly not ideal and totally defeats the purpose of refresh tokens.

There is no example to guide how to get rotating refresh tokens to work correctly in a Next.js 13 project using app router.

I am already using “Regular Web Application” for this. I have set the correct audience and scope correctly in environment, including `offline_access`. I’m lost how to solve this. Is using “reusable refresh token” the only way? There’s no way to use the rotating one?

PS: It works correctly in Postman when directly accessing `https://my-api.my.com/test` after setting up the OAuth 2.0 to authenticate with Auth0. So the problem is unlikely to be on my backend.

---

<div class="post-metadata">

**Author:** ![arundas.tc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/arundas.tc/32/49957_2.png) [@arundas.tc](https://community.auth0.com/u/arundas.tc)\
**Post date:** [October 2, 2023, 3:20pm UTC](https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459/2 "2023-10-02T15:20:16Z")

</div>

Noting here that I didn’t find any solution for the above. In the end, we just disabled “Refresh Token Rotation” because we don’t have a solution.

I hope the documentation can be improved and a solution can be made public by Auth0.

---

<div class="post-metadata">

**Author:** ![Pipboy](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pipboy/32/57162_2.png) [@Pipboy](https://community.auth0.com/u/Pipboy)\
**Post date:** [May 16, 2024, 5:53pm UTC](https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459/3 "2024-05-16T17:53:29Z")

</div>

I’m a little late on the subject, but I think I’ve found an alternative to this problem.

Instead of using getAccessToken(), try using getSession(). This will retrieve the session access token (which has normally been refreshed).

The problem is that a token may expire while the background session remains active. In this case, the user will not be automatically redirected to the login page (because his session is active) and the token obtained by getAccessToken() will have expired.

---

<div class="post-metadata">

**Author:** ![arundas.tc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/arundas.tc/32/49957_2.png) [@arundas.tc](https://community.auth0.com/u/arundas.tc)\
**Post date:** [July 24, 2024, 7:12pm UTC](https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459/4 "2024-07-24T19:12:21Z")

</div>

We re-enabled Rotating Refresh Tokens, and now we’re facing the same problem again. I haven’t tried it in the API route (the code I shared in my original post) but when I tried using getSession().accessToken in a Server Action, it just expired after a given time, but didn’t refresh. After the expiry the whole authentication just started failing.

Anyway, the problem with getAccessToken() is that it doesn’t work correctly in Server Actions. It seems to always use the original session, even after a refresh. In fact, calling getSession() seems to always give the original session, not the updated session after a refresh, even if the cookies contain the updated session! This seems to be a bug in how the SDK handles server components (RSC), or it’s something weird with Vercel itself.

---

<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:** [July 26, 2024, 1:17am UTC](https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459/6 "2024-07-26T01:17:15Z")

</div>

Hi @arundas.tc,

When refreshing tokens, you need to ensure that the session is updated to extend the session. You can do this by including the `refresh: true` parameter.

For example:

```auto
export default async function MyHandler(req, res) {
  const accessToken = await getAccessToken(req, res, {
    refresh: true,
    afterRefresh,
  });
};

```

(Reference: [AccessTokenRequest | @auth0/nextjs-auth0](https://auth0.github.io/nextjs-auth0/interfaces/session_get_access_token.AccessTokenRequest.html#refresh))

Let me know how this goes for you.

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:** [August 9, 2024, 1:17am UTC](https://community.auth0.com/t/next-js-13-how-to-access-an-external-api-from-an-api-route-using-app-router-api-route/116459/7 "2024-08-09T01:17:27Z")

</div>

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