# JavaScript API

### API

From the customized API JavaScript, you can call some API functions to control the behavior of the Assistant. All these functions have to be called from the Extension object, for example:

```javascript
var selectedOptionID = Extension.getSelectionId(3, history);
```

The available API functions for public use are:

**getSelectionId(slideId, history)**

Returns the ID of the selected slide answer. Only for single selection slides.

**getSlide(slideId, slides)**

Returns the slide with the indicated ID.

**getOptionId(slideId, index, slides)**

Returns the answer ID from its answer index (0..n).

**getFormResponse(slideId, slides, fieldId, history)**

Returns the user field response from field ID.

**isSelectedOption(optionId, slides, history)**

Returns true if the optionId was selected.

**getSelectionIndex(slideId, history, slides)**

Returns the index of the slide selected answer (0..n).

**setFinalizer(extension, fn)**

Code that will be executed when the slide is destroyed.

**setHook(extension, id, fn)**

Code that will be executed before the hook with that ID is executed.

**setGlobalValue(id, value)**

Sets a global value that can be used to generate customized emails.

**getGlobalValue(id)**

Gets a global value previously set though setSessionValue.

**setSessionValue(id, value)**

Sets a value in memory that can be shared between slides. It’s used for programmatic control.

**getSessionValue(id)**

Gets a value previously set though setSessionValue.

**getRequest(url, callback)**

Makes a GET request. The callback function will be executed passing the response as a parameter.

**postRequest(url, contentType, data, callback)**

Makes a POST request. The callback function will be executed passing the response as a parameter.

Valid contentTypes: json, form, text.

**getCurrentSlideState()**

Gets the current slide state. For example, to get a value from within a defined hook.

**contains(item,list)**

Returns true if the item is in the list.

**containsAny(list1, list2)**

Returns true if any alement of list1 is in list2.

**containsAll(list1, list2)**

Returns true if all elements of list1 are in list2.

**getParameterByName(name, url)**

Returns the specified query parameter from the URL.

**getByKey(list, key, value)**

Finds an element in the list whose key value is specified.

**getIndexByKey(list, key, value)**

Returns the index of the element in the list whose key value is specified.

**executeJS(fn, callback, async, params)**

Executes a JavaScript function in the iframe holder page.

* code: Name of the JavaScript function
* callback: Function to be executed when the call is completed. This parameter is only needed in the case of an asynchronous call.
* async: true if the call is asynchronous, false otherwise.
* params: Array of parameters to be passed to the function.

{% hint style="info" %}
Some API functions need special parameters such as extension, slides, or history. These variables are available in the context and can be used to call the API.
{% endhint %}

### Injected variables

Pickzen will automatically inject some variables that can be used in the JavaScript sections to customize the functionality of each slide:

* **slide**: It will include the current slide definition: title, options, etc.
* **slides**: It will contain an array with all slides' definitions.
* **history**: It will contain the previous slide responses so far.

<br>


---

# 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://help.pickzen.com/developers/api.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.
