Zen Router | An opinionated HTTP router compatible with Cloudflare Workers, Node.js, Bun, Deno.

2 min read Original article ↗

An opinionated HTTP router with typed path params, built-in body validation, and a clean auth model.

Zen Router is an opinionated API router built by Liveblocks, where it has been powering billions of requests per month. It’s designed for Cloudflare Workers, Bun, Node.js, and other modern JavaScript runtimes.

npm install @liveblocks/zenrouter
import { ZenRouter } from "@liveblocks/zenrouter";
import { z } from "zod";

const zen = new ZenRouter({
  authorize: async ({ req }) => {
    const token = req.headers.get("Authorization");
    const currentUser = await db.getUserByToken(token);
    if (!currentUser) return false;
    return { currentUser };
  },
});

// Get a specific post
zen.route(
  "GET /api/posts/<postId>",

  async ({ p, auth }) => {
    const post = await db.getPost(auth.currentUser.id, p.postId);
    return { id: post.id, title: post.title };
  }
);

// Create a new post for the current user
zen.route(
  "POST /api/posts",

  z.object({ title: z.string() }),

  async ({ auth, body }) => {
    const post = await db.createPost({
      title: body.title,
      authorId: auth.currentUser.id,
    });
    return { id: post.id, title: post.title };
  }
);

export default zen;