Uploaded images (profile avatars and CMS images) are stored in Vercel Blob or Cloudflare R2, chosen with NEXT_PUBLIC_IMAGE_STORAGE. A download keeps only the provider you chose.
How uploads work
| Route | Who | Limit | Stored under |
|---|---|---|---|
POST /api/settings/modifyAvatar | Any signed-in user (not guests) | 5 MB | profile-images/ |
POST /api/cms/upload | Admins | 10 MB | cms-images/ |
Both routes are rate-limited and size-limited by the route policy. apps/web/server/uploads/image-upload.ts then:
- checks the size,
- reads the image with
sharpand accepts only PNG, JPEG and WebP (the real format, not the file extension), - re-encodes it, which also strips metadata and applies the EXIF rotation,
- uploads it under a random key (
<prefix>/<timestamp>-<uuid>.<ext>) and returns the public URL.
Option 1: Vercel Blob (default)
- In the Vercel dashboard, open Storage → Create → Blob and connect the store to your project.
- Copy the token from the store's
.env.localtab:
NEXT_PUBLIC_IMAGE_STORAGE="vercel_blob"
BLOB_READ_WRITE_TOKEN="vercel_blob_rw_..."
Option 2: Cloudflare R2
- In the Cloudflare dashboard, open R2 and create a bucket.
- Make it readable: connect a custom domain to the bucket, or enable the
r2.devpublic URL. - Under Manage R2 API Tokens, create a token with Object Read & Write on the bucket. Copy the access key ID and secret.
NEXT_PUBLIC_IMAGE_STORAGE="cloudflare_r2"
R2_ACCOUNT_ID="..."
R2_ACCESS_KEY_ID="..."
R2_SECRET_ACCESS_KEY="..."
R2_BUCKET_NAME="acme-uploads"
NEXT_PUBLIC_R2_PUBLIC_URL="https://files.acme.com"
Without NEXT_PUBLIC_R2_PUBLIC_URL the returned URL points at the bucket's S3 endpoint, which is not public. Set it.
pnpm doctor writes and deletes a probe file to prove the credentials work, for either provider.
Allow your image host in Next.js
Logos, hero and feature images are rendered with next/image, which only loads remote hosts listed in images.remotePatterns in apps/web/next.config.mjs. Add your storage host before uploading CMS images:
images: {
remotePatterns: [
// ...existing entries
{ protocol: "https", hostname: "*.public.blob.vercel-storage.com" }, // Vercel Blob
{ protocol: "https", hostname: "files.acme.com" }, // R2 custom domain
],
},
Uploading from your own feature
Reuse the helpers in a session route:
import { guardRoute } from "@/server/routeGuard";
import { normalizeImageUpload, uploadNormalizedImage } from "@/server/uploads/image-upload";
export async function POST(req: Request) {
const guard = await guardRoute(req, "/api/reports/cover");
if (!guard.ok) return Response.json({ error: guard.error }, { status: guard.status });
const file = (await req.formData()).get("file") as File | null;
if (!file) return Response.json({ error: "No file provided" }, { status: 400 });
const image = await normalizeImageUpload({ file, maxSizeBytes: 5 * 1024 * 1024 });
const url = await uploadNormalizedImage({ ...image, keyPrefix: "report-covers" });
return Response.json({ url });
}
Add the route to apps/web/lib/route-policy.ts with input: "image-upload" and a maxBodyBytes limit. For other file types (PDFs, video), write a separate helper; the image helper rejects anything that is not an image.
