Express
NodeJS Express

Install

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

Configuration

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

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

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
24
25
26
27
28
29
30
31
import { validateRequest } from "botbye-node-express";

app.post('/protected', async (req, res) => {
  // Get token from header or any place you store it.
  // For example in "x-botbye-token" header
  const botbyeToken = req.headers["x-botbye-token"]

  // Additional custom fields for linking request
  const customFields = {
    someKey: "some-value"
  }

  const options = {
    token: botbyeToken,
    request: req,
    customFields,
  }

  /**
   * @param {Object} options - Options for request validation
   * @return {Promise} - botByeResponse promise
   */
  const botByeResponse = await validateRequest(options);

  const isBot = botByeResponse.result?.isBot;

  if (isBot) {
  ...
  }
...
})

Examples of BotBye API responses

Bot detected:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": false
  },
  "error": "Automation tool used"
}

Bot not detected:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": true
  },
  "error": null
}

Request banned by custom rule:

1
2
3
4
5
6
7
8
9
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": false
  },
  "error": {
    "message": "Banned by rule: MY_CUSTOM_RULE"
  }
}

Invalid server-key:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": null,
  "error": {
    "message": "[BotBye] Bad Request: Invalid Server Key"
  }
}