@fastify/multipart
Parses multipart/form-data requests for file uploads.
Syntax
app.register(multipart, { limits? }) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | object | No | Options like limits (fileSize, files) and attachFieldsToBody. |
Returns
Promise<void> — Resolves when the plugin is registered.
Examples
import Fastify from 'fastify';
import multipart from '@fastify/multipart';
import { pipeline } from 'node:stream/promises';
import { createWriteStream } from 'node:fs';
const app = Fastify();
await app.register(multipart, { limits: { fileSize: 5_000_000 } });
app.post('/upload', async (req, reply) => {
const file = await req.file();
await pipeline(file.file, createWriteStream(`/tmp/${file.filename}`));
return { uploaded: file.filename };
});
Notes
Stream files with req.file()/req.files() to avoid buffering large uploads
in memory. Always set `limits.fileSize` to guard against abuse. Use
attachFieldsToBody to access fields alongside a schema.