> For the complete documentation index, see [llms.txt](https://sdk.logicdialog.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sdk.logicdialog.ai/reference/api-reference/webhookclient.md).

# WebhookClient

The WebhookClient is the entrypoint into the SDK. Here we can setup handlers that will respond to various events from the bot.

A `WebhookClient` is instantiated using the JWT Secret found in logicdialog. This is used to call the handler function.

{% tabs %}
{% tab title="Create a WebhookClient" %}

```javascript
export const whClient: WebhookClient = new WebhookClient('secret');
```

{% endtab %}
{% endtabs %}

There are two main methods on the Webhook client - `handleRequest` and `addHandler`

### handleRequest

The `handleRequest` function is called from within a code that has been setup to handle HTTP requests made to your application. This might be part of an `express` route, or some other process. The function is passed two arguments. The first of these arguments is the body of the HTTP request which has originated from logicdialog. The second argument provides the authorization token used on the incoming request. This token is used to verify the payload that has been sent has not been modified in transit.&#x20;

Examples of these function calls are provided below.&#x20;

{% tabs %}
{% tab title="Handling a Request" %}

```javascript
// This should match the endpoint set in IntelAgent
app.post('/', async (req: Request, res: Response) => {
  // handleRequest returns a JSON object and a statusCode
  const { status, body } = await whClient.handleRequest(
    req.body,
    getToken(req)
  );
  res.status(status).send(body);
});
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Fetching the Authorization token" %}

```javascript
function getToken(request: Request): string {
  const token = request.headers.authorization?.split('Bearer ')[1];
  if (!token) {
    throw Error('No token found');
  }
  return token;
}
```

{% endtab %}
{% endtabs %}

### addHandler

The `addHandler` function allows you to specify a function that can be run to process the data coming in from logicdialog. Much like event subscriptions, this handler is associated with a specific name and as such you can add multiple handlers to a webhook client so that they can handle different types of events from your bot.&#x20;

The handler that you create is an asynchronous function that accepts two parameters - a `WebhookRequest` object and a link to a `ResponseBuilder`.&#x20;

A new `WebhookHandler` is initiated as follows :&#x20;

{% tabs %}
{% tab title="Create a Webhook handler" %}

```javascript
export const divisionHandler: WebhookHandler = async (
  webhookReq: WebhookRequest,
  responseBuilder: ResponseBuilder
  ): Promise<void> => {
    // Logic goes here
}
```

{% endtab %}
{% endtabs %}

Once you have created a handler, you can add it to your webhook client using the `addHandler` method.&#x20;

{% tabs %}
{% tab title="Add the 'division-handler' hander" %}

```javascript
whClient.addHandler('division-handler', divisionHandler);
```

{% endtab %}
{% endtabs %}

In the example above, only forms in logicdialog that have a handler specified as `division-handler` will trigger the function.&#x20;

Inside the function you can find the parameters that have been set in the conversation within the `webhookReq` object. Each form will differ depending on the number and type of questions that are asked to the user however the following example shows how you might access two values of a form.&#x20;

{% tabs %}
{% tab title="Fetch Values" %}

```javascript
const {value: baseNumber} = webhookReq.formValue.baseNumber;
const {value: divisionNumber} = webhookReq.formValue.divisionNumber;
```

{% endtab %}
{% endtabs %}

The `formValues` in the example are structured as the following interface:

{% tabs %}
{% tab title="Data Structure" %}

```
{
  baseNumber: {
    title: string,
    value: string
  },
  divisionNumber: {
    title: string,
    value: string
  }
}
```

{% endtab %}
{% endtabs %}

For more information about constructing a reply to the user, please see the `ResponseBuilder` section.&#x20;

{% content-ref url="/pages/IlDf1AffmDN9Kzb09GRw" %}
[ResponseBuilder](/reference/api-reference/responsebuilder.md)
{% endcontent-ref %}
