> This page is for Applications Protection.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.humansecurity.com/llms.txt.

# API

## HumanSecurityEnforcer

The entity responsible for performing HUMAN enforcement.

### HumanSecurityEnforcer.initialize()

A static function that creates a new instance of the HumanSecurityEnforcer class from a HumanSecurityConfiguration object.

#### Service Worker Syntax

```ts
HumanSecurityEnforcer.initialize(config: HumanSecurityConfiguration) => Promise<HumanSecurityEnforcer>
```

* Parameters
  * params: HumanSecurityConfiguration
* Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to a new instance of the HumanSecurityEnforcer class

#### ES Modules Syntax

```ts
HumanSecurityEnforcer.initialize(config: HumanSecurityConfiguration, env?: Environment) => Promise<HumanSecurityEnforcer>
```

* Parameters
  * params: HumanSecurityConfiguration
  * env?: Environment
* Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to a new instance of the HumanSecurityEnforcer class

### enforce()

Executes the enforcement functionality, returning a request or response depending on which action should be taken by the worker.

**The function returns a Request when...**

1. The request should not be blocked.
2. The request should be blocked, but the enforcer has been configured to let these requests pass.

> **Note**
>
> Depending on the products and features enabled in the enforcer, the `enforce()` function may return a [Request](https://developers.cloudflare.com/workers/runtime-apis/request/) with additional headers that were not present on the original Request!

**The function returns a Response when...**

1. The request should be blocked, and the response is a block page generated by the enforcer.
2. The request was a first-party request, and the response is the first-party resource requested.

> **Note**
>
> Modifications can be made to this response as needed prior to returning it from the main function.

#### Service Worker Syntax

```ts
enforce(event: FetchEvent) => Promise<Request | Response>
```

* Parameters
  * event: [FetchEvent](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/)
* Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to a [Request](https://developers.cloudflare.com/workers/runtime-apis/request/) or [Response](https://developers.cloudflare.com/workers/runtime-apis/response/)

#### ES Modules Syntax

```ts
enforce(executionContext: ExecutionContext, request: Request) => Promise<Request | Response>
```

* Parameters
  * executionContext: [ExecutionContext](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/#parameters)
  * request: [Request](https://developers.cloudflare.com/workers/runtime-apis/request/)
* Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to a [Request](https://developers.cloudflare.com/workers/runtime-apis/request/) or [Response](https://developers.cloudflare.com/workers/runtime-apis/response/)

### postEnforce()

Performs any post-enforcement processing actions and final modifications to the response (i.e., setting cookies or headers) if needed.

#### Service Worker Syntax

```ts
postEnforce(event: FetchEvent, response: Response) => Promise<Response>
```

* Parameters
  * event: [FetchEvent](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/)
  * response: [Response](https://developers.cloudflare.com/workers/runtime-apis/response/)
* Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to a [Response](https://developers.cloudflare.com/workers/runtime-apis/response/).

#### ES Modules Syntax

```ts
postEnforce(executionContext: ExecutionContext, response: Response) => Promise<Response>
```

* Parameters
  * executionContext: [ExecutionContext](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/#parameters)
  * response: [Response](https://developers.cloudflare.com/workers/runtime-apis/response/)
* Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to a [Response](https://developers.cloudflare.com/workers/runtime-apis/response/).