Lesson 16 / 32
Type Providers with TypeBox
Define schemas once with TypeBox and get validation plus TypeScript types in your Fastify handlers.
By Rahul Agarwal · 15 years in web development · Updated 1 Oct 2026
One schema, two benefits
Writing JSON Schema and TypeScript types separately invites drift. A type provider such as @fastify/type-provider-typebox infers handler types from the schema, so validation and types always match.
Typed route
The schema both validates the request and types request.body.
import Fastify from 'fastify';
import { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
const app = Fastify().withTypeProvider<TypeBoxTypeProvider>();
app.post('/items', {
schema: { body: Type.Object({ name: Type.String(), price: Type.Number({ minimum: 0 }) }) },
}, async (req) => {
return { name: req.body.name.toUpperCase() }; // req.body is typed
});Keep response schemas too: they speed up serialization and stop accidental data leaks.