Consenti

Backend — @consenti/api

The Consenti backend module records consent to a database, serves the REST API, provides an admin dashboard, and runs server-side plugins. It has zero external runtime dependencies — only Node.js 24 built-ins.

Installation

bash
npm install @consenti/api

Quick start

Add it to your existing Node.js server — consenti.router mounts all routes inside your app. No separate process or port needed.

ts
import express from 'express'
import { createConsenti } from '@consenti/api'

const app = express()

const consenti = createConsenti({
  storage: { driver: 'node:sqlite', path: './consenti-data' },  // path is a directory
  auth: {
    mode: 'local',
    adminEmail: '[email protected]',
    adminPassword: process.env.CONSENTI_ADMIN_PASSWORD!,
  },
  dashboard: true,
})

app.use(consenti.router)  // all Consenti routes now live under /consenti
app.listen(3000)

What createConsenti returns

PropertyTypeDescription
handlerhttp.RequestListenerPlain Node.js HTTP handler — use with http.createServer or as a terminal handler
routerExpress-compatible middlewareMount with app.use(consenti.router); calls next() for unmatched routes
fastifyHandlerFastify pluginRegister with fastify.register(consenti.fastifyHandler)
honoAppHono appUse with consenti.honoApp.fetch(request) for fetch-based runtimes
readyPromise<void>Resolves when storage is connected, migrations run, and bootstrap admin is created
storageStorageAdapterDirect access to the storage layer for custom queries
servicesservice mapProgrammatic access — consenti.services.profile.get(id), consenti.services.consent.submit(...), etc.
eventBusEventEmitterSubscribe to lifecycle events: consent.created, profile.updated, cache:warm, etc.
destroy()Promise<void>Graceful shutdown — closes DB connection and calls plugin destroy()

Architecture

text
Request → Routes → Services → Repositories → StorageAdapter → Database

           Plugins (hooks fire at service layer)
  • Routes — validate input, call services, return HTTP responses
  • Services — business logic (consent validation, versioning, expiry)
  • Repositories — translate domain objects to/from the storage adapter
  • StorageAdapter — the only layer that knows about the DB engine
  • Plugins — lifecycle hooks fired at the service layer

Storage adapters

DriverConfig valueNotes
JSON filejsonDefault — zero install, dev/low-traffic only. Not suitable for production.
node:sqlite (built-in)node:sqliteNode 22.5+ built-in. Zero install. Recommended for Node 22+.
better-sqlite3better-sqlite3 or sqliteNative addon — faster, needs build toolchain. Node 20+.
WASM SQLitenode-sqlite3-wasmPure WASM, no compilation. Node 20+.
PostgreSQLpostgresqlOptional peer dep: npm install pg
MySQL / MariaDBmysqlOptional peer dep: npm install mysql2
MongoDBmongodbOptional peer dep: npm install mongodb

Sections