next-api-layerNext API Layer
DocumentationAPI ReferenceExamples
next-api-layerNext API Layer

Production-grade API layer for Next.js with external JWT backends.

Documentation

  • Introduction
  • Installation
  • Quick Start
  • API Reference

Resources

  • Examples
  • Proxy
  • API Client
  • AuthProvider

Community

  • GitHub
  • Issues
  • Discussions
  • Contact

© 2026 Next API Layer. All rights reserved.

Created by
Documentation

Getting Started

  • Introduction
  • Installation
  • Quick Start

Core Concepts

  • How It Works
  • Token Management
  • Guest Tokens

Configuration

  • Auth Proxy
  • Proxy Handler
  • API Client
  • Security
  • i18n Integration

Client Side

  • AuthProvider
  • useAuth Hook

API Reference

  • API Reference
  • Types

Examples

  • Examples
  • Authentication Patterns
  • Role-Based Access
  • API Routes
  • Forms
  • Data Fetching
Changelog

Getting Started

  • Introduction
  • Installation
  • Quick Start

Core Concepts

  • How It Works
  • Token Management
  • Guest Tokens

Configuration

  • Auth Proxy
  • Proxy Handler
  • API Client
  • Security
  • i18n Integration

Client Side

  • AuthProvider
  • useAuth Hook

API Reference

  • API Reference
  • Types

Examples

  • Examples
  • Authentication Patterns
  • Role-Based Access
  • API Routes
  • Forms
  • Data Fetching
Changelog

API Reference

Complete API reference for all next-api-layer exports.

Server-Side#

createAuthProxy#

Creates a Next.js proxy for handling authentication.

TypeScript
Loading...

Parameters:

  • config - AuthProxyConfig object

Returns:

  • Next.js proxy function

Example:

TypeScript
Loading...

createApiClient#

Creates a server-side API client for making authenticated requests.

TypeScript
Loading...

Parameters:

  • config - ApiClientConfig object

Returns:

  • ApiClient instance with request methods

Methods:

MethodSignatureDescription
getget(endpoint, options?)GET request
postpost(endpoint, body?, options?)POST request
putput(endpoint, body?, options?)PUT request
patchpatch(endpoint, body?, options?)PATCH request
deletedelete(endpoint, options?)DELETE request

Example:

TypeScript
Loading...

getServerUser#

Fetches the current user on the server side. First checks x-auth-user header (set by proxy) for zero-latency access, falls back to backend validation only if needed.

TypeScript
Loading...

Parameters:

OptionTypeDefaultDescription
userCookiestring"userAuthToken"Name of the user token cookie
guestCookiestring"guestAuthToken"Name of the guest token cookie
apiBaseUrlstringprocess.env.API_BASE_URLBase URL for API requests
validateEndpointstring"auth/me"Endpoint to validate token
skipHeaderbooleanfalseSkip header check and always fetch from backend
isGuestFn(user) => booleanchecks token_typeFunction to check if user is a guest
parseResponse(response) => TUserextracts from data/userFunction to parse API response

Returns: Promise<ServerUserResult<TUser>>

TypeScript
Loading...

Example:

TypeScript
Loading...

isAuthenticatedServer#

Lightweight check if user is authenticated. Only checks for cookie presence, doesn't validate with backend.

TypeScript
Loading...

Parameters:

  • userCookie - Cookie name to check (default: "userAuthToken")

Returns: Promise<boolean>

Example:

TypeScript
Loading...

getServerToken#

Get the auth token directly in Server Components.

TypeScript
Loading...

Parameters:

  • userCookie - User cookie name (default: "userAuthToken")
  • guestCookie - Guest cookie name (default: "guestAuthToken")

Returns: Promise<string | null>

Example:

TypeScript
Loading...

Client-Side#

AuthProvider#

React context provider for authentication state.

TSX
Loading...

Props:

PropTypeDefaultDescription
userEndpointstring/api/auth/meEndpoint to fetch user data
loginEndpointstring/api/auth/loginEndpoint for login requests
registerEndpointstring/api/auth/registerEndpoint for registration
logoutEndpointstring/api/auth/logoutEndpoint for logout
logoutRedirectstring-Redirect path after logout
swrConfigSWRConfiguration{}SWR options
isGuestFn(user) => booleanchecks token_typeFunction to check if user is guest
parseResponse(res) => TUserextracts from data/userFunction to parse API response
onLogin(user) => void-Callback on successful login
onLogout() => void-Callback on logout
onError(error) => void-Error callback
initialUserTUser-Initial user from SSR

useAuth#

Hook to access authentication state and actions.

TypeScript
Loading...

Returns:

PropertyTypeDescription
userTUser | nullCurrent user
isAuthenticatedbooleanIs authenticated (not guest)
isGuestbooleanIs guest session
isLoadingbooleanLoading state
errorError | nullAuth error
login(credentials) => Promise<AuthResult>Login with credentials
register(data) => Promise<AuthResult>Register new user
logout() => Promise<void>Log out
refresh() => Promise<void>Refresh user data
mutate() => Promise<void>Revalidate user (SWR mutate)

Configuration Types#

AuthProxyConfig#

TypeScript
Loading...

ApiClientConfig#

TypeScript
Loading...

CsrfConfig#

TypeScript
Loading...

RateLimitConfig#

TypeScript
Loading...

AuditConfig#

TypeScript
Loading...

Response Types#

ApiResponse#

Standard API response format.

TypeScript
Loading...

AuthResult#

Result of authentication validation.

TypeScript
Loading...

TokenInfo#

Token validation result.

TypeScript
Loading...

Constants#

Default Values#

TypeScript
Loading...

Error Handling#

AuthError#

TypeScript
Loading...