Skip to main content
This example demonstrates how to integrate OpenAPI TypeScript with Next.js, supporting both server and client components with type-safe API calls.

Configuration

OpenAPI TypeScript Config

Create openapi-ts.config.ts in your project root:
openapi-ts.config.ts
Key features:
  • @hey-api/client-next - Next.js-specific client with App Router support
  • runtimeConfigPath - Path to client configuration file

Client Configuration

Create a configuration file for the client:
src/hey-api.ts

Package Dependencies

package.json

Usage

Client Component

Use 'use client' directive for interactive components:
app/page.tsx

Server Component

Server components can directly await API calls:
app/pet/[id]/page.tsx

Layout with Interceptors

Configure global interceptors in the root layout:
app/layout.tsx

Next.js Features

Caching and Revalidation

Use Next.js caching options with API calls:

Dynamic Routes

Type-safe dynamic route parameters:

Error Handling

Handle errors gracefully:

Metadata Generation

Generate metadata from API responses:

Key Features

Type Safety

Full TypeScript support in both server and client components:

Server Components

Direct async/await in server components:

Request Interceptors

Add authentication and logging:

Running the Example

1

Clone the repository

2

Install dependencies

3

Run the Next.js example

4

Open in browser

Navigate to http://localhost:3000

Full Example

View the complete example in the repository: Next.js Example on GitHub

Learn More

Next.js Client

SDK Plugin

TypeScript Plugin

Examples Overview