# How to access my custom protected API, created in Next JS

**URL:** <https://community.auth0.com/t/how-to-access-my-custom-protected-api-created-in-next-js/132116>\
**Category:** Get Help\
**Tags:** client-grants, management-api\
**Created:** [April 16, 2024, 3:40pm UTC](https://community.auth0.com/t/how-to-access-my-custom-protected-api-created-in-next-js/132116 "2024-04-16T15:40:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rezgui.aziz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rezgui.aziz/32/51642_2.png) [@rezgui.aziz](https://community.auth0.com/u/rezgui.aziz)\
**Post date:** [April 16, 2024, 3:40pm UTC](https://community.auth0.com/t/how-to-access-my-custom-protected-api-created-in-next-js/132116/1 "2024-04-16T15:40:00Z")

</div>

I created this api endpoint in next js (let’s call it userData) :

```auto

export const GET = withApiAuthRequired(async function GET() {
  await dbConnect();
  try {
    const session = await getSession();
    const user = session?.user;
    const userData = await user_data.find({
      email: user?.email,
    }); /* find all the data in our database */
    return new Response(JSON.stringify({ data: userData }), {
      status: 200,
    });
  } catch (error) {
    return new Response(JSON.stringify({ message: "An error occurred" }), {
      status: 500,
    });
  }
});

```

I have another endpoint called token, also protected with withApiAuthRequired().  
when calling the the userData api from the token api I get

```auto
 {
  error: 'not_authenticated',
  description: 'The user does not have an active session or is not authenticated'
}

```

please how can I fix it?  
I want to be able to access it.

---

<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:** [April 16, 2024, 8:07pm UTC](https://community.auth0.com/t/how-to-access-my-custom-protected-api-created-in-next-js/132116/3 "2024-04-16T20:07:23Z")

</div>

Hi @rezgui.aziz,

Thanks for reaching out to the Auth0 Community!

When calling the `withApiAuthRequired` function, it requires the user to have a valid session which requires them to login.

Could you please clarify if you have logged in before trying to access your protected endpoint?

See [handleAuth | @auth0/nextjs-auth0](https://auth0.github.io/nextjs-auth0/functions/index.handleAuth-1.html).

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![rezgui.aziz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rezgui.aziz/32/51642_2.png) [@rezgui.aziz](https://community.auth0.com/u/rezgui.aziz)\
**Post date:** [April 16, 2024, 8:18pm UTC](https://community.auth0.com/t/how-to-access-my-custom-protected-api-created-in-next-js/132116/4 "2024-04-16T20:18:55Z")

</div>

Hey @rueben.tiow, yes I am sure I am logged in, here is my login handler, maybe the config might caused the issue?

```auto
  login: async (req: NextApiRequest, res: NextApiResponse) => {
    try {
      const loginResponse = await handleLogin(req, res, {
        authorizationParams: {
          audience: "https://xxx.auth0.com/mfa/",
          scope: process.env.AUTH0_SCOPE,
          client_id: process.env.AUTH0_CLIENT_ID,
          client_secret: process.env.AUTH0_CLIENT_SECRET,
        },
      });

      return loginResponse;
    } catch (e) {
      console.log(e);
    }
  },

```

---

<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:** [April 18, 2024, 7:18pm UTC](https://community.auth0.com/t/how-to-access-my-custom-protected-api-created-in-next-js/132116/5 "2024-04-18T19:18:52Z")

</div>

Hi @rezgui.aziz,

Thank you for your reply.

It seems that you would need to call `getAccessToken()` to correctly get an access token to access an external API from the server. Please check out the example below on accessing an external API from an API route.

> <https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route>

Let me know if you have any questions.

Cheers,  
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:** [May 2, 2024, 7:19pm UTC](https://community.auth0.com/t/how-to-access-my-custom-protected-api-created-in-next-js/132116/6 "2024-05-02T19:19:17Z")

</div>

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