Skip to main content

POST /api/files/upload

Upload files to Vercel Blob storage. Currently supports image files (JPEG and PNG) up to 5MB.

Authentication

Requires authenticated session. Returns 401 if not authenticated.

Request

This endpoint expects a multipart/form-data request with a file field.
file
File
required
The file to upload. Must be a JPEG or PNG image under 5MB.

Validation rules

file size
constraint
Maximum 5MB (5,242,880 bytes)
file type
constraint
Must be image/jpeg or image/png

Response

Returns a blob metadata object on success.
url
string
Public URL of the uploaded file
pathname
string
Path of the file in blob storage
contentType
string
MIME type of the uploaded file
contentDisposition
string
Content disposition header value

Example

const formData = new FormData();
formData.append("file", file);

const response = await fetch("/api/files/upload", {
  method: "POST",
  body: formData,
});

const data = await response.json();
console.log(data.url); // https://blob-url.vercel-storage.com/filename.png

Error responses

400 Bad Request
error
  • No file uploaded
  • File size exceeds 5MB
  • File type is not JPEG or PNG
401 Unauthorized
error
User is not authenticated
500 Internal Server Error
error
  • Upload to Vercel Blob failed
  • Failed to process request

Implementation

The endpoint uses Vercel Blob’s put function with public access. Files are stored with their original filename.
app/(chat)/api/files/upload/route.ts
import { put } from "@vercel/blob";

const data = await put(`${filename}`, fileBuffer, {
  access: "public",
});
To support additional file types, update the FileSchema validation in the route handler.

Chat interface

Learn how files are used in chat messages

Data persistence

Learn about Vercel Blob storage configuration

Build docs developers (and LLMs) love