October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Your Next.js API Route Is Public—Even If Its UI Isn’t

Next.js Route Handlers are reachable HTTP endpoints even when the page or button that calls them is hidden. Protect sensitive reads and mutations with server-side authentication and authorization.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Yes. A Next.js Route Handler is a public HTTP endpoint: hiding the page, button, or link that calls it does not prevent someone from sending a request directly. Protect private data and actions with server-side authentication and authorization in the handler or in the protected data-access layer it uses.

Why hiding the UI does not protect a route

A page controls what your application displays; a Route Handler exposes an HTTP endpoint. Next.js states that “Route Handlers are public HTTP endpoints. Any client can access them.” A caller does not need to find a link in your navigation or use your intended interface if they can reach the endpoint over the network. See the Next.js Backend for Frontend guide.

As an Amazon Associate I earn from qualifying purchases.

That does not mean every route must require a login. It means you must decide deliberately which callers may use each route, and enforce that decision on the server. A hidden UI is not a security boundary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What a protected handler needs to check

Authenticate the requester

Authentication establishes who is making the request. For a protected operation, check the request’s credentials or session on the server; do not rely on the browser having already hidden the relevant control.

Authorize the specific action or resource

Authorization determines whether that authenticated user may perform this action or access this particular resource. A valid session alone does not establish that the user owns a requested record or has the required role. Next.js’s Authentication guide illustrates checking for a session and then checking the user’s role, with different responses for missing authentication and insufficient permission.

Make the check at the boundary that protects the sensitive operation: in the Route Handler or in the server-side data-access function it calls. Next.js advises treating handlers like public-facing APIs and verifying that the user is allowed to access them. A proxy-only gate or a UI check should not be the sole protection.

Rank #2
Sale
Guide to Firewalls and VPNs
  • Used Book in Good Condition

How to audit Route Handlers

  1. Find the endpoints. Review every route.ts or route.js file under app. These files define HTTP handlers, not page visibility rules. The Route Handlers reference documents the convention.
  2. Mark sensitive operations. Identify handlers that read private data, expose records, or perform mutations. For each, identify which user, role, or ownership condition is required.
  3. Enforce both checks server-side. Authenticate the request, then authorize the requested action and resource. Do not infer permission merely from a session existing.
  4. Return the appropriate response. A missing or invalid authentication state is different from an authenticated user who lacks permission. The framework’s example uses 401 for unauthenticated requests and 403 for forbidden ones.
  5. Review what the endpoint accepts and returns. Validate incoming data and return only what the caller needs; details follow below.

Secure inputs, errors, and responses

Requests to an endpoint are untrusted, even when your own UI normally sends them. The Backend for Frontend guide advises checking request content type and size, sanitizing data against XSS before use, and using timeouts to protect resources. Apply validation that fits the operation, and do not treat client-supplied fields as proof of identity, ownership, or permission.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Limit payloads: reject unexpected content types and oversized request bodies.
  • Validate values: check that fields have the expected shape and meaning before using them.
  • Bound work: use timeouts where appropriate so a request cannot consume resources indefinitely.
  • Minimize disclosure: keep secrets and internal error details out of client-facing error responses.
  • Minimize returned data: expose only the fields needed for the task. The Authentication guide recommends a data access layer and DTOs as patterns for centralizing authorization and limiting returned data.

CORS is not authentication

CORS configures whether browsers permit scripts from other origins to read responses. It does not establish who a caller is or whether that person is permitted to use an endpoint. A Route Handler can send CORS headers and still need authentication and authorization. Do not use a restrictive browser-origin policy as the access-control check for private data or mutations.

Know which methods your route exposes

Next.js Route Handlers support GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS. If you do not define OPTIONS, Next.js generates it and sets the Allow header based on the other methods defined. Check the methods your handler supports as part of the audit; securing one UI action does not secure every HTTP operation on the endpoint.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the right place for authorization

Next.js describes optimistic cookie or session checks as useful for quick operations, while secure, database-backed checks are more appropriate for sensitive data or actions. A data access layer can centralize those secure checks so they are not inconsistently implemented across handlers; DTOs can keep the returned data to the fields a caller needs. Choose the check based on the impact of unauthorized access, not on whether the UI appears private.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.