# Run Identify at the edge

Run Identify in a Cloudflare Worker or Vercel Edge Middleware in front of your site, with the same policy bundle, the same verdict and the platform's own bot score where it has one.

Run Identify in front of your origin, at the edge, so a block, a rate limit or an approval challenge never reaches your servers. The templates use WebCrypto only and decide from the same policy bundle as the middleware.

**Before you begin:** create a secret key for your account in the console, and install the package in your Worker or Next.js project.

**This package isn't on npm yet.** Until then, every request in these docs runs over HTTP against the [sandbox](https://provenanceauth.com/docs/get-started/sandbox.md).

With access to the private Provenance repository, which your Provenance contact grants, install from it:

```bash
git clone https://github.com/michaelnbane-jpg/provenance.git provenance
cd provenance && pnpm install && pnpm build
node scripts/check-publishable.mjs --out ../provenance-packs
cd ../your-project
npm install ../provenance-packs/provenanceauth-sdk-1.0.0.tgz ../provenance-packs/provenanceauth-rails-1.0.0.tgz ../provenance-packs/provenanceauth-identify-1.0.0.tgz
```

**Availability:** `@provenanceauth/identify` is available to beta customers. The templates are in the package's `templates/` folder.

## 1. Deploy the Cloudflare Worker

```ts
// src/index.ts
import { identifyWorker } from "@provenanceauth/identify/edge/cloudflare";

export default identifyWorker({ origin: (request) => fetch(request) });
```

```bash
npx wrangler secret put PROVENANCE_KEY
npx wrangler deploy
```

The Worker reads the client address from `CF-Connecting-IP`, which Cloudflare sets. A request that passes reaches your origin with an `X-Provenance-Identify` header, such as `class=signed_agent;decision=allow;agent=agt_01M5T7VVC8FGYV5V4TGZSFATGN`. It names no person.

## 2. Read Cloudflare's bot score

When your zone has Bot Management, the Worker reports its score as `vendor_score` next to Identify's own `bot_score`, never merged with it:

```json
{ "vendor_score": { "vendor": "cloudflare", "status": "ok", "score": 0.99, "raw": 2 } }
```

`score` maps Cloudflare's 1 (automated) to 99 (human) onto 1 to 0. A zone without Bot Management reports `status: "absent"`. Write a rule on `vendor_score` if you want to act on it; a signed agent stays `signed_agent` whatever the score.

## 3. Or use Vercel Edge Middleware

```ts
// middleware.ts
import { identifyVercel } from "@provenanceauth/identify/edge/vercel";

export default identifyVercel();
export const config = { matcher: "/((?!_next/static|favicon.ico).*)" };
```

Set `PROVENANCE_KEY` in the project's environment. Vercel exposes no bot score, so `vendor_score` is `absent` unless you pass `vendorScore`, for example Fingerprint with your own key ([add a vendor score](https://provenanceauth.com/docs/identify/guides/vendor-score.md)).

## 4. Choose the fail mode

The edge templates default to `failMode: "open"`: before the first bundle loads, requests pass and are logged, so the edge never takes your site down on its own. Pass `failMode: "closed"` to answer `503` instead. After the first load, the last good bundle keeps deciding even when a refresh fails, and decisions are reported with `bundle_stale`.

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| Every verdict has `bundle_stale` | The Worker can't reach the API to refresh its bundle | Check `PROVENANCE_KEY` and `PROVENANCE_API`. |
| `vendor_score.status` is `absent` on Cloudflare | The zone has no Bot Management | Nothing to fix; Identify's own `bot_score` still applies. |
| Signed agents fail behind the Worker | A route rewrote the host or path before the Worker ran | Run the Worker on the original hostname and path. |

## See also

- [Verify signed agents](https://provenanceauth.com/docs/identify/guides/verify-signed-agents.md)
- [Add a vendor score](https://provenanceauth.com/docs/identify/guides/vendor-score.md)
- [Signals and categories](https://provenanceauth.com/docs/identify/reference/signals.md)
