# Functions

### `FTCaptures.init(...)`

This function initializes the configuration for a specific route in a given environment. Here is an explanation of how the function works:

The function takes three parameters:&#x20;

1. `routeID` (the ID of the route)
2. `environment` (the environment in which the route is being initialized)
3. `callback` (a function to be executed after the initialization is complete)
4. `tags` (an array of  tags to identify the permanent token). *Optional*\
   \&#xNAN;*(Only is utilized when the inbound route has been set up with a permanent token)*.
5. `jwtToken` (a JWT token) - Optional\
   \&#xNAN;*Note: Only applicable in version 2.3*

<pre><code><strong>const FT = window.FTCaptures.init("&#x3C;ROUTE_ID>", "production", function (state) { }, tags, jwtToken);
</strong></code></pre>

### `FTCaptures.on(...)`

This function provides a convenient way to register multiple event handlers for different events and allows for easy execution of the registered callbacks when the corresponding events occur.

You can use the `on` function to subscribe to the following events:&#x20;

{% tabs %}
{% tab title="For Validation Failed" %}

* cardHolderValidationFailed
* cardNumberValidationFailed
* cardExpirationDateValidationFailed
* cardSecurityCodeValidationFailed
* expirationYearValidationFailed
* expirationMonthValidationFailed
* textValidationFailed
* AmountValidationFailed
* selectValidationFailed
* radioValidationFailed
  {% endtab %}

{% tab title="For Validation Success" %}

* cardHolderValidationSuccess
* cardNumberValidationSuccess
* cardExpirationDateValidationSuccess
* cardSecurityCodeValidationSuccess
* expirationYearValidationSuccess
* expirationMonthValidationSuccess
* textValidationSuccess
* AmountValidationSuccess
* selectValidationSuccess
* radioValidationSuccess
  {% endtab %}
  {% endtabs %}

Here's an example of how you can subscribe to an event:

```
FTCaptures.on("cardNumberValidationFailed", function (event) {
    //Do something with the event
    
});
```

### `FT.field(...)`

Field function creates a field within a specified container using an `<iframe>`. Here is an explanation of how the function works:

1. The function takes two parameters:&#x20;
   * `selector` (the id selector for the container element)&#x20;
   * `options` (an object containing configuration options for the field).
2. It checks if the `routeID` property is set. If not, it throws an error indicating that the SDK must be initialized before creating fields.

### `FTCaptures.tokenize(...)`

Tokenize function prepares the fields within a specified route for tokenization. Here is an explanation of how the function works:

1. The function takes one parameter: `routeID` (the ID of the route).
2. It sets the `routeID` property of the current object (`this`) to the provided `routeID`.
3. The function returns a new Promise that takes two parameters: `resolve` and `reject`. These parameters are used to handle the asynchronous result of the tokenization process.

### `FTCaptures.validate(...)`

The `validate` function performs field validation for a specified route. Here is an explanation of how the function works:

1. The function takes one parameter: `routeID` (the ID of the route).
2. The function returns a new Promise that takes two parameters: `resolve` and `reject`. These parameters are used to handle the asynchronous result of the validation process.
3. An object `validationResults` is created to store the validation results for each field.

Overall, this function performs field validation for a specified route by sending messages to the content windows of the associated `<iframe>` elements and listening for validation results. The Promise is resolved with the validation results when all fields have been validated.

### `FTCaptures.destroy(...)`

This function removes the fields associated with a specified route and destroys any active instances. The function takes one parameter: `routeID` (the ID of the route).

Overall, this function removes the `<iframe>` elements associated with a specified route from the DOM and destroys any active instances associated with that route.


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://firstoken.gitbook.io/api-docs/guides/implementing-captures-sdk-js/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
