Published
What you are building
multer reads the multipart request into memory. The route passes the buffer to Fileboy and returns the ID. Nothing is written to your server's disk.
1. Install and set your keys
npm install express multer @cd2/fileboy-nodeimport { Fileboy } from "@cd2/fileboy-node"
export const fileboy = Fileboy({
publicKey: process.env.FILEBOY_PUBLIC_KEY!,
privateKey: process.env.FILEBOY_PRIVATE_KEY, // optional
})You need a public key. Create a free app and copy it from the Keys page. Keep keys in environment variables, and never put a private key in code that reaches the browser. An upload needs only the public key; add the private key only if your app uses signed requests.
2. Upload the file
import express from "express"
import multer from "multer"
import { fileboy } from "./fileboy"
const app = express()
const upload = multer({
storage: multer.memoryStorage(),
limits: { fileSize: 10 * 1024 * 1024 }, // 10 MB; the buffer is held in memory
})
app.post("/avatar", upload.single("avatar"), async (req, res) => {
if (!req.file) {
return res.status(400).json({ error: "Choose a file" })
}
try {
const uploaded = await fileboy.uploadFile(req.file.buffer, {
name: req.file.originalname,
mime: req.file.mimetype,
})
// save uploaded.id against the user here
res.json({ id: uploaded.id })
} catch (e) {
res.status(502).json({ error: e instanceof Error ? e.message : "The upload failed" })
}
})
app.listen(3000)curl -F "avatar=@./photo.jpg" http://localhost:3000/avatarStore the id the upload returns and nothing else: it is the only thing you need to show the file again, at any size.
3. Show it at the size you need
import { fileboy } from "./fileboy"
export function avatarUrl(id: string, size = 96) {
return fileboy.buildImageUrl(id, {
public: true, // no 10-second private-key token in a URL that goes into a page
transforms: [
{ type: "resize", width: size * 2, height: size * 2, fit: "cover" },
{ type: "format", format: "webp" },
],
})
}For files too big to hold in memory, pass a stream instead: fileboy.uploadFile(fs.createReadStream(path), { name }), or use resumable uploads. Reading a file or image needs no key, so these URLs work in <img> tags, emails and anywhere else, unless the app requires signed URLs.
Checked against the current libraries in November 2026. Found something that no longer works? Tell us.