# Integration with third-party APIs

You can create bots that interact with other services via APIs. For example, bots can make requests to check availability for a specific date or to find a product in an online store.

## How to interact with third-party APIs using Dadata as an example

Let's take an address suggestion API as an example: <https://dadata.ru/api/suggest/address/> <mark style="color:red;">**ссылка будет другая**</mark>

{% hint style="info" %}
Read the documentation of third-party APIs carefully.
{% endhint %}

{% hint style="warning" %}
Please note! Third-party APIs require the IP addresses from which requests will be sent: 158.160.49.208 – website.
{% endhint %}

In the "Request URL" field, enter `https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address`.

In the **"Request header"** field, enter the header in JSON format: `{"Content-Type": "application/json", "Accept": "application/json", "Authorization": "Token " + token}`.

In the **"JSON parameters"** field, enter the request itself: `{"query": "#{CurrencyType}"}`.

To get the request result, fill in the **"Saved values"** field.

An example of the request is shown in the illustration below.

![](/files/z10eZ4td3q3L0jgAJOps)

### How to save values from variables

It might not be completely clear the first time—you really need to try it yourself.

First, enter #{custom\_answer} in the "Answer" field. (The variable #{custom\_answer} contains the response received from the server specified in the "Response URL" field.)

Run the request block to send the request and get the response. The received response needs to be analyzed, and you need to set up which variables to extract from it. For this, use the "Saved Values" field.

Let's go through the example described above.

The best way to understand how to address data in JSON is by example. As a result of our request, the response received in #{custom\_answer} looks like this:

`{"suggestions": [{"value": "US Dollar","unrestricted_value": "US Dollar","data": {"code": "840","strcode": "USD","name": "US Dollar","country": "United States"}}]}`

In the "Saved values" field, enter VALUE -> VARIABLE:

`suggestions|0|value->CurrencyType;` \
`suggestions|0|data|code->CurrencyDigCode;` \
`suggestions|0|data|strcode->CurrencyStrCode;` \
`suggestions|0|data|country->CurrencyCountry`

**suggestions -** key for the array \[{"value": "US Dollar", "unrestricted\_value": "US Dollar", "data": {"code": "840", "strcode": "USD", "name": "US Dollar", "country": "United States"}}]

**suggestions|0** - key for the first element of the array {"value": "US Dollar", "unrestricted\_value": "US Dollar", "data": {"code": "840", "strcode": "USD", "name": "US Dollar", "country": "United States"}}

**suggestions|0|value** - key for the value "US Dollar"

The longest key in this JSON:

**suggestions|0|data|strcode** - key for the value "USD"

{% hint style="success" %}
Keys are separated by a vertical bar (|). If a JSON element is an array, you access its items by index, starting from 0, and this is also written using a vertical bar.
{% endhint %}

{% hint style="info" %}
You need to repeat the entire nesting. For convenience, you can visualize the JSON using a JSON viewer application.
{% endhint %}

The nesting level is separated by a vertical bar. That is, you need to list all the keys to reach the value, separating them with a vertical bar.

{% hint style="info" %}
If the JSON contains an array, use the element’s index instead of a key, starting from zero.
{% endhint %}

Separate the expressions for obtaining our variables with a semicolon ;.

Each expression consists of the path to the data in the response and the name of the variable to save it to, separated by ->.

The search query represents the path in the JSON to the required value.

### API development for bots

The bot supports GET and POST requests, which are the main types used in most public APIs.

**GET** is a request that happens when you click a link or open a URL in a browser. Parameters in this request are passed in the URL.

Syntax for passing parameters: after the URL, place a question mark ?, then separate each parameter with an ampersand &. Each parameter consists of a name followed by = and its value.

Example of a URL with a parameter:

![Fig. 3](/files/-M1FvTT-n5HTgbuO8j1V)

Here, a single parameter q is passed with the value "good music". %20 represents a space.

When working with the builder, you can just use a space—the bot will replace it automatically.

**POST** is a request most often sent when submitting a form on a website. The parameters are sent in the body of the request. While they can also be sent in the URL like in GET requests, this is uncommon. Parameters in the body can be in JSON format or as key-value pairs. Forms usually send key-value pairs, while APIs most often use JSON.

**Request header** - you can add a header to each request. Most often, it specifies the data format and access keys. Usually, this field is left empty, but in rare cases, you need to include an API token or a request type, e.g., "Content-Type": "application/json".

**JSON** (JavaScript Object Notation, usually pronounced /ˈdʒeɪsən/ JAY-sən) is a text-based data exchange format based on JavaScript. Like many other text formats, JSON is easy for humans to read. To work with APIs in the bot, you need to understand this format, as it underlies all interactions.

Before continuing, read the following articles:

<https://www.json.org/json-ru.html>\
<https://developer.mozilla.org/ru/docs/Learn/JavaScript/%D0%9E%D0%B1%D1%8A%D0%B5%D0%BA%D1%82%D1%8B/JSON>

Also, try creating your own JSON file on this website:

<https://jsoneditoronline.org/>

**Practice**&#x20;

Requests can be executed by blocks and conditions. Conditional requests are designed to check whether a dialog can follow a certain branch on the site. In this case, the request is sent every time the branch condition is evaluated. Requests in blocks, on the other hand, are executed only when the dialog enters that state.

Before creating a request, you need to choose its type:

![](/files/YOZwnYaOJgGI7ZwPGwTu)

POST-data and POST-JSON differ in how parameters are sent, as mentioned earlier (either in JSON format or as key-value pairs). If you choose JSON, parameters can only be sent in the request body. An additional field will appear for JSON POST parameters.

**Important!** Parameters must be written in JSON format only, as the bot works exclusively with it. You have already practiced writing JSON.

The response from the server can be parsed and saved into variables.&#x20;

{% hint style="info" %}
You can parse a response only if it is in JSON format.
{% endhint %}

Saved variables are recorded in the last unfilled form. If the form is submitted through a red block, the variables are discarded. Therefore, if you need to save this data, pass the variables to the CRM system through a yellow block.

You can also see what the bot has recorded in the variables in the "Leads" section.

![Fig. 8](/files/-M1FywXYZbbfG0b8GB7D)

### Passing variables not as a string

By default, variable values must be strings in the format "#{}", but in this case, the variable is sent as a string. To pass a variable as a number, you need to disable the parameter format check in the project settings:

<figure><img src="/files/yn2ZWh7ESdGo8Y3TyP89" alt=""><figcaption></figcaption></figure>

Then you can simply use the variable name, for example: `{"key": #{variable_name}}`, where variable\_name is the variable itself—without surrounding quotes.

{% hint style="warning" %}
It is recommended to enable the format check again after setting the parameters.
{% endhint %}


---

# 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://docs.mavibot.ai/working-with-api/integration-with-third-party-apis.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.
