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 🙏

© 2025 – Pkg Stats / Ryan Hefner

payload-express-middleware

v0.6.0

Published

Enabling Express.js backend to interact with Payload CMS APIs seemlessly

Readme

Payload Express Middleware

payload-express-middleware is a library that enables API routing in Express.js, similar to Next.js API routes, using Payload CMS. This middleware simplifies the process of integrating Payload's Local API with an Express.js application.

Features

  • Seamlessly map API routes to Payload CMS collections.
  • Built-in authentication and authorization middleware.
  • CRUD operations for Payload collections.
  • Error handling for common Payload validation and authentication errors.

Installation

Install the library and its peer dependencies:

NPM

npm install payload-express-middleware payload express

Yarn

yarn add payload-express-middleware payload express

Usage

1. Initialize Payload CMS

Ensure you have a Payload CMS configuration file (e.g., payload.config.js) and initialize Payload in your Express app.

2. Use the Middleware

Import and use the payloadAPIRouterMiddleware function in your Express app:

import express from "express";
import payload from "payload";
import { payloadAPIRouterMiddleware } from "payload-express-middleware";
import path from "path";

const app = express();

async function start() {
  // Load Payload configuration
  const config = path.resolve(__dirname, "/route/to/payload.config.js");

  // Initialize Payload
  await payload.init({
    secret: "PAYLOAD_SECRET", // Replace with your Payload secret
    config,
  });

  // Use the middleware
  app.use(payloadAPIRouterMiddleware(payload, options)); // options are Optional

  // Use your other middleware, routes, etc
  // ....

  // Start the server
  app.listen(4000, () => {
    console.log("Express API running on port 4000");
  });
}

start();

3. API Routes

The middleware automatically maps the following routes to Payload's Local API:

Authentication Routes

  • Login: POST /api/users-collection}/login
  • Logout: POST /api/{users-collection}/logout
  • Me: GET /api/{users-collection}/me

Note: {user-collection} defined by your payload.config.js

CRUD Routes

  • Find Many: GET /api/:collection
  • Count: POST /api/:collection/count
  • Find By ID: GET /api/:collection/:id
  • Create: POST /api/:collection
  • Update: PATCH /api/:collection/:id
  • Delete: DELETE /api/:collection/:id

4. Example Payload Configuration

Ensure your Payload CMS configuration file (payload.config.js) is properly set up. For example:

module.exports = {
  secret: "MY_SECRET",
  collections: [
    {
      slug: "users",
      admin: true, // --> enables 'user-collection' for Auth operations
      fields: [
        { name: "email", type: "email", required: true },
        { name: "password", type: "password", required: true },
      ],
    },
    {
      slug: "posts",
      fields: [
        { name: "title", type: "text", required: true },
        { name: "content", type: "richText" },
      ],
    },
  ],
};

API Documentation

payloadAPIRouterMiddleware(payload: Payload, options?: PayloadAPIRouterMiddlewareOptions)

This function creates an Express router that maps API routes to Payload's Local API.

Parameters

  • payload: The initialized Payload CMS instance.
  • options: Additional options to the middleware:
    • simpleResponses?: boolean - if set to true return response.docs as the whole response

Error Handling

The middleware includes built-in error handling for:

  • Validation errors (400 Bad Request).
  • Authentication errors (401 Unauthorized).
  • Not found errors (404 Not Found).
  • Generic server errors (500 Internal Server Error).

License

This project is licensed under the MIT License.