# Installation and Configuration

### Prerequisites

To successfully integrate using Netacea, please ensure you have:

1. Installed [Vercel command-line interface (CLI)](https://vercel.com/docs/cli), [Node.js](https://nodejs.org/en) (version 16.13.0 or higher) and [Git ](https://git-scm.com/)on you machine.
2. An active Vercel account with access to the edge middleware functionality.
3. A “Paid” Vercel subscription, in order to avoid any issues with request-limits etc.
4. An existing Vercel Project.
5. 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:

```json
{
"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.

{% hint style="info" %}
The attribute `mitigationType` is used to determine the mode of the integration. This can be one of three values:

* "**INGEST**" - This integration mode is monitoring only, meaning that no mitigation actions will be executed. This is recommended during POCs.
* "**MITIGATE**" - This is the "normal" integration mode, meaning that the Netacea solution will monitor and actively mitigate requests.
* "**INJECT**" - This mode is used in custom solutions in which the mitigation action by Netacea will only be a recommendation using HTTP headers added to the request.
  {% endhint %}

{% hint style="danger" %}
cookieEncryptionKey, netaceaCookieName and netaceaCaptchaCookieName variables are used to increase security by concealing Netacea's default cookie names and values from public view.
{% endhint %}

### 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:

```tsconfig
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 control
* Add `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.
