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
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"
}
}