npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@uploadbox/nextjs

v0.11.0

Published

Next.js route handler for Uploadbox — presigned URL uploads with your own S3 bucket

Readme

@uploadbox/nextjs

Next.js route handler for Uploadbox — presigned URL uploads with your own S3 bucket.

npm license

Installation

npm i @uploadbox/nextjs @uploadbox/core

Peer dependency: Next.js 14, 15, or 16.

Quick Start

1. Define your file router

// src/lib/uploadbox.ts
import { f } from "@uploadbox/core";

export const router = {
  imageUploader: f({ image: { maxFileSize: "4MB", maxFileCount: 5 } })
    .middleware(async ({ auth }) => {
      return { userId: auth?.userId ?? auth?.apiKeyId };
    })
    .onUploadComplete(async ({ file, metadata }) => {
      console.log("Upload complete:", file.url);
      return { fileId: file.key };
    }),
};

export type AppRouter = typeof router;

2. Create the route handler

// src/app/api/uploadbox/route.ts
import { createRouteHandler } from "@uploadbox/nextjs";
import { router } from "@/lib/uploadbox";

export const { GET, POST } = createRouteHandler({ router });

S3 credentials are read from environment variables by default (UPLOADBOX_S3_BUCKET, UPLOADBOX_AWS_REGION, UPLOADBOX_AWS_ACCESS_KEY_ID, UPLOADBOX_AWS_SECRET_ACCESS_KEY), or pass a config object explicitly.

3. Add the React components

See @uploadbox/react for the client-side integration.

Lifecycle Hooks

Lifecycle hooks let you integrate authentication, quotas, and database tracking without coupling your upload logic to a specific database.

export const { GET, POST } = createRouteHandler({
  router,
  hooks: {
    // API key auth (hosted mode):
    onAuthenticate: async (req) => {
      const token = req.headers.get("authorization");
      const user = await verifyToken(token);
      return { apiKeyId: user.id, apiKeyName: user.name };
    },
    // Session auth (self-hosted mode):
    // onAuthenticate: async (req) => {
    //   const session = await getSession(req);
    //   if (!session) return undefined;
    //   return { userId: session.id, userName: session.name };
    // },

    onQuotaCheck: async ({ auth, totalSize, fileCount }) => {
      const usage = await getStorageUsage(auth.apiKeyId ?? auth.userId);
      if (usage + totalSize > MAX_STORAGE) {
        throw UploadboxError.quotaExceeded();
      }
    },

    onUploadStarted: async (events) => {
      await db.insert(uploads).values(events.map(toRecord));
    },

    onUploadCompleted: async (event) => {
      await db.update(uploads)
        .set({ status: "completed" })
        .where(eq(uploads.key, event.file.key));
    },

    onUploadFailed: async (event) => {
      await db.update(uploads)
        .set({ status: "failed" })
        .where(eq(uploads.key, event.key));
    },

    onFileVerified: async (fileKey) => {
      // Fallback lookup when in-memory state is lost (e.g., multi-instance deploys).
      // Pending only: a finished upload that came back would complete a second time.
      return db.query.uploads.findFirst({
        where: and(eq(uploads.key, fileKey), eq(uploads.status, "pending")),
      });
    },
  },
});

Available Hooks

| Hook | When | Purpose | |------|------|---------| | onAuthenticate | Every request | Return AuthContext or undefined for anonymous. Use apiKeyId/apiKeyName for hosted/API-key auth; use userId/userName for self-hosted session auth | | onQuotaCheck | Before presigning | Throw UploadboxError.quotaExceeded() to deny | | onUploadStarted | After presigned URLs generated | Track pending uploads | | onFileVerified | On complete, if in-memory miss | Fallback lookup for pending uploads | | onUploadCompleted | After onUploadComplete succeeds | Update DB, trigger webhooks | | onUploadFailed | On upload failure | Log errors, clean up | | onMultipartStarted | Multipart upload created | Track large uploads | | onMultipartCompleted | Multipart upload finished | Update status | | onMultipartAborted | Multipart upload cancelled | Clean up | | onResolveConfig | Every request | Per-tenant S3 config (BYOB) and the caller's keyPrefix |

Event hooks finish before the response, so they also complete on serverless. If one throws, the route handler logs the error and the upload carries on. Pass waitUntil to run onUploadCompleted and processing after the response:

import { after } from "next/server";

export const { GET, POST } = createRouteHandler({ router, waitUntil: after });

Who Can Call Each Action

middleware runs for upload and create-multipart only. list, download, delete, presign-parts and uploads with a client-chosen key reach everything under the key prefix, so the caller must own all of it:

  • Self-hosted: return a caller-specific keyPrefix from onResolveConfig, such as `users/${auth.userId}/`. Without one, the route handler answers 403.
  • Hosted: send an Uploadbox API key in the Authorization header. The route handler checks it with the platform, whatever your onAuthenticate returns. A browser cannot call these actions.

allowUnscopedAccess: true lifts this rule for every authenticated caller.

Serverless Deployments

Between upload and complete, the route handler keeps a record of each upload in one process's memory. On Vercel, AWS Lambda, or any deployment with more than one instance, complete can reach another instance and answer 404 UPLOAD_NOT_FOUND. Pass a shared store. This one uses Upstash Redis:

import type { PendingUpload, PendingUploadStore } from "@uploadbox/nextjs";
import { Redis } from "@upstash/redis";

const redis = Redis.fromEnv();
const redisKey = (key: string) => `uploadbox:pending:${key}`;

const pendingStore: PendingUploadStore = {
  get: (key) => redis.get<PendingUpload>(redisKey(key)),
  set: async (key, value) => {
    await redis.set(redisKey(key), value, { ex: 24 * 60 * 60 });
  },
  delete: async (key) => {
    await redis.del(redisKey(key));
  },
  cleanupExpired: async () => {}, // the TTL above expires entries
};

export const { GET, POST } = createRouteHandler({ router, pendingStore });

Hosted mode needs no store, because the platform keeps the record.

Rate Limiting

Built-in sliding window rate limiter, no external dependencies:

export const { GET, POST } = createRouteHandler({
  router,
  rateLimit: {
    requestsPerMinute: 60,
    uploadsPerHour: 100,
  },
});

Limits apply per user, API key or client IP. The built-in limiter counts per instance: pass a shared rateLimiter for a limit across instances.

Processing Pipeline

Run post-upload processing hooks (image resize, virus scan, etc.):

import { createImageResizeHook } from "@uploadbox/core/hooks/image-resize";
import { createVirusScanHook } from "@uploadbox/core/hooks/virus-scan";

export const { GET, POST } = createRouteHandler({
  router,
  processing: {
    hooks: [
      createImageResizeHook({ maxWidth: 1920, format: "webp" }),
      createVirusScanHook({ clamavUrl: "http://localhost:3310" }),
    ],
    mode: "sequential", // or "parallel"
    continueOnError: true,
  },
});

In hosted mode a hook can only read the file through ctx.getFileUrl, and must set usesFileUrlOnly: true. Any other hook stops the route handler at startup, unless you bring your own bucket through hooks.onResolveConfig.

Or use runProcessingPipeline directly:

import { runProcessingPipeline } from "@uploadbox/nextjs";

const results = await runProcessingPipeline(pipelineConfig, file, metadata, s3Client, config);

Hosted Mode

For the Uploadbox hosted platform, pass an apiKey to createRouteHandler. The platform then handles auth, quotas, event tracking and every storage operation, so your server never holds the bucket credentials:

import { createRouteHandler } from "@uploadbox/nextjs";

export const { GET, POST } = createRouteHandler({
  router,
  apiKey: process.env.UPLOADBOX_API_KEY!,
});

Keep the key on the server. The browser needs no key, and anyone who holds one can read, replace and delete every file in the project.

The key identifies your project and says nothing about the visitor. A browser request has auth.anonymous set to true, so check your app's session in each route's middleware to decide who can upload.

Configuration

Full createRouteHandler options:

createRouteHandler({
  router,                    // FileRouter (required)
  config: {                  // S3 config (or use env vars)
    bucket: "my-bucket",
    region: "us-east-1",
    accessKeyId: "...",
    secretAccessKey: "...",
    endpoint: "...",         // MinIO, R2, Wasabi
    forcePathStyle: true,
    cdnBaseUrl: "https://cdn.example.com",
    presignedUrlExpiry: 3600,
  },
  hooks: { ... },            // LifecycleHooks
  rateLimit: { ... },        // RateLimitConfig
  processing: { ... },       // ProcessingPipelineConfig
  pendingStore: store,       // Shared store for serverless deployments
  waitUntil: after,          // Run post-upload work after the response
  allowUnscopedAccess: false,
  s3ClientFactory: (cfg) => createCustomClient(cfg),
});

Related Packages

License

MIT