Next.js
Next.js
Install
1
npm i botbye-nextjs
or
1
yarn add botbye-nextjs
Client Configuration
Init BotBye! using BotByeComponent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { BotByeComponent } from "botbye-nextjs";
default function App() {
/* Use your client-key */
const clientKey = "00000000-0000-0000-0000-000000000000"
return (
<>
<BotByeComponent clientKey={clientKey}/>
...
</>
);
}
Server Configuration
1. Init BotBye! with server-key
1
2
3
4
5
6
import { init } from "botbye-nextjs";
init({
/* Use your server-key */
serverKey: "00000000-0000-0000-0000-000000000000"
})
Client Usage
1. To run challenge and generate BotBye! token call runChallenge. Send this token in any convenient way to the server. For example in x-botbye-token `typescript "use client" import { runChallenge } from "botbye-nextjs";
const LoginButton = () => { const onClick = async () => { const token = await runChallenge();
fetch("/api/login", { method: "POST", headers: { ["x-botbye-token"]: token } }) }
return (<button onClick={onClick}>{"Login"}</button>) } `
Server Usage
Add request validation
By middleware:
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
32
33
34
35
36
// middleware.ts
import { NextRequest, NextResponse } from "next/server";
import { validateRequest } from "botbye-nextjs";
async function middleware(request: NextRequest) {
/* Get token from header or any place you store it */
const token = request.headers.get("x-botbye-token") ?? "";
/* Additional custom fields for linking request */
const customFields = {
someKey: "some-value"
}
const botbyeResponse = await validateRequest({
request,
token,
customFields,
})
const isAllowed = botbyeResponse.result?.isAllowed ?? true;
/* Decline request when it not pass validation */
if (!isAllowed) {
return new NextResponse(null, {status: 403, statusText: "Forbidden"})
}
return NextResponse.next();
}
/* Api endpoints middleware */
const config = {
matcher: '/api/:path',
}
or
By handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// app/api/login/route.ts
import { NextRequest, NextResponse } from 'next/server'
import { validateRequest } from "botbye-nextjs";
async function POST(request: NextRequest) {
// Get token from header or any place you store it.
// For example, from "x-botbye-token" header
const token = request.headers.get("x-botbye-token") ?? "";
const botbyeResponse = await validateRequest({
request,
token,
})
const isAllowed = botbyeResponse.result?.isAllowed ?? true;
/* Decline request when it not pass validation */
if (!isAllowed) {
return new NextResponse(null, {status: 403, statusText: "Forbidden"})
}
return NextResponse.json({message: 'Login successful'})
}
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"
}
}