> ## Documentation Index
> Fetch the complete documentation index at: https://chainpatrol.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# ThreatDetector Constructor

> Instantiate a new ThreatDetector with local or cloud mode, an API key, storage adapter, proxy URL, and custom redirect URL for scam URL detection.

## Modes

There are two modes of operation for the Threat Detector:

* `local`: This mode uses a local database of scam URLs. Use this mode for offline threat detection and testing.
* `cloud`: This mode uses ChainPatrol's cloud service to detect scams.

### Local Mode

To use the local mode, you need to instantiate the `ThreatDetector` class with the `mode` option set to `local`.

```js theme={null}
const detector = new ThreatDetector({
  mode: "local",
});
```

### Cloud Mode

To use the cloud mode, you need to instantiate the `ThreatDetector` class with the `mode` option set to `cloud` and provide your API key.

```js theme={null}
const detector = new ThreatDetector({
  mode: "cloud",
  apiKey: "YOUR_API_KEY",
});
```

## Parameters

**options** `object` *required*

**properties**

**mode** `'local' | 'cloud'` *required*

The `mode` property controls the mode of operation for the Threat Detector.

The value of this property can be either `local` or `cloud`.

**apiKey** `string`

The `apiKey` property is the API Key for accessing ChainPatrol's APIs. We
recommend using an environment variable to store your API Key.

This property is required when the `mode` property is set to `cloud`,
even if `proxyUrl` is set. Leaving it out throws a `Missing API key`
error. The key is sent in the `X-API-KEY` header, so if your proxy adds
the real key on the server, pass any non-empty placeholder here.

**proxyUrl** `string`

The `proxyUrl` property is an optional property that can be used to
specify a proxy URL for accessing ChainPatrol's APIs.

This property is useful when you are behind a firewall or need to use a
proxy to access the internet.

This value is only used when the `mode` property is set to `cloud`.

**redirectUrl** `string | (url: string) =\> string`

The `redirectUrl` property is an optional property that can be used to
specify a redirect URL for when the detector detects a scam.

By default, the detector will return the ChainPatrol warning page URL,
but you can override this by specifying your own URL.

If you specify a **string**, the string will be used as the redirect URL
with the scam URL appended as the `originUrl` query parameter:

```js theme={null}
const detector = new ThreatDetector({
  redirectUrl: "https://your-warning-page.com/",
});
```

If you specify a **function**, the function will be called with the scam URL
as the first argument:

```js theme={null}
const detector = new ThreatDetector({
  redirectUrl: (url) => {
    return `https://your-warning-page.com/?url=${encodeURIComponent(url)}`;
  },
});
```

**storage** `Storage`

The `storage` property is an optional property that can be used to
specify a custom storage adapter for the detector to cache URL lookups.

By default, the detector will use the `Storage.memory()` adapter, which
stores the cache database in memory. This means that the database will be
cleared when the process exits. If you want to persist the database, you
can use the `Storage.browser()` adapter, which stores the database in
local storage, or the `Storage.extension()` adapter, which stores the database
in the browser's extension storage.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.