---
schema_version: "1"
id: 419d18507cc33101c438cbbccbbf7fb99e4e7fb517497ed17b4e2e1490168725
title: prechatAPI
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/service-cloud/messaging-web/references/prechatAPI.md
canonical_url: https://developer.salesforce.com/docs/service/messaging-web/references/m4w-reference/prechatAPI.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 32f8d9477a4e77ba7f08ecce0f925e481b4acb762e84512239fe4b06b882af7c
products:
  - Enhanced Web Chat
  - Service Cloud Developer Content
---
# prechatAPI

This object contains all the APIs associated with pre-chat.

**note:** If your Enhanced Web Chat deployment is on an Experience Cloud site, follow the instructions on how to add JavaScript code to the head markup section of your site. See [Configure an Enhanced Web Chat Deployment in an Experience Builder Site](https://help.salesforce.com/s/articleView?id=service.miaw_deployment_experience_builder.htm).

## `setHiddenPrechatFields({fields})`

Sends hidden pre-chat fields to Salesforce. To learn about and set up hidden pre-chat, see [Customize Pre-Chat for Enhanced Chat](https://help.salesforce.com/s/articleView?id=service.miaw_custom_prechat_2.htm) in _Salesforce Help_. This method must be called after the [`onEmbeddedMessagingReady`](/docs/service/messaging-web/guide/event-listeners.html) event is dispatched and before the conversation begins. See [Hidden Pre-Chat](/docs/service/messaging-web/guide/pre-chat.html).

### Parameters

`{fields}`: Required. A JSON object containing a list of key-value pairs of the hidden pre-chat field names and their values.

### Example

```javascript
embeddedservice_bootstrap.prechatAPI.setHiddenPrechatFields({
  Hidden1: "value one",
  Hidden2: "value two",
  Hidden3: "value three",
  Hidden4: "value four",
});
```

## `removeHiddenPrechatFields({fields})`

Removes hidden pre-chat fields from Salesforce. This method must be called after the [`onEmbeddedMessagingReady`](/docs/service/messaging-web/guide/event-listeners.html) event is dispatched and before the conversation begins. See [Hidden Pre-Chat](/docs/service/messaging-web/guide/pre-chat.html).

### Parameters

`{fields}`: Required. An array of strings. Each string is a pre-chat field. When this method is called, the hidden pre-chat field is removed from the conversation.

### Example

```javascript
embeddedservice_bootstrap.prechatAPI.removeHiddenPrechatFields(["Hidden3"]);
```

## `setVisiblePrechatFields({fields})`

Populates visible pre-chat fields with data in the pre-chat form. The pre-chat field must already be in the pre-chat form. See [Customize Pre-Chat for Enhanced Chat](https://help.salesforce.com/s/articleView?id=service.miaw_custom_prechat_2.htm) in _Salesforce Help_. Call this method after the [`onEmbeddedMessagingReady`](/docs/service/messaging-web/guide/event-listeners.html) event is dispatched and before the conversation begins. See [Populate Pre-Chat Form Fields](/docs/service/messaging-web/guide/populate-prechat-fields.html).

### Parameter

`{fields}`: Required. A JSON object containing nested `{field}` objects. The key of each nested object is the channel variable name of a custom or standard pre-chat field.

From Setup, in the Quick Find box, enter `Messaging Settings`, and then select **Messaging Settings**. Click the name of your messaging channel. The channel variable names are listed in the Parameter Mappings section.

Standard pre-chat fields start with an underscore. They are `_firstName`, `_lastName`, `_email`, and `_subject`.

For more information on editing pre-chat fields, see [Customize Pre-Chat for Enhanced Chat](https://help.salesforce.com/s/articleView?id=service.miaw_custom_prechat_2.htm) in _Salesforce Help_.

#### `{field}`

| Parameter             | Type    | Description                                                                                                   | Required or Optional |
| --------------------- | ------- | ------------------------------------------------------------------------------------------------------------- | -------------------- |
| `value`               | String  | Required. The value to populate the field with.                                                               |
| `isEditableByEndUser` | Boolean | Indicates whether a user can edit this pre-chat field (`true`) or not (`false`). The default value is `true`. | Optional             |

### Example

Pass each pre-chat field with its value and whether it's editable in the pre-chat form.

```javascript
window.addEventListener("onEmbeddedMessagingReady", e => {
  embeddedservice_bootstrap.prechatAPI.setVisiblePrechatFields({
   "_firstName": {
      "value": "Jane",
      "isEditableByEndUser": false
    },
    "dropdown_prechat": {
      "value": "A2",
      "isEditableByEndUser": false
    },
    "checkbox_prechat": {
      "value": false,
      "isEditableByEndUser": false
    }
  });
});
```

## `unsetVisiblePrechatFields({fields})`

Removes the values from visible pre-chat fields that you populated by using `setVisiblePrechatFields`. Call this method after the [`onEmbeddedMessagingReady`](/docs/service/messaging-web/guide/event-listeners.html) event is dispatched and before the conversation begins. See [Populate Pre-Chat Form Fields](/docs/service/messaging-web/guide/populate-prechat-fields.html) in _Salesforce Help_.

### Parameters

`{fields}`: Required. An array of strings. Each string is a pre-chat field name. When this method is called, the value of each pre-chat field is removed. Use the channel variable name of your custom or standard pre-chat fields for the pre-chat field names.

From Setup, in the Quick Find box, enter `Messaging Settings`, and then select **Messaging Settings**. Click the name of your messaging channel. The channel variable names are listed in the Parameter Mappings section.

Standard pre-chat fields start with an underscore. They are `_firstName`, `_lastName`, `_email`, and `_subject`.

For more information on editing pre-chat fields, see [Customize Pre-Chat for Enhanced Chat](https://help.salesforce.com/s/articleView?id=service.miaw_custom_prechat_2.htm) in _Salesforce Help_.

### Example

```javascript
embeddedservice_bootstrap.prechatAPI.unsetVisiblePrechatFields(
    ['_firstName','dropdown_prechat']);
```
