payfence-node
v1.1.0
Published
PayFence middleware for Express.js — API monetization in 3 lines
Maintainers
Readme
@payfence/middleware
Express middleware for API monetization via PayFence.
Quick Start
npm install @payfence/middlewareimport express from "express";
import { payfenceMiddleware } from "@payfence/middleware";
const app = express();
app.use(payfenceMiddleware({ apiKey: "your_site_api_key" }));Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| apiKey | string | (required) | Your PayFence site API key |
| baseUrl | string | https://api.payfence.io | PayFence API base URL |
| onDeny | function | 402 JSON response | Custom deny handler |
| excludePaths | string[] | [] | Paths to skip authorization |
HMAC Verification (Proxy Mode)
import { verifyPayFenceSignature } from "@payfence/middleware";
app.use((req, res, next) => {
const valid = verifyPayFenceSignature({
secret: process.env.ORIGIN_SECRET!,
method: req.method,
path: req.path,
timestamp: req.headers["x-payfence-timestamp"] as string,
requestId: req.headers["x-payfence-request-id"] as string,
body: req.body,
signature: req.headers["x-payfence-signature"] as string,
});
if (!valid) return res.status(403).json({ error: "Invalid signature" });
next();
});