Guide

Upload files in Express with multer

Read the upload into memory, send it to Fileboy, and never write it to your server's disk.

Uploads guide · in this guide

  • What you are building
  • 1. Install and set your keys
  • 2. Upload the file
  • 3. Show it at the size you need

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-node
fileboy.ts
import { 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

server.ts
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/avatar

Store 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

avatar.ts
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.

Still choosing a tool?

Fileboy is small on purpose. This comparison says where it fits and where another option is the better call.

Get a key and run this code

Create a free app, copy your keys into the example and run it. The free plan needs no card.