# Widget configuration

This reference targets the CDN API exposed as `window.katcha`, not the native
package factory API. Obtain your deployed widget URL from your operator.

## Katcha container configuration

| HTML attribute | Render option | Values and behavior |
| --- | --- | --- |
| `data-site-key` | `site_key` | Required public site key |
| `data-size` | `size` | `normal` (default) or `invisible` |
| `data-theme` | `theme` | `light` (default) or `dark` |
| `data-color` | `color` | Optional color name or hex code |
| `data-error-print` | `error_print` | `0` shows inline errors (default); `1` hides them |
| `data-lang` | `lang` | Use `en` for English; implementation default is `fa` |
| `data-callback` | `callback` | Success; receives the token string |
| `data-rendered-callback` | `rendered_callback` | Widget rendered |
| `data-blocked-callback` | `blocked_callback` | Challenge blocked |
| `data-error-callback` | `error_callback` | Receives legacy `{ code, message }` errors |
| `data-reset-callback` | `reset_callback` | Reset notification; public `reset()` does not emit it |
| `data-expired-callback` | `expired_callback` | Token expired |
| `data-chlexpired-callback` | `chlexpired_callback` | Challenge expired |
| `data-clicked-callback` | `clicked_callback` | Checkbox clicked or execution started |
| `data-opened-callback` | `opened_callback` | Challenge opened |
| `data-closed-callback` | `closed_callback` | Challenge closed |

For automatic rendering, callback names must resolve to functions on `window`.
For explicit rendering, pass functions or global function names. Success callbacks
receive a **string**, unlike the object returned by `execute()`.

```html
<div class="katcha" data-site-key="YOUR_SITE_KEY" data-lang="en"
     data-theme="dark" data-callback="onSolved"></div>
<script>
  window.onSolved = function (token) {
    document.getElementById('captcha-status').textContent = token ? 'Challenge completed.' : '';
  };
</script>
```

Provide a status element and load your deployment's widget script after defining
callbacks. A completion message is not authorization: [verify on the server](/markdown/api/verify.md).

## JavaScript API

The following methods belong to the CDN/SDK public API. Reset, token retrieval,
and execution target the first widget when the widget ID is omitted. Pass an
explicit ID when a page contains more than one widget.

### katcha.render(container, params)

Renders a widget and returns a **string widget ID**. Supply a CSS
selector (for example, `#captcha`) or element, and options from the table above. The native package's
`createKatcha()` interface returns a widget handle instead; do not mix the APIs.

### katcha.reset(widgetID)

Resets the selected widget and clears its token. Returns `undefined` without
invoking `reset_callback`. Use it to prepare a fresh attempt, not to reuse tokens.

### katcha.getKatchaToken(widgetID)

Returns the current token, or no usable token before success or after reset.
The old `getArcToken` name is not a Katcha alias.

### katcha.execute(widgetID)

Starts the workflow, normally on an invisible widget. Await its promise and
handle rejection, including dismissal and expiration. Success resolves to
`{ katcha_token, site_key }`; success callbacks still receive only a token string.

```js
try {
  const { katcha_token } = await window.katcha.execute(widgetId);
  if (katcha_token) {
    document.getElementById('protected-form').requestSubmit();
  }
} catch (error) {
  document.getElementById('captcha-status').textContent = 'Please try again.';
}
```

Here `widgetId` must be the ID returned by `render`. Do not use this pattern in a
submit handler that recursively invokes itself through `requestSubmit()`.

## Explicitly render Katcha

Use a container **without** the `.katcha` discovery class. Render only after the
script loads; an asynchronous script is not necessarily ready for adjacent code.
Replace the `.example` script host with your deployed Katcha host.

```html
<form id="protected-form" action="/protected-action" method="post">
  <div id="captcha"></div>
  <p id="captcha-status" role="status"></p>
  <button type="submit">Continue</button>
</form>
<script>
  window.initializeCaptcha = function () {
    window.protectedWidgetId = window.katcha.render('#captcha', {
      site_key: 'YOUR_SITE_KEY',
      lang: 'en',
      theme: 'light'
    });
  };
</script>
<script src="https://widget.katcha.eu/1/api.js" defer onload="initializeCaptcha()"></script>
```

This uses the script element's `onload`, not an assumed `?onload=` query contract.
Do not render the same element both explicitly and through automatic discovery.

## Error handling (client-side)

Set `data-error-callback="onCaptchaError"` and define the function before loading
the script. Provide a status element and a retry path without logging tokens.

```js
window.onCaptchaError = function ({ code, message }) {
  document.getElementById('captcha-status').textContent =
    `Challenge failed (${code}). Please try again.`;
};
```

| Legacy code | Meaning |
| --- | --- |
| `101` | Challenge creation server error |
| `102` | Challenge creation network error |
| `201` | Answer server error |
| `202` | Answer network error |
| `203` | Incorrect answer |
| `401` | Unknown-error conversion fallback; not emitted by the legacy error callback |

The legacy callback does not report all rejected executions. Handle the promise
separately for dismissal, blocked challenges, and expiration. Client errors are
not [server verification error codes](/markdown/api/verify.md#error-code-reference).

## Integration testing: test keys

**No bot protection**
The built-in test key bypasses challenge requests. Never use it on production
forms or treat its deterministic token as proof of a real challenge.

### Test key set

| Widget test value | Default |
| --- | --- |
| Site key | `0000000000` |
| Generated token | `000000000000000000000000000000` |

These widget defaults are configurable at build time and test browser integration
only. The source documentation's test secret is not verified against the Katcha
verification deployment and is not published here. Use a mocked verification
service for automated application tests, or operator-approved staging credentials
for end-to-end checks.
