@botbye/nest-fastify
v2.2.0
Published
BotBye! integration for NestJS with Fastify
Readme
@botbye/nest-fastify
BotBye! integration for NestJS with Fastify applications.
Full documentation: https://botbye.com/docs/server-side/node-js/nestjs/fastify
Install
npm i @botbye/nest-fastifyyarn add @botbye/nest-fastifyRequires @nestjs/common >= 10 and fastify >= 4 as peer dependencies.
Configuration
Register BotByeModule once in your root module:
import { Module } from "@nestjs/common";
import { BotByeModule } from "@botbye/nest-fastify";
@Module({
imports: [
BotByeModule.register({
// Use your project server-key
serverKey: "00000000-0000-0000-0000-000000000000",
}),
],
})
export class AppModule {}init options
| Option | Type | Required | Description |
|---|---|---|---|
| serverKey | string | Yes | Server key from your BotBye project |
| url | string | No | Override BotBye API endpoint (default: https://verify.botbye.com) |
| logger.level | "error" \| "warn" \| "info" \| "debug" \| "log" | No | Log level (default: "info") |
| logger.logger | TLogger | No | Custom logger instance implementing { error, warn, info, debug, log } |
| timeouts.evaluate | number | No | Timeout in milliseconds for each evaluate call |
| tokenExtractor | (request: FastifyRequest) => string \| null \| undefined | No* | Extracts the BotBye token from the Fastify request. *Required when using BotByeMiddleware — without it the middleware skips evaluation and returns an error result. |
Usage
After registering BotByeModule, inject TBotByeService using BOTBYE_SERVICE_DI_TOKEN to call evaluate from guards, controllers, or services.
import { Controller, Post, Inject, Req, ForbiddenException } from "@nestjs/common";
import { FastifyRequest } from "fastify";
import { BOTBYE_SERVICE_DI_TOKEN, TBotByeService } from "@botbye/nest-fastify";
@Controller()
export class AppController {
constructor(
@Inject(BOTBYE_SERVICE_DI_TOKEN) private readonly botbye: TBotByeService
) {}
@Post("/api/submit")
async submit(@Req() req: FastifyRequest) {
const result = await this.botbye.evaluate({
type: "validate",
request: {
request: req,
// "x-botbye-token" is an example — pass the token from wherever you store it
token: req.headers["x-botbye-token"] as string | null,
},
});
if (result.decision === "BLOCK") {
throw new ForbiddenException();
}
// proceed normally
}
}There are three event types — validate, risk, and full — each suited for a different layer of your application.
validate — edge-level bot check
Use at the edge — guard, route handler, middleware — when you just want to know: was this request made by a bot? No user or domain context needed.
Event fields:
{
type: "validate";
request:
// Option A: pass the Fastify request object directly — SDK extracts everything automatically
| { request: FastifyRequest; token?: string | null }
// Option B: construct request info manually
| { ip: string; headers: Record<string, string>; requestMethod?: string | null; requestUri?: string | null; token?: string | null };
customFields?: Record<string, string>;
}The SDK extracts IP, headers, method, and URI from the Fastify request automatically. You can also pass request info manually — see Option B above. The token is a one-time token generated by the BotBye client-side SDK that contains information about the user's device. Pass whatever the client sent; if no token is received, the decision will be "BLOCK".
import { Injectable, CanActivate, ExecutionContext, Inject } from "@nestjs/common";
import { FastifyRequest } from "fastify";
import { BOTBYE_SERVICE_DI_TOKEN, TBotByeService } from "@botbye/nest-fastify";
@Injectable()
export class BotByeGuard implements CanActivate {
constructor(
@Inject(BOTBYE_SERVICE_DI_TOKEN) private readonly botbye: TBotByeService
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<FastifyRequest>();
const result = await this.botbye.evaluate({
type: "validate",
request: {
request,
// "x-botbye-token" is an example — pass the token from wherever you store it
token: request.headers["x-botbye-token"] as string | null,
},
});
return result.decision !== "BLOCK";
}
}risk — domain-level risk scoring
Use inside services that already know the user: auth, payments, account management, etc. The purpose shifts from "is this a bot?" to "is something suspicious happening for this user?" — credential stuffing, account takeover, account sharing, logins from a new geo.
Event fields:
{
type: "risk";
request:
// Only ip is needed at this level
| { ip: string; headers?: Record<string, string>; requestMethod?: string | null; requestUri?: string | null; token?: string | null }
// Fastify request object also accepted if convenient
| { request: FastifyRequest };
event: {
type: string; // e.g. "login", "password_change", "checkout"
status: "ATTEMPTED" | "SUCCESSFUL" | "FAILED" | "UNKNOWN";
};
user: {
accountId: string;
username?: string | null;
email?: string | null;
phone?: string | null;
};
customFields?: Record<string, string>;
botbyeResult?: string;
}event and user are the key fields here — they define what action is being performed and who is performing it, which is what drives the risk score. ip is equally important: BotBye tracks which IPs access the account to detect patterns like account sharing, credential stuffing, and suspicious geo logins. Pass it directly as { ip }, or pass the Fastify request object if that's more convenient.
import { Injectable, Inject } from "@nestjs/common";
import { BOTBYE_SERVICE_DI_TOKEN, TBotByeService } from "@botbye/nest-fastify";
@Injectable()
export class AuthService {
constructor(
@Inject(BOTBYE_SERVICE_DI_TOKEN) private readonly botbye: TBotByeService
) {}
async onLoginAttempt(ip: string, userId: string, email: string, loginSucceeded: boolean) {
const result = await this.botbye.evaluate({
type: "risk",
request: { ip },
event: {
type: "login",
status: loginSucceeded ? "SUCCESSFUL" : "FAILED",
},
user: {
accountId: userId,
email,
},
});
if (result.decision === "BLOCK") {
// Lock account, trigger MFA, send alert, etc.
}
}
}Linking validate and risk events
When the same request is evaluated at two layers — for example, once at the edge in a guard (type: "validate") and then again inside a domain service (type: "risk") — BotBye can link both events and display them as a single event in the dashboard.
Step 1 — guard (edge layer): run validate and capture botbye_result:
// e.g. in BotByeGuard.canActivate()
const edgeResult = await this.botbye.evaluate({
type: "validate",
request: {
request,
// "x-botbye-token" is an example — pass the token from wherever you store it
token: request.headers["x-botbye-token"] as string | null,
},
});
const edgeBotbyeResult = edgeResult.botbye_result;
// Pass edgeBotbyeResult downstream — request object, function argument, shared context, etc.Step 2 — domain service (auth, payment, account management): pass it as botbyeResult in the risk call:
// e.g. in AuthService.onLoginAttempt()
const riskResult = await this.botbye.evaluate({
type: "risk",
request: { ip },
event: {
type: "login",
status: loginSucceeded ? "SUCCESSFUL" : "FAILED",
},
user: {
accountId: userId,
email,
},
botbyeResult: edgeBotbyeResult,
});botbye_result is optional in the response — if it is absent, omit botbyeResult and the events will be recorded independently.
full — edge check and domain scoring in one call
Use when you have all context at once: raw request, token, user, and event. Equivalent to running validate and risk in a single call. A login endpoint is a typical example — it receives the HTTP request and immediately knows the user and outcome.
Event fields:
{
type: "full";
request:
| { request: FastifyRequest; token?: string | null }
| { ip: string; headers: Record<string, string>; requestMethod?: string | null; requestUri?: string | null; token?: string | null };
event: {
type: string;
status: "ATTEMPTED" | "SUCCESSFUL" | "FAILED" | "UNKNOWN";
};
user: {
accountId: string;
username?: string | null;
email?: string | null;
phone?: string | null;
};
customFields?: Record<string, string>;
}import { Injectable, Inject, ForbiddenException } from "@nestjs/common";
import { FastifyRequest } from "fastify";
import { BOTBYE_SERVICE_DI_TOKEN, TBotByeService } from "@botbye/nest-fastify";
@Injectable()
export class AuthService {
constructor(
@Inject(BOTBYE_SERVICE_DI_TOKEN) private readonly botbye: TBotByeService
) {}
async login(request: FastifyRequest, email: string, password: string) {
const user = await this.findUser(email);
const loginSucceeded = user && (await this.checkPassword(user, password));
const result = await this.botbye.evaluate({
type: "full",
request: {
request,
// "x-botbye-token" is an example — pass the token from wherever you store it
token: request.headers["x-botbye-token"] as string | null,
},
event: {
type: "login",
status: loginSucceeded ? "SUCCESSFUL" : "FAILED",
},
user: {
accountId: user?.id ?? "unknown",
email,
},
});
if (result.decision === "BLOCK") {
throw new ForbiddenException();
}
// proceed normally
}
}Response
evaluate always returns a Promise<TEvaluationResult>:
type TEvaluationResult =
| {
decision: "ALLOW" | "BLOCK" | "CHALLENGE";
request_id: string;
risk_score: number;
scores: Record<string, number>;
signals: string[];
botbye_result?: string;
}
| {
decision: "ALLOW" | "BLOCK" | "CHALLENGE";
botbye_result?: string;
error: { message: string };
};Check result.decision to decide how to handle the request:
"ALLOW"— request appears legitimate, proceed normally"BLOCK"— bot or suspicious activity detected, block the request"CHALLENGE"— uncertain, consider issuing a CAPTCHA, MFA, or additional verification step
When the response contains an error field, BotBye could not evaluate the request (e.g. invalid server key). In that case decision defaults to "ALLOW" so that a misconfiguration does not block real users — but you should monitor and fix the underlying error.
Response examples
Blocked (bot detected):
{
"request_id": "f77b2abd-c5d7-44f0-be4f-174b04876583",
"decision": "BLOCK",
"risk_score": 0.95,
"scores": { "bot": 0.95 },
"signals": ["AutomationTool"]
}Allowed:
{
"request_id": "f77b2abd-c5d7-44f0-be4f-174b04876583",
"decision": "ALLOW",
"risk_score": 0.05,
"scores": { "bot": 0.05, "ato": 0.02 },
"signals": []
}Challenge:
{
"request_id": "f77b2abd-c5d7-44f0-be4f-174b04876583",
"decision": "CHALLENGE",
"risk_score": 0.65,
"scores": { "bot": 0.65 },
"signals": ["SuspiciousFingerprint"],
"challenge": { "type": "CAPTCHA", "token": "..." }
}Invalid serverKey:
{
"decision": "ALLOW",
"error": { "message": "[BotBye] Bad Request: Invalid Server Key" }
}Middleware usage
BotByeMiddleware evaluates every incoming request automatically and stores the result on the request object. Use the @BotByeResponse() parameter decorator to access the result in a controller without calling evaluate manually.
Provide a tokenExtractor in module options so the middleware knows where to find the BotBye token:
// app.module.ts
import { Module, NestModule, MiddlewareConsumer } from "@nestjs/common";
import { BotByeModule, BotByeMiddleware } from "@botbye/nest-fastify";
@Module({
imports: [
BotByeModule.register({
// Use your project server-key
serverKey: "00000000-0000-0000-0000-000000000000",
// "x-botbye-token" is an example — pass the token from wherever you store it
tokenExtractor: (req) => req.headers["x-botbye-token"] as string | undefined,
}),
],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
// Global: protect all routes
consumer.apply(BotByeMiddleware).forRoutes("*");
}
}Access the result in a controller with @BotByeResponse():
// app.controller.ts
import { Controller, Post, ForbiddenException } from "@nestjs/common";
import { BotByeResponse } from "@botbye/nest-fastify";
import type { TEvaluationResult } from "@botbye/nest-fastify";
@Controller()
export class AppController {
@Post("/api/submit")
async submit(@BotByeResponse() result: TEvaluationResult) {
if (result.decision === "BLOCK") {
throw new ForbiddenException();
}
// proceed normally
}
}To apply middleware only to specific routes, replace .forRoutes("*") with a path or route descriptor:
// Scoped: protect only routes registered under "protected"
consumer.apply(BotByeMiddleware).forRoutes("protected");Anti-Phishing
BotBye's anti-phishing detects look-alike sites that clone your pages to steal credentials. This integration serves the detection catcher from your own origin, so the BotBye domain stays hidden from the client.
Anti-phishing is identified by its own clientKey (available in your Phishing Project in the Dashboard), not the server key used by evaluate, so it is configured separately.
Init
Register BotByePhishingModule alongside BotByeModule in your root module:
import { Module } from "@nestjs/common";
import { BotByePhishingModule } from "@botbye/nest-fastify";
@Module({
imports: [
BotByePhishingModule.register({
// clientKey from your Phishing Project on the Admin Dashboard
clientKey: "00000000-0000-0000-0000-000000000000",
}),
],
})
export class AppModule {}register options
| Option | Type | Required | Description |
|---|---|---|---|
| clientKey | string | Yes | clientKey from your Phishing Project on the Admin Dashboard |
| url | string | No | Override BotBye API endpoint (default: https://verify.botbye.com) |
| logger.level | "error" \| "warn" \| "info" \| "debug" \| "log" | No | Log level (default: "info") |
| logger.logger | TLogger | No | Custom logger instance implementing { error, warn, info, debug, log } |
| timeouts.fetchCatcher | number | No | Timeout in milliseconds for each fetchCatcher call |
Usage
Anti-phishing needs two routes on your own origin, each proxied through fetchCatcher:
- SVG route — serves the SVG catcher. This is the URL your client passes to
getCatcher({ url }). - PNG route — serves the PNG that the SVG references (via
innerPngUrl).
After registering BotByePhishingModule, inject TBotByePhishingService using BOTBYE_PHISHING_SERVICE_DI_TOKEN and call fetchCatcher from a controller. Pass the Fastify request as request and the format. For the SVG, innerPngUrl must be the absolute URL of your PNG route — the browser loads that PNG directly from your origin.
import { Controller, Get, Inject, Req, Res } from "@nestjs/common";
import { FastifyRequest, FastifyReply } from "fastify";
import { BOTBYE_PHISHING_SERVICE_DI_TOKEN, TBotByePhishingService } from "@botbye/nest-fastify";
// Absolute URL of your PNG endpoint — the SVG catcher references it through innerPngUrl.
const PNG_CATCHER_URL = "https://your-site.example/botbye-catcher.png";
@Controller()
export class PhishingCatcherController {
constructor(
@Inject(BOTBYE_PHISHING_SERVICE_DI_TOKEN) private readonly phishing: TBotByePhishingService
) {}
// Endpoint 1 — SVG catcher (this URL goes into the catcher element on your pages)
@Get("/botbye-catcher.svg")
async svg(@Req() request: FastifyRequest, @Res() reply: FastifyReply) {
const catcher = await this.phishing.fetchCatcher({
request,
format: "svg",
innerPngUrl: PNG_CATCHER_URL, // absolute URL of the PNG endpoint below
});
reply.status(catcher.status).headers(catcher.headers).send(Buffer.from(catcher.body));
}
// Endpoint 2 — companion PNG the SVG above references via innerPngUrl (PNG_CATCHER_URL)
@Get("/botbye-catcher.png")
async png(@Req() request: FastifyRequest, @Res() reply: FastifyReply) {
const catcher = await this.phishing.fetchCatcher({ request, format: "png" });
reply.status(catcher.status).headers(catcher.headers).send(Buffer.from(catcher.body));
}
}We recommend embedding the SVG catcher: it is designed to keep tracking even when a phishing site copies all of your assets to its own infrastructure (the PNG route exists because the SVG catcher relies on it).
Documentation
- Web: https://botbye.com/docs/server-side/node-js/nestjs/fastify
- Markdown (for AI tools and agents): https://botbye.com/docs/server-side/node-js/nestjs/fastify.md
