Installation and Configuration
The Netacea Vercel integration detects bot activity and performs mitigating actions in line with the client blocking strategy.
Prerequisites
To successfully integrate using Netacea, please ensure you have:
Installed Vercel command-line interface (CLI), Node.js (version 16.13.0 or higher) and Git on you machine.
An active Vercel account with access to the edge middleware functionality.
A “Paid” Vercel subscription, in order to avoid any issues with request-limits etc.
An existing Vercel Project.
Details of the relevant API and Secret keys, and a Kinesis endpoint - provided by Netacea.
Installation
Within your Vercel project, run:
npm i @netacea/vercel
.
This will install Netacea package to your project.
Configure the Vercel Project
Within the project, create NetaceaConfig.json
in the same directory as your middleware(described in the "Middleware Setup" section). Then update its contents to match the below:
{
"apiKey": "API-KEY-PROVIDED-BY-NETACEA",
"secretKey": "SECRET-KEY-PROVIDED-BY-NETACEA",
"mitigationType": "PREFERRED-MITIGATION-TYPE",
"ingestType": "KINESIS",
"kinesis": {
"kinesisStreamName": "NAME-PROVIDED-BY-NETACEA",
"kinesisAccessKey": "KEY-PROVIDED-BY-NETACEA",
"kinesisSecretKey": "KEY-PROVIDED-BY-NETACEA"
},
"cookieEncryptionKey": "ENCRYPTION-KEY-PROVIDED-BY-NETACEA",
"netaceaCookieName": "_cookieName",
"netaceaCaptchaCookieName": "_captchaCookieName",
"enableDynamicCaptchaContentType": false,
"timeout": 3000
}
Replace the values with the details that Netacea has provided you and save the file.
cookieEncryptionKey, netaceaCookieName and netaceaCaptchaCookieName variables are used to increase security by concealing Netacea's default cookie names and values from public view.
Middleware Setup
A middleware.ts
should be created in root directory of your project, if not present already. The following code should be placed in the middleware.ts
file:
import { NextRequest, NextResponse } from 'next/server'
import { waitUntil } from '@vercel/functions'
import * as netaceaConfig from './NetaceaConfig.json'
import NetaceaVercelIntegration from '@netacea/vercel'
import type { NetaceaVercelIntegrationArgs } from '@netacea/vercel'
let netaceaWorker: NetaceaVercelIntegration | undefined = undefined
export default async function middleware(req: NextRequest) {
try {
// Initialize Netacea worker
if (netaceaWorker === undefined) {
netaceaWorker = new NetaceaVercelIntegration(netaceaConfig as NetaceaVercelIntegrationArgs)
}
// Run Netacea integration
const event = { request: req }
const netaceaResult = await netaceaWorker.run(event, originRequest)
// Asynchronously ingest the Netacea result, without adding latency to the request
waitUntil(netaceaWorker.ingest(req, netaceaResult))
return netaceaResult.response
} catch (error) {
console.error("Netacea Middleware Error:", error)
return NextResponse.next()
}
}
async function originRequest(request: Request): Promise<NextResponse> {
return NextResponse.next({
headers: request.headers
})
}
Deploy the project to Vercel
To successfully deploy the project to Vercel, the following steps must be followed:
Commit
NetaceaConfig.json
to your source controlAdd
NetaceaConfig.json
to your.gitignore
The project should then be pushed to your Git repository.
Vercel will automatically detect the push to Github and initiate the deployment process.
Last updated