# How to properly use protected api routes in Next.js?

**URL:** <https://community.auth0.com/t/how-to-properly-use-protected-api-routes-in-next-js/193489>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, nextjs-auth0\
**Created:** [October 22, 2025, 9:46am UTC](https://community.auth0.com/t/how-to-properly-use-protected-api-routes-in-next-js/193489 "2025-10-22T09:46:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldruzicka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ronaldruzicka/32/76814_2.png) [@ronaldruzicka](https://community.auth0.com/u/ronaldruzicka)\
**Post date:** [October 22, 2025, 9:46am UTC](https://community.auth0.com/t/how-to-properly-use-protected-api-routes-in-next-js/193489/1 "2025-10-22T09:46:33Z")

</div>

Hi, I would like to ask how to properly use protected API Routes in Next.js using Next.js SDK.

In the migration guide from v3 to v4 you mention that `withApiAuthRequired` has been removed from the SDK, but it’s still there. And it is also used as an example of how to protect API routes in App router.

But when I actually use it, it shows me this typescript error:  
`The inferred type of 'GET' cannot be named without a reference to '../../../../../../packages/nextjs/node_modules/@auth0/nextjs-auth0/dist/server/helpers/with-api-auth-required'. This is likely not portable. A type annotation is necessary.`

I can type it like this, but the `NextApiHandler`is not for App router API routes is it?

`export const ` **`GET`** `: NextApiHandler<User> = auth0Client.withApiAuthRequired(fetchUser);`

Thanks!

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [October 23, 2025, 3:03pm UTC](https://community.auth0.com/t/how-to-properly-use-protected-api-routes-in-next-js/193489/3 "2025-10-23T15:03:29Z")

</div>

Hi @ronaldruzicka

The method `withApiAuthRequired` used to protect API routes should still be available in the v4 SDK.

As far as I can see in our [documentation](https://auth0.github.io/nextjs-auth0/classes/server.Auth0Client.html#withapiauthrequired) regarding the SDK, the method is still available under `Auth0Client` instance for the server.

The documentation available on the Github repository also shows [examples](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#protect-an-api-route) on protecting an API route using that method.

This issue might also occur when you are exporting a function whose return type could not be inferred by TypeScript. You can try adding a type annotation like **“data”** :

```auto
const foo = async () => {
// Add type annotation to `data`
  const data: DoStuffReturnType = someExternalLibary.doStuff(); 
  return data;
};

```

Also, does your API route use a custom API or one registered within the Auth0 Dashboard? If you do have it registered within Auth0, are you passing in the proper audience? I would recommend that you double check if you are importing all necessary dependencies when you are trying to use the method.

If you still encounter the same issue after following our documentation on the matter, please let me know!

Kind Regards,  
Nik

---

<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 6, 2025, 3:03pm UTC](https://community.auth0.com/t/how-to-properly-use-protected-api-routes-in-next-js/193489/4 "2025-11-06T15:03:35Z")

</div>

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