A route handler is an API route in Next.js: a route.ts file that answers with data. To protect one with Better Auth, read the session from the request and answer with status 401 when there isn’t one.
The short answer: in GET(request), call auth.api.getSession({ headers: request.headers }), and return Response.json(…, { status: 401 }) when it’s null.
Syntax
export async function GET(request: Request) {
const session = await auth.api.getSession({
headers: request.headers,
});
if (!session) {
return Response.json(<error>, { status: 401 });
}
return Response.json(<data>);
}
GETanswersGETrequests. UsePOST,PUTand so on for other methods.request.headersare the headers of the request, with the session cookie.Response.json(<error>, { status: 401 })sends an error object with the status 401, Unauthorized.Response.json(<data>)sends the data to signed-in callers.
See Next.js’s Route Handlers (opens in a new tab) reference and Better Auth’s Next.js integration (opens in a new tab) page.
Step 1: Create the Route Handler
The example is a small Next.js app with Better Auth and email and password sign-in, and one person, Alex Rivera (alex@example.com). It gets an API route at /api/me that returns the signed-in person’s name and email address. Create app/api/me/route.ts:
import { auth } from "@/lib/auth";
export async function GET(request: Request) {
const session = await auth.api.getSession({
headers: request.headers,
});
if (!session) {
return Response.json({ error: "Not signed in" }, { status: 401 });
}
return Response.json({
name: session.user.name,
email: session.user.email,
});
}
- A route handler receives the request, so its headers go straight to
getSession. - Without a session, the route answers with
{ error: "Not signed in" }and status 401. - With one, it answers with the user’s name and email address.
Step 2: Call It Signed In and Signed Out
Signed in as Alex, open http://localhost:3000/api/me:

In a browser where nobody is signed in, the same address answers with the error:

Terminal shows both requests, the second with status 401:
GET /api/me 200 in 50ms (next.js: 24ms, application-code: 26ms)
GET /api/me 401 in 11ms (next.js: 5ms, application-code: 6ms)
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Redirecting to the sign-in page | Programs that call the API get a web page instead of an error | Answer with status 401 |
| Returning the whole session | Tokens and other session details reach the caller | Return only the fields the caller needs |
Checking the session only in proxy.ts | A made-up cookie gets through | Check it in the route handler too |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| The route always answers 401 | The request has no session cookie, for example from another site or a script | Call it from the signed-in browser, or send the cookie with the request |
404 instead of JSON | The file isn’t at app/api/me/route.ts | Check the folder names |
FAQ
Why 401 and not 403?
401, Unauthorized, means the caller isn’t signed in. 403, Forbidden, is for a signed-in caller who isn’t allowed to do something.
Do I need headers() from next/headers here?
No. A route handler gets the request itself, so request.headers is enough.
Conclusion
Read the session from request.headers, answer with 401 when it’s missing, and return only what the caller needs. That’s a protected route handler.
Additional Resources
- Route Handlers (opens in a new tab) in the Next.js documentation
- Next.js integration (opens in a new tab) in the Better Auth documentation
Related
Better Auth, Next.js and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by Better Auth, Inc. or Vercel.
0 comments