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 Client Configuration

The createApiClient function creates a server-side API client that works seamlessly with the auth proxy. It handles token management, request sanitization, and proper error formatting.

Basic Setup#

TypeScript
Loading...

Using the API Client#

In API Routes#

TypeScript
Loading...

Available Methods#

TypeScript
Loading...

Configuration Options#

apiBaseUrl#

Required - Backend API base URL.

TypeScript
Loading...

cookies#

Required - Must match the cookie names in your auth proxy.

TypeScript
Loading...

sanitization#

XSS protection for response data.

TypeScript
Loading...

Default Sanitization

By default, all string values in responses are sanitized:

TypeScript
Loading...

i18n#

Automatically append locale to API requests. Works with the x-locale header set by the auth proxy.

TypeScript
Loading...

When enabled, the API client:

  1. Reads the x-locale header (set by auth proxy middleware)
  2. Validates it against the locales array
  3. Appends ?lang={locale} to all requests
TypeScript
Loading...

See the i18n documentation for full setup guide.

methodSpoofing#

For backends (like Laravel) that don't support PUT/PATCH/DELETE methods natively.

TypeScript
Loading...

When enabled:

TypeScript
Loading...

errorMessages#

Customize error messages.

TypeScript
Loading...

timeout#

Request timeout in milliseconds.

TypeScript
Loading...

defaultHeaders#

Headers to include in every request.

TypeScript
Loading...

responseFormat#

Expected response format.

TypeScript
Loading...

Advanced Usage#

Custom Headers per Request#

TypeScript
Loading...

Query Parameters#

TypeScript
Loading...

File Uploads#

TypeScript
Loading...

Raw Response#

TypeScript
Loading...

With Refreshed Token#

The API client automatically uses refreshed tokens from the proxy:

TypeScript
Loading...

Full Example#

TypeScript
Loading...

Response Format#

All API client methods return a Response object:

TypeScript
Loading...

Error Handling#

TypeScript
Loading...

Token Priority#

The API client uses tokens in this order:

  1. x-refreshed-token header - If proxy just refreshed the token
  2. User token cookie - For authenticated requests
  3. Guest token cookie - For guest requests

This ensures seamless token refresh without race conditions.