# \[NextJs\] How to get idToken in a function or in a React component?

**URL:** <https://community.auth0.com/t/nextjs-how-to-get-idtoken-in-a-function-or-in-a-react-component/123089>\
**Category:** Get Help\
**Tags:** authentication-sessions, sessions\
**Created:** [December 14, 2023, 1:38pm UTC](https://community.auth0.com/t/nextjs-how-to-get-idtoken-in-a-function-or-in-a-react-component/123089 "2023-12-14T13:38:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vtomic85](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vtomic85/32/53196_2.png) [@vtomic85](https://community.auth0.com/u/vtomic85)\
**Post date:** [December 14, 2023, 1:38pm UTC](https://community.auth0.com/t/nextjs-how-to-get-idtoken-in-a-function-or-in-a-react-component/123089/1 "2023-12-14T13:38:43Z")

</div>

I’m working on a NextJs app and I need to send the `idToken` in each backend call. Is there an easy way to implement that? Here’s our app structure (extremely simplified):  
We have a React component:

```auto
/* src/pages/components/MyComponent/MyComponent.tsx */

export const MyComponent: FC<MyComponentProps> = (...) => {
  const handleClick = async () => {
    await makeBackendCall(param1, param2);
  }

  ...
  <Button onClick={handleClick} />
  ...
}

```

The backend call is here:

```auto
/* src/fetchers/webapi.ts */

export const makeBackendCall = (param1: string, param2: string): Promise<Response> => {
  fetch('http://some.url', {
    method: 'GET',
    headers: {
      /* ***THIS IS WHERE I NEED TO SEND idToken*** */
    }
  });
}

export const anotherCallWhichNeedsTheToken = () => {...}
export const andAnotherOne = () => {...}
/*...*/

```

How can I achieve this?

---

<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:** [December 18, 2023, 11:47pm UTC](https://community.auth0.com/t/nextjs-how-to-get-idtoken-in-a-function-or-in-a-react-component/123089/3 "2023-12-18T23:47:07Z")

</div>

Hi @vtomic85,

Welcome to the Auth0 Community!

To obtain an ID token, make sure to initialize `InitAuth0` with the `scope=openid` query parameter.

Alternatively, if necessary, you can directly pass the scope as an `authorizationParams` object to the ‘handleLogin’ method.

References:

- [Auth0 OpenID Connect Scopes](https://auth0.com/docs/get-started/apis/scopes/openid-connect-scopes#standard-claims)
- [InitAuth0 Documentation](https://auth0.github.io/nextjs-auth0/types/edge.InitAuth0.html)
- [AuthorizationParams Interface](https://auth0.github.io/nextjs-auth0/interfaces/handlers_login.AuthorizationParams.html)

Please feel free to reach out if you have any questions.

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:** [January 1, 2024, 11:48pm UTC](https://community.auth0.com/t/nextjs-how-to-get-idtoken-in-a-function-or-in-a-react-component/123089/4 "2024-01-01T23:48:00Z")

</div>

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