# knownprint quickstart for AI coding agents

You have been asked to add knownprint to an app. Follow these steps in order; each one says when
to stop and ask the user. knownprint is a hosted anti-fraud service built on browser
fingerprinting, and the integration has three parts:

1. **A proxy rule** on nginx or Caddy that forwards `/kp/` on the site's own domain to knownprint.
2. **A script and a helper in the page** that get a **request id** when the user submits a
   protected form (signup, login, checkout, password reset) and send it as `kp_request_id`.
3. **One module in the backend** that reads the event for that request id with the secret key,
   checks it, and decides: allow, challenge, review or block.

Each part has its own file under https://knownprint.com/quickstart/. Fetch only the ones the app's
stack needs.

## Rules

- **Never read, print or commit secrets.** Do not open `.env` files, secret stores or proxy secret
  files, and never put a secret key or proxy secret in code, a committed file, a log or your reply.
  The user puts the values into the environment.
- **Only the public key (`pk_...`) may appear in browser code.**
- **Ask before touching a live server.** No `sudo`, no installing files, no reload or restart of a
  web server that serves real traffic without the user's go-ahead. By default, write the
  configuration and the commands and hand them over in Step 8.
- **Unknown is never a pass, and never a block on its own.** A missing request id, a failed check
  or a `null` signal sends the user through the app's extra step (an email code, a review).

## Step 1: Look at the app

Without changing anything, find: the frontend (HTML forms, templates, or a single page app); the
backend language and framework; the web server in front of the site (an nginx config, a
`Caddyfile`, a `Dockerfile` or compose file running either), whether Cloudflare proxies the DNS,
and whether the web server reaches the app by `proxy_pass` / `reverse_proxy` or by `fastcgi_pass`
(PHP-FPM); the forms and handlers worth protecting; any Content Security Policy; and a shared store
(Redis, Valkey, or the framework's shared cache) for single-use checks.

If no nginx or Caddy that the user controls sits in front of the site (a serverless or edge
platform on its own), tell the user at Step 4 that the proxy rule needs one today; their
knownprint contact can advise.

## Step 2: Check for an existing install

List file names only, from tracked files only, so that no secret is printed:

```bash
git grep -l -e '/kp/agent.js' -e 'knownprint.load' -e 'kp_request_id' -e 'KNOWNPRINT_' \
  -e '@knownprint/' -e '/etc/knownprint/' -e 'import knownprint' -e '/v1/events/'
```

If anything matches, tell the user what is there and ask before changing it.

## Step 3: Get the account values

knownprint is in early access: there is no self-serve signup or command line tool, and the values
come with the user's account. If the user has none, send them to
https://knownprint.com/#early-access and prepare the code anyway: until the values arrive, every
request id is missing and the backend takes the challenge path.

Ask the user to set these in the app's environment themselves (never in the chat):

| Variable                  | What it is                                                                    | Used by          |
| ------------------------- | ----------------------------------------------------------------------------- | ---------------- |
| `KNOWNPRINT_PUBLIC_KEY`   | `pk_test_` or `pk_live_` and 32 characters                                    | The page         |
| `KNOWNPRINT_SECRET_KEY`   | `sk_test_` or `sk_live_` and 32 characters                                    | The backend only |
| `KNOWNPRINT_API`          | The API address that comes with the account                                   | The backend only |
| `KNOWNPRINT_SITE_ORIGINS` | Every origin whose pages run the script, comma separated, no trailing slashes | The backend only |

Build and test with the test environment's values. The public key must list the same origins
(set with the account, or `allowed_origins` through `POST /v1/keys`:
https://docs.knownprint.com/api/keys.md). If the frontend only exposes prefixed variables, expose
the public key alone under that prefix (`NEXT_PUBLIC_`, `VITE_`). The proxy secret, the recipe files
and the disclosure file `knownprint.json` also come with the account; they belong on the web
server, never in the repository.

## Step 4: Add the proxy rule

Use the account's recipe files as they are; never write a proxy rule of your own. Fetch the guide
for the web server:

| Web server                     | Fetch                                      |
| ------------------------------ | ------------------------------------------ |
| nginx (also behind Cloudflare) | https://knownprint.com/quickstart/nginx.md |
| Caddy                          | https://knownprint.com/quickstart/caddy.md |

The user fills three fields of `knownprint.json` (`operator`, `rights_url`, `contact`). When the
rule is in place, `https://<site>/kp/agent.js` and `https://<site>/.well-known/knownprint.json`
both answer `200`.

## Step 5: Add the script and the helper

Fetch https://knownprint.com/quickstart/browser.md. It loads `/kp/agent.js`, starts the agent when
the page opens, and gets a fresh request id at submit (a request id is checked within 10 seconds,
so never earlier), giving up after 3 seconds.

## Step 6: Verify in the backend

Fetch the module for the backend, use it as written, and call it from each protected handler:

| Backend                           | Fetch                                       |
| --------------------------------- | ------------------------------------------- |
| Node (Express, Fastify, Next.js)  | https://knownprint.com/quickstart/node.md   |
| Python (Django, Flask, FastAPI)   | https://knownprint.com/quickstart/python.md |
| PHP (Laravel, Symfony, plain PHP) | https://knownprint.com/quickstart/php.md    |
| Go                                | https://knownprint.com/quickstart/go.md     |
| Any other language                | https://knownprint.com/quickstart/other.md  |

Each module burns the request id, reads the event, checks that it is fresh, not replayed, from one
of the site's origins and from the address the site's own proxy reports, and returns a decision;
after a signup it attaches the account id. If the site sends a Content Security Policy, add:

```text
script-src 'self';
connect-src 'self' https://na1.knownprint.com;
worker-src blob:;
```

(`na1.knownprint.com` is the knownprint host accounts name today; use the one the account names. A
`'strict-dynamic'` policy needs the page's nonce on the script tags instead of `'self'`.)

## Step 7: Check it works

With the test keys, on a host where the proxy rule runs and whose origin is listed:

1. Both URLs of Step 4 answer `200`.
2. In the browser's developer tools, `/kp/agent.js` loads from the site's own origin and the
   protected request carries a `kp_request_id` starting with `evt_`.
3. The backend's verification returns ok and a decision. `address` means the proxy does not pass
   the visitor's address or the app is reachable around it; `origin` means the page's origin is
   missing from `KNOWNPRINT_SITE_ORIGINS`; `not_found` means the public and secret keys belong to
   different environments.
4. A test that posts without `kp_request_id` gets the challenge path, never a crash or a refusal.

## Step 8: Report to the user

Say what you changed, then what is left for them: the environment values for each environment, the
web server steps with exact commands, the origins on the public key, the policy directives, the
privacy notice (https://docs.knownprint.com/privacy.md), the go-live checklist
(https://docs.knownprint.com/install/go-live.md), and where to tune the decision thresholds.

## More

- Documentation: https://docs.knownprint.com (each page also as Markdown at its path plus `.md`)
- Index for language models: https://docs.knownprint.com/llms.txt
- The event object: https://docs.knownprint.com/events.md
- Signals and the suspect score: https://docs.knownprint.com/signals.md
- Fraud guides with worked rules: https://docs.knownprint.com/guides.md

These instructions are published at https://knownprint.com/quickstart.md; fetch them from there to
be sure they are knownprint's own.
