Uploads and image delivery in Next.js with Dreep
Keep DREEP_API_KEY on the server and presign uploads in a route handler. The browser PUTs the file straight to Dreep, a server action confirms it, and next/image renders through a custom loader that builds Dreep URLs.
Set it up in six steps
Written for Next.js 15 with the App Router.
- 1
Install and add the key
Run npm install dreep server-only and put DREEP_API_KEY in .env.local. Never prefix it with NEXT_PUBLIC_.
- 2
Create a server-only client
Construct one Dreep instance in lib/dreep.ts and import server-only there, so a stray client import fails the build.
- 3
Presign in a route handler
Check the signed-in user and your own type and size rules, then call dreep.presignUpload and record who owns the upload id.
- 4
PUT from the client with progress
Use XMLHttpRequest for progress events. The Content-Type header must equal the type you presigned.
- 5
Confirm in a server action
Check the user presigned this id, call dreep.confirmUpload({ id }) and save the asset in your database.
- 6
Render with a next/image loader
A custom loader adds width, quality and format to the Dreep URL. Dreep has no automatic format, so pick one.
The two files that matter
getCurrentUser stands in for your own auth. Everything prefixed dreep is the real SDK API.
import { NextResponse } from "next/server";
import { dreep } from "@/lib/dreep";
import { getCurrentUser } from "@/lib/auth"; // your auth
export const POST = async (request: Request) => {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: "Unauthorised" }, { status: 401 });
}
const { filename, contentType, sizeBytes } = await request.json();
const upload = await dreep.presignUpload({
filename,
contentType,
sizeBytes,
folder: `users/${user.id}`,
});
return NextResponse.json({ id: upload.id, uploadUrl: upload.uploadUrl });
};"use client";
import type { ImageLoaderProps } from "next/image";
const dreepLoader = ({ src, width, quality }: ImageLoaderProps) => {
const url = new URL(src);
url.searchParams.set("width", String(width));
url.searchParams.set("quality", String(quality ?? 80));
url.searchParams.set("format", "webp");
return url.toString();
};
export default dreepLoader;
// next.config.ts
// images: { loader: "custom", loaderFile: "./lib/dreepLoader.ts" }Why it is shaped this way
The key never reaches the bundle
The SDK throws in a browser and server-only blocks client imports at build time.
Progress without a proxy
Bytes skip your Next.js server, so serverless body limits and timeouts don't apply to the file.
One original, every size
Each srcset width becomes its own Dreep variant, generated once and then cached.
Frequently asked questions
Can I call the Dreep SDK from a client component?
No. The SDK throws if it detects a browser. Call it from route handlers, server actions or server components, and give the browser only the presigned uploadUrl.
Why not upload through a server action with FormData?
Server actions have request body limits and timeouts. Presigning keeps your function to a small JSON request while the file, up to 500 MB, goes straight to Dreep.
Does the custom loader replace the Next.js image optimiser?
Yes. With loader set to custom, Next.js asks the loader for a URL per width and Dreep generates and caches each size.
What happens if the user never confirms the upload?
The asset stays pending and is excluded from media listings. The upload URL is single use and expires after 10 minutes.
Ship uploads in your Next.js app this afternoon
Create a free project, copy the API key into .env.local, and add the route handler, action and loader.