File uploads
An endpoint declared with body: 'multipart' receives multipart/form-data, and answers any other body with 415. The handler is an ordinary @Endpoint handler: text fields arrive as strings and files as File objects in data, already validated by the declaration's requestSchema.
The declaration, and how a client fills it, belong to the Builder guide File uploads. This page covers the server side.
Receiving a file
data.file is a web File: name, type, size, and arrayBuffer(), text() or stream() for its content. Its name is the last part of the name that the client sent, on every adapter: ../../etc/passwd arrives as passwd, and .. as an empty name. What you do with it is up to a service:
file.name and file.type are chosen by the client. The name is never a path, but two clients may send the same one: do not store a file under it. Do not treat the type as proof of the content.
Enabling multipart
The option and its limits are described on the Fastify and Bun adapter pages.
How parts become data
The adapter collects the parts into one object, and requestSchema validates that object.
- A file part becomes a
File. Every other part is a string. - Several parts with the same name become an array.
- When the schema can describe itself as JSON Schema (Zod 4 and ArkType can), text takes the form the schema describes, by the rules for all fields that are sent as text: a field declared as an array is an array even when one part was sent or none, and a field declared as a number, a boolean or an object is one.
- With a schema library that cannot, such as Valibot, a single part stays a single value, and a text value that starts with
{or[is parsed as JSON when possible. Describe the text in such a schema.
So this declaration accepts one or many files under files, and a client that sends a single tags part still gives the handler an array:
Limits
Both adapters read every file into memory before the handler runs, so an upload endpoint needs two limits.
In the schema, for what a valid upload is. A file that fails it is a 400 validation response with source: 'body', like any other invalid body:
The schema runs after the body has been received, so it does not protect the server's memory.
In the adapter, for how much the server is willing to read at all: bodyLimit and multipart.limits on Fastify and serve.maxRequestBodySize on Bun. Set them a little above the largest upload any schema allows. A request over the limit is answered with a 413 problem response. See the adapter pages linked above.
Guards run before the body is read. An upload from a caller who is not allowed to upload is rejected without reading the file.
For files too large to hold in memory, do not send them through the application: have the handler issue a pre-signed URL for your object storage and let the client upload there.
Testing an upload
The test client encodes data as a client of the builder does. It is parsed again on every adapter, the in-memory one included:
app.inject() takes a FormData as its body. Both are described in Testing.