# How to Use the Fastify Adapter in NestJS: A Complete Guide

> Learn to use the Fastify adapter in NestJS to boost performance. Install @nestjs/platform-fastify and switch from Express with our complete guide.

- Repository: [nestjs/nest](https://github.com/nestjs/nest)
- Tags: how-to-guide
- Published: 2026-03-01

---

**To use the Fastify adapter in NestJS, install `@nestjs/platform-fastify` and pass `new FastifyAdapter()` to `NestFactory.create()` instead of the default Express adapter.**

The NestJS framework abstracts the underlying HTTP server through platform adapters, with Express being the default. Switching to Fastify requires only minimal bootstrap changes while unlocking significant performance benefits, as implemented in the `nestjs/nest` repository.

## Installing the Fastify Platform Package

Before modifying your bootstrap code, add the Fastify platform package to your dependencies:

```bash
npm install @nestjs/platform-fastify

```

This package contains the `FastifyAdapter` class located at [`packages/platform-fastify/adapters/fastify-adapter.ts`](https://github.com/nestjs/nest/blob/main/packages/platform-fastify/adapters/fastify-adapter.ts), which implements the Nest `AbstractHttpAdapter` contract.

## Bootstrapping with FastifyAdapter

Replace the default Express bootstrap with Fastify by importing `FastifyAdapter` and `NestFastifyApplication` from the platform package:

```typescript
import { NestFactory } from '@nestjs/core';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );
  await app.listen(3000);
}
bootstrap();

```

The generic type `NestFastifyApplication` (defined in [`packages/platform-fastify/interfaces/nest-fastify-application.interface.ts`](https://github.com/nestjs/nest/blob/main/packages/platform-fastify/interfaces/nest-fastify-application.interface.ts)) provides TypeScript support for Fastify-specific methods.

## FastifyAdapter Architecture and Implementation Details

Understanding the internal structure helps when debugging or extending the adapter functionality.

### Adapter Class Structure

The `FastifyAdapter` class extends `AbstractHttpAdapter` and translates Nest's generic HTTP handling into Fastify's API. The constructor (lines 46-60 in [`packages/platform-fastify/adapters/fastify-adapter.ts`](https://github.com/nestjs/nest/blob/main/packages/platform-fastify/adapters/fastify-adapter.ts)) creates a new Fastify instance or accepts an existing one, then configures the router with version-constraint handling.

### Middleware Support with Middie

Fastify requires the `middie` plugin for Express-style middleware compatibility. When you call `use()` before the adapter initializes, the adapter queues middleware internally (lines 333-341). During `app.init()`, Nest automatically registers `middie` and flushes the queued middlewares.

### Type Identification

The `getType()` method returns the string `'fastify'` (lines 292-294), allowing Nest to identify the active platform and load the correct internal modules.

## Configuring Fastify-Specific Features

The adapter exposes helper methods that wrap native Fastify plugins for common web server features.

### Server Options and Logging

Pass standard `FastifyServerOptions` to the constructor for core behavior configuration:

```typescript
const fastifyAdapter = new FastifyAdapter({
  logger: true,
  ignoreTrailingSlash: true,
});

const app = await NestFactory.create<NestFastifyApplication>(
  AppModule,
  fastifyAdapter,
);

```

### Static Assets and View Engines

Serve static files using the `useStaticAssets` method (implemented at lines 558-580), which internally delegates to `@fastify/static`:

```typescript
import { join } from 'path';

fastifyAdapter.useStaticAssets({
  root: join(__dirname, '..', 'public'),
});

```

Configure template engines via `setViewEngine` (lines 674-679), which loads `@fastify/view`:

```typescript
fastifyAdapter.setViewEngine({
  engine: {
    handlebars: require('handlebars'),
  },
  templates: 'views',
});

```

### CORS Configuration

Enable cross-origin resource sharing through the adapter's `enableCors` method, which utilizes `@fastify/cors`:

```typescript
fastifyAdapter.enableCors({ origin: true });

```

## Using Express-Style Middleware

Despite running on Fastify, you can register middleware using the familiar Express pattern. The adapter maintains compatibility through its internal queuing system:

```typescript
fastifyAdapter.use((req, res, next) => {
  console.log('Request received:', req.raw.url);
  next();
});

```

According to the source at lines 67-81, the adapter registers `onRequest` and `onResponse` hooks to integrate Nest's global guards, pipes, and interceptors into Fastify's lifecycle.

## Summary

- **Install** the `@nestjs/platform-fastify` package to access the Fastify adapter.
- **Import** `FastifyAdapter` and `NestFastifyApplication` from the platform package for type-safe bootstrapping.
- **Instantiate** `new FastifyAdapter()` with optional `FastifyServerOptions` and pass it to `NestFactory.create()`.
- **Configure** Fastify-specific features using `enableCors`, `useStaticAssets`, and `setViewEngine` helper methods.
- **Reference** the implementation at [`packages/platform-fastify/adapters/fastify-adapter.ts`](https://github.com/nestjs/nest/blob/main/packages/platform-fastify/adapters/fastify-adapter.ts) for advanced customization or debugging.

## Frequently Asked Questions

### What is the difference between FastifyAdapter and the default Express adapter in NestJS?

The **FastifyAdapter** translates NestJS abstractions into Fastify's high-performance HTTP engine, while the default adapter uses Express. Both implement the same `AbstractHttpAdapter` contract, so your controllers, services, and decorators remain identical. Fastify offers superior throughput and lower overhead, though the Express adapter has broader middleware ecosystem compatibility by default.

### Can I use Express middleware with FastifyAdapter?

Yes. The `FastifyAdapter` queues Express-style middleware calls and automatically registers the `middie` plugin during initialization (source lines 333-341). This allows you to use `app.use()` or `fastifyAdapter.use()` with traditional `(req, res, next)` signature functions, though native Fastify plugins often provide better performance.

### How do I configure Fastify plugins like static file serving?

Use the adapter's convenience methods rather than manual plugin registration. Call `fastifyAdapter.useStaticAssets()` for static files (wrapping `@fastify/static`) and `fastifyAdapter.setViewEngine()` for templates (wrapping `@fastify/view`). These methods handle plugin initialization and TypeScript type safety automatically.

### Where is the FastifyAdapter implementation located in the NestJS repository?

The core implementation resides at [`packages/platform-fastify/adapters/fastify-adapter.ts`](https://github.com/nestjs/nest/blob/main/packages/platform-fastify/adapters/fastify-adapter.ts) in the `nestjs/nest` repository. Key supporting files include the interface definitions at [`packages/platform-fastify/interfaces/nest-fastify-application.interface.ts`](https://github.com/nestjs/nest/blob/main/packages/platform-fastify/interfaces/nest-fastify-application.interface.ts) and working examples in [`sample/10-fastify/src/main.ts`](https://github.com/nestjs/nest/blob/main/sample/10-fastify/src/main.ts) and [`sample/17-mvc-fastify/src/main.ts`](https://github.com/nestjs/nest/blob/main/sample/17-mvc-fastify/src/main.ts).