Getting Started

Quick Start

Get permly working in 2 minutes

What it does

This page takes you from an empty project to your first permission check.

Step 1: Generate setup

Answer a few questions. permly detects your TypeScript and module setup automatically:

npx permly init

This creates two files:

  • src/permly.js (or .ts): your roles and permissions
  • migrations/<timestamp>_permly_init.sql: database tables (.mjs for MongoDB)

Step 2: Create the tables

Point permly at your database and run the migration:

DATABASE_URL=mysql://user:password@localhost:3306/mydb npx permly migrate

For Postgres:

DATABASE_URL=postgresql://user:password@localhost:5432/mydb npx permly migrate

For MongoDB:

DATABASE_URL=mongodb://localhost:27017/mydb npx permly migrate

Or run the generated SQL file with your own migration tool.

Step 3: Use it

Call setupPermissions() once at startup (it creates the roles and permissions). Then check anywhere:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

await perms.user(42).assignRole("editor");

const allowed = await perms.user(42).can("posts.create");
console.log(allowed); // true or false
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

await perms.user(42).assignRole("editor");

const allowed: boolean = await perms.user(42).can("posts.create");
console.log(allowed);

Example: protect a route

In Express, use the middleware:

import express from "express";
import { requirePermission } from "permly/express";
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

const app = express();
app.post("/posts", requirePermission(perms, "posts.create"), (req, res) => {
  res.json({ ok: true });
});

app.listen(3000);
import express from "express";
import { requirePermission } from "permly/express";
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

const app = express();
app.post("/posts", requirePermission(perms, "posts.create"), (_req, res) => {
  res.json({ ok: true });
});

app.listen(3000);

Next steps

Troubleshooting

"Permission not found"? Make sure you called setupPermissions() at startup — it creates the roles and permissions in the database.

SQL file instead of migrate? Run the generated migration file with your own tool (e.g., prisma migrate deploy, knex migrate:latest).

No database yet? Use the memory adapter to try permly first:

import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";

const perms = createPermissions({
  adapter: memoryAdapter(),
  permissions: ["posts.create", "posts.edit"],
  roles: ["editor"],
});

await perms.sync();