Fastify
NodeJS Fastify

Install

1
npm i botbye-node-fastify
or
1
yarn add botbye-node-fastify

Configuration

1. Import init from botbye-node-fastify module:

1
import { init } from "botbye-node-fastify";

2. Call init with your project server-key (available inside your Project):

1
2
3
4
init({
  // Use your project server-key
  serverKey: "00000000-0000-0000-0000-000000000000"
});

Usage

Use validateRequest on handlers where you need bot protection:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { validateRequest } from "botbye-node-fastify";

fastify.get('/', async(request, reply) => {
  // Get token from header or any place you store it.
  // For example in "x-botbye-token" header
  const botbyeToken = request.headers["x-botbye-token"]

  const options = {
    token: botbyeToken,
    request,
  }

  const botbyeResponse = await validateRequest(options);

  const isAllowed = botbyeResponse.result?.isAllowed ?? true;

  if (!isAllowed) {
    reply.code(403).send()
    return;
  }

  reply.send({ hello: "world" })
})

Examples of BotBye API responses

Blocked (bot detected):

1
2
3
4
5
6
7
8
{
  "request_id": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "decision": "BLOCK",
  "risk_score": 0.95,
  "scores": { "bot": 0.95 },
  "signals": ["AutomationTool"],
  "config": { "bypass_bot_validation": false }
}

Allowed:

1
2
3
4
5
6
7
8
{
  "request_id": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "decision": "ALLOW",
  "risk_score": 0.05,
  "scores": { "bot": 0.05, "ato": 0.02 },
  "signals": [],
  "config": { "bypass_bot_validation": false }
}

Challenge:

1
2
3
4
5
6
7
8
9
{
  "request_id": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "decision": "CHALLENGE",
  "risk_score": 0.65,
  "scores": { "bot": 0.65 },
  "signals": ["SuspiciousFingerprint"],
  "challenge": { "type": "captcha", "token": "..." },
  "config": { "bypass_bot_validation": false }
}

Invalid server-key:

1
2
3
4
5
{
  "decision": "ALLOW",
  "config": { "bypass_bot_validation": true },
  "error": { "message": "[BotBye] Bad Request: Invalid Server Key" }
}