# Verify a response

Verification is a **server-to-server POST** using `Content-Type: application/json`.
Store your operator-approved full URL as `KATCHA_VERIFY_URL` on the server. The
local widget demo uses a legacy verification path; do not infer or rename the
endpoint from product branding. The public Katcha origin is unconfirmed.

## Request

| JSON field | Required | Description |
| --- | --- | --- |
| `challenge_id` | Yes | Token submitted in the browser's `katcha-token` field |
| `site_key` | Yes | Expected site key from trusted server configuration |
| `secret_key` | Yes | Secret for the same account/site, read only on the server |

```json
{
  "challenge_id": "TOKEN_FROM_FORM",
  "site_key": "YOUR_SITE_KEY",
  "secret_key": "YOUR_SERVER_ONLY_SECRET"
}
```

Do not accept a verification URL, expected site key, or success flag from the
browser. Treat tokens as single-use and time-limited; verify during the protected
request rather than caching a token for later actions.

## Response

| Field | Meaning |
| --- | --- |
| `success` | Boolean; only the literal `true` permits the action |
| `error-codes` | Optional list of verification errors |

Illustrative responses from the v4 contract:

```json
{ "success": true }
```

```json
{ "success": false, "error-codes": ["invalid-input-response"] }
```

The local widget demo confirms request fields and the `success` check. Error
identifiers also exist in the local API catalog. The verification service itself
is not present in this workspace, so confirm its response serialization against
staging before release. Do not substitute the separate `/2/siteverify` protocol
(`sitekey`, `secret`, `response`) used by some older clients.

## Node.js example

This framework-neutral helper uses server-side `fetch`. Supply the form token;
it returns `false` on missing values, HTTP failure, timeout, malformed JSON,
or any response without `success: true`.

```js
export async function verifyKatcha(token) {
  const verifyUrl = process.env.KATCHA_VERIFY_URL;
  const siteKey = process.env.KATCHA_SITE_KEY;
  const secretKey = process.env.KATCHA_SECRET_KEY;
  if (typeof token !== 'string' || !token.trim()) return false;
  if (!verifyUrl || !siteKey || !secretKey) return false;
  try {
    const endpoint = new URL(verifyUrl);
    if (endpoint.protocol !== 'https:') return false;
    const response = await fetch(endpoint, {
      method: 'POST',
      redirect: 'error',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        challenge_id: token,
        site_key: siteKey,
        secret_key: secretKey
      }),
      signal: AbortSignal.timeout(5000)
    });
    if (!response.ok) return false;
    const result = await response.json();
    return result?.success === true;
  } catch (error) {
    return false;
  }
}
```

In your route, after parsing the form, call
`await verifyKatcha(request.body['katcha-token'])`. If it returns `false`, stop
before any protected side effect. Otherwise apply normal authentication,
authorization, input validation, and CSRF checks. Never log the token or secret,
and do not automatically replay verification after a timeout.

## Error code reference

| Error code | Meaning |
| --- | --- |
| `missing-input-sitekey` | `site_key` is missing |
| `missing-input-secret` | `secret_key` is missing |
| `missing-input-response` | `challenge_id` is missing |
| `bad-request` | Invalid or malformed request |
| `invalid-input-sitekey` | Invalid site key |
| `invalid-input-secret` | Invalid secret |
| `invalid-input-response` | Invalid token |
| `timeout-or-duplicate` | Expired or already used token |

Ask for a fresh challenge after expiry or duplicate use. Correct server
configuration errors rather than bypassing verification. Do not assume a precise
token lifetime without confirmation from your deployment.
