SDK options
All the options shared by the Zarex SDKs, whatever your framework.
These options are passed to your framework's function: withZarex / zarexNext, zarex, zarexFastify, zarexHandle or zarexBun.
Options
| Option | Type | Role |
|---|---|---|
key | string | Required. The project's API key. It is never written to the logs. |
rules | Rule[] | Required. From 1 to 10 rules. |
ipSource | see The IP address | How to find the visitor's IP address. |
inspectBody | boolean | Send request bodies (Shield, sensitive data). Default true since 0.2.0 (false before); false to turn it off. It is inspected in memory, never stored. |
inspectCookies | boolean | Send the Cookie header (Shield, cookie filters). Default false. |
extraHeaders | string[] | Additional headers to send. Authorization, Proxy-Authorization, Set-Cookie and X-Zarex-Key are never sent; Cookie only with inspectCookies. |
call | (request) => ProtectCall | Data to provide for each request: see below. |
onDeny | function | Replaces the denial response with yours. With Next.js, SvelteKit and Bun, it must return a Response (otherwise: default denial, the request stays blocked). |
onDegraded | function | Called when Zarex could not decide (the request was let through, or denied on a critical route). |
failMode | "open" · "closed" · function | What to do if Zarex cannot decide: let through ("open", default) or deny ("closed"). See Critical routes. Since 0.2.0. |
secureHeaders | boolean or object | Adds HTTP security headers to your responses. |
responseGuard | boolean or object | Masks sensitive data that would end up in your responses. |
timeoutMs | number | How long to wait for the decision; beyond that, the request goes through. The default suits most cases. |
breaker | object or false | After several consecutive failures, lets requests through without waiting during a short pause. false to disable it. |
logger | { warn, error } or false | Where to write the SDK's warnings. false to silence them. |
baseUrl | string | Address of the Zarex service. Not needed in production; used for tests and local development (the ZAREX_URL variable does the same). |
The data of call
call receives your framework's request and returns, for that request:
| Field | Used by |
|---|---|
characteristics | Rate limiting and token budget: the visitor's identifiers (for example { userId: "42" }), hashed before being sent. 16 at most: beyond that, the extra ones are ignored and the SDK writes it to your logs. |
tokensRequested | Token budget: tokens consumed by the request. Default 1. |
email | Email validation. |
phone | Phone validation. |
text | Sensitive data and prompt injection. |
rules | Replaces the configured rules, for this request only. |
failMode | "open" or "closed" for this request only; takes precedence over the middleware option. See Critical routes. |
call is a synchronous function: it returns its data directly, not a promise. So read values that are already available (body already parsed, user already loaded). An error in call is ignored: the request continues, without that data.
Direct use
For full control, or outside the supported frameworks, create an instance and call protect yourself:
import { createZarex, shield, rateLimit } from "@zarex/express"; // every package exports it
const zarex = createZarex({
key: process.env.ZAREX_KEY!,
ipSource: "socket",
rules: [shield(), rateLimit({ max: 100, window: "1m" })],
});
const decision = await zarex.protect(
{ method: "GET", path: "/api/products", headers: { "user-agent": "…" }, socketIp: "41.7.7.7" },
{ characteristics: { userId: "42" } }
);
if (decision.denied) {
// deny the request
}protect never throws: if something goes wrong, the decision is allowed and degraded.