# How can I get the user id after login and pass it to an api route?

**URL:** <https://community.auth0.com/t/how-can-i-get-the-user-id-after-login-and-pass-it-to-an-api-route/81556>\
**Category:** Get Help\
**Tags:** api, nextjs, auth0\
**Created:** [April 11, 2022, 3:57pm UTC](https://community.auth0.com/t/how-can-i-get-the-user-id-after-login-and-pass-it-to-an-api-route/81556 "2022-04-11T15:57:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thekoala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thekoala/32/35703_2.png) [@thekoala](https://community.auth0.com/u/thekoala)\
**Post date:** [April 11, 2022, 3:57pm UTC](https://community.auth0.com/t/how-can-i-get-the-user-id-after-login-and-pass-it-to-an-api-route/81556/1 "2022-04-11T15:57:14Z")

</div>

Essentially what I am trying to do is pass the auth0 user ID to an API route in my application which calls a stripe endpoint which then creates a customer portal session. I need to pass the user ID to the api call but I am unable to call the useUser in an API route so I am unsure of how to proceed here.

```auto
const stripe = require('stripe')('some-api-key');

export default function create_customer_portal_session(req, res) {
    
    const customerId = 'this should be the auth0 user id';
    const returnUrl = 'some-return-url';
    const portalSession = async () => await stripe.billingPortal.sessions.create({
        customer: customerId,
        return_url: returnUrl,
    });
    res.redirect(303, portalSession.url);
};

```

does anyone have an idea of how to do this?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [April 12, 2022, 12:45am UTC](https://community.auth0.com/t/how-can-i-get-the-user-id-after-login-and-pass-it-to-an-api-route/81556/3 "2022-04-12T00:45:40Z")

</div>

Hey there @thekoala!

> [@thekoala](#):
>
> I need to pass the user ID to the api call but I am unable to call the useUser in an API route so I am unsure of how to proceed here.

Considering this is post login, perhaps you could inspect the access token (or id token) of the user and use the `sub` claim (Auth0 user\_id) in the code above - For reference, here’s an [example access token](https://auth0.com/docs/secure/tokens/access-tokens#sample-access-token).

Hope this helps!

---

<div class="post-metadata">

**Author:** ![thekoala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/thekoala/32/35703_2.png) [@thekoala](https://community.auth0.com/u/thekoala)\
**Post date:** [April 14, 2022, 12:38pm UTC](https://community.auth0.com/t/how-can-i-get-the-user-id-after-login-and-pass-it-to-an-api-route/81556/4 "2022-04-14T12:38:37Z")

</div>

I ended up figuring it out. All I had to do was create a form in one of my react components and pass the auth0 user id to it and since it was on the front end it had access to the useUser hook

```auto
<form method="POST" action="/api/create_customer_portal_session">
         <input type={'hidden'} name={'customerID'} value={customerID} onChange={() => {}}/>
         <button type="submit">Account Settings</button>
</form>

```

I had no idea I could pass a hidden value to an input. But, it worked out and everything is linked correctly now!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [April 14, 2022, 3:16pm UTC](https://community.auth0.com/t/how-can-i-get-the-user-id-after-login-and-pass-it-to-an-api-route/81556/5 "2022-04-14T15:16:09Z")

</div>

@thekoala Good stuff! Awesome to know you got working how you wanted and really appreciate you sharing with the community 😄

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [April 29, 2022, 3:16pm UTC](https://community.auth0.com/t/how-can-i-get-the-user-id-after-login-and-pass-it-to-an-api-route/81556/6 "2022-04-29T15:16:17Z")

</div>

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