# How to access user id in App Router API routes with @auth0/nextjs-auth0?

**URL:** <https://community.auth0.com/t/how-to-access-user-id-in-app-router-api-routes-with-auth0-nextjs-auth0/190719>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [August 18, 2025, 4:20am UTC](https://community.auth0.com/t/how-to-access-user-id-in-app-router-api-routes-with-auth0-nextjs-auth0/190719 "2025-08-18T04:20:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rithvikrao2005](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rithvikrao2005/32/71217_2.png) [@rithvikrao2005](https://community.auth0.com/u/rithvikrao2005)\
**Post date:** [August 18, 2025, 4:20am UTC](https://community.auth0.com/t/how-to-access-user-id-in-app-router-api-routes-with-auth0-nextjs-auth0/190719/1 "2025-08-18T04:20:20Z")

</div>

Hi,

I’m using Next.js 15.4.6 with the App Router and @auth0/nextjs-auth0 ^4.9.0.

I need to access the logged-in user’s ID (sub) inside a backend API route so I can create and store an object tied to that user.

However, I can’t figure out the correct way to retrieve the session in an App Router API route.

getSession doesn’t exist on @auth0/nextjs-auth0.  
Importing from @auth0/nextjs-auth0/edge throws Cannot find module.

What’s the proper way to get the user/session inside an API route when using the App Router?

Thanks!

---

<div class="post-metadata">

**Author:** ![teodor.andrei](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/teodor.andrei/32/75320_2.png) [@teodor.andrei](https://community.auth0.com/u/teodor.andrei)\
**Post date:** [August 19, 2025, 10:44am UTC](https://community.auth0.com/t/how-to-access-user-id-in-app-router-api-routes-with-auth0-nextjs-auth0/190719/3 "2025-08-19T10:44:33Z")

</div>

Hi @rithvikrao2005, and welcome to the Augth0 Community!

The `getSession` function **does exist** and is the correct one to use. You might have been looking at documentation for a different context or had a typo in your import. The correct import is `import { getSession } from '@auth0/nextjs-auth0';`.

Here is an example:

```auto
import { getSession } from '@auth0/nextjs-auth0';
import { NextResponse } from 'next/server';

export async function POST(req: Request) {
  const session = await getSession();

  if (!session?.user) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  const userId = session.user.sub;

  return NextResponse.json({ success: true, userId: userId });
}

```

If that still doesn’t work, try with something like this:

```auto
import { auth0 } from "@/lib/auth0";
import { NextResponse, NextRequest } from "next/server";

export async function GET(req: NextRequest) {
  const session = await auth0.getSession();

if (!session?.user) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  const userId = session.user.sub;

  return NextResponse.json({ ... });
}

```

I hope this helps you!  
Teodor.

---

<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:** [September 2, 2025, 10:45am UTC](https://community.auth0.com/t/how-to-access-user-id-in-app-router-api-routes-with-auth0-nextjs-auth0/190719/4 "2025-09-02T10:45:24Z")

</div>

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