> For the complete documentation index, see [llms.txt](https://docs.caf.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.caf.io/caf-sdk/web-javascript/getting-started/document-detector/sdk-event-listeners.md). Agents can also search and read this documentation through its [MCP server](https://docs.caf.io/~gitbook/mcp).

# Event listeners

The SDK provides various event listeners to handle different events during the document capture process. These listeners allow you to execute custom logic when specific events occur.

## addEventListener <a href="#sr1l558mx177" id="sr1l558mx177"></a>

The `addEventListener` method is used to listen to events of the specified `type`.

```javascript
documentDetector.addEventListener(type, listener, options);
```

**Parameters**

* `type` ([EventType](#66n5usmgj32o)): The type of event to listen for (check [EventType](#66n5usmgj32o) for details).
* `listener` (function): The function to be executed when the event is triggered.
* `options` (object, optional): Additional options for the event listener

## removeEventListener <a href="#id-2umdj3vdpe3t" id="id-2umdj3vdpe3t"></a>

The `removeEventListener` method is used to remove an event listener of the specified `type`.

```javascript
documentDetector.removeEventListener(type, callback, options);
```

**Parameters**

* `type` ([EventType](#66n5usmgj32o)): The type of event to listen for (check [EventType](#66n5usmgj32o) for details).
* `callback` (function): The function to be removed.
* `options` (object, optional): Additional options for the event listener

## EventType <a href="#id-66n5usmgj32o" id="id-66n5usmgj32o"></a>

<table data-header-hidden><thead><tr><th></th><th width="468"></th></tr></thead><tbody><tr><td><strong>Type</strong></td><td><strong>Description</strong></td></tr><tr><td><strong><code>"front_capture_started"</code></strong></td><td>Triggered when the capture of the front of the document has started.</td></tr><tr><td><strong><code>"back_capture_started"</code></strong></td><td>Triggered when the capture of the back of the document has started.</td></tr><tr><td><strong><code>"capture_invalid"</code></strong></td><td>Triggered when an error occurs in some validation performed by the SDK.</td></tr><tr><td><strong><code>"capture_failed"</code></strong></td><td>Triggered when an error occurs that is not handled by the SDK.</td></tr><tr><td><strong><code>"capture_ready"</code></strong></td><td><p>Triggered when the SDK capture screen (camera preview or upload UI) is rendered and ready for user interaction.</p><p>Event detail: <code>mode</code>, <code>expectedDocumentType</code>, <code>expectedDocumentSide</code>.</p></td></tr><tr><td><strong><code>"upload_button_clicked"</code></strong></td><td>Triggered when the user clicks the upload button in upload mode.</td></tr><tr><td><strong><code>"capture_result"</code></strong></td><td><p>Triggered after a document is successfully captured and validated.</p><p>Event detail: <code>mode</code>, <code>isValid</code>, <code>expectedDocumentType</code>, <code>expectedDocumentSide</code>.</p></td></tr></tbody></table>

## Example <a href="#h196wa5m346d" id="h196wa5m346d"></a>

```javascript
// Listen for the event when the capture of the front of the document starts
documentDetector.addEventListener('front_capture_started', () => {
    console.debug('Document front capture event fired');
});

// Listen for the event when the capture of the back of the document starts
documentDetector.addEventListener('back_capture_started', () => {
    console.debug('Document back capture event fired');
});

// Listen for the event when the SDK UI is ready for user interaction
documentDetector.addEventListener('capture_ready', (event) => {
    console.debug('SDK is ready for capture', {
        mode: event.detail?.mode,
        expectedDocumentType: event.detail?.expectedDocumentType,
        expectedDocumentSide: event.detail?.expectedDocumentSide,
    });
});

// Listen for the event when the user clicks the upload button
documentDetector.addEventListener('upload_button_clicked', () => {
    console.debug('Upload button clicked');
});

// Listen for the event when a capture result is available
documentDetector.addEventListener('capture_result', (event) => {
    console.debug('Capture result', {
        mode: event.detail?.mode,
        isValid: event.detail?.isValid,
        expectedDocumentType: event.detail?.expectedDocumentType,
        expectedDocumentSide: event.detail?.expectedDocumentSide,
    });
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.caf.io/caf-sdk/web-javascript/getting-started/document-detector/sdk-event-listeners.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
