> For the complete documentation index, see [llms.txt](https://fredhopper.gitbook.io/product-discovery/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fredhopper.gitbook.io/product-discovery/resources/archived-pages/introduction/api/search-api-1.md).

# Autocomplete API (Beta)

Autocomplete API allows you to display queries suggestions, based on the user query. This allows the user to refine its search request, with queries that yield results in the past.

Suggested queries are based on previous queries typed by users that returned results, over the last week (default time span). By default, suggestions are ordered by the number of search views.

{% hint style="info" %}
**Prerequisites to enable autocomplete**

* Having a working XO Search implementation
* Having tracking enabled
* Having XO search tracking implemented, as documented in the XO tracking plan:

[https://github.com/Attraqt/product-discovery-documentation/blob/main/new-docs/Archive/archived-pages/xo.md](https://github.com/Attraqt/product-discovery-documentation/blob/main/new-docs/Archive/archived-pages/xo.md "mention")

* **Contact us to enable autocomplete.**
  {% endhint %}

## Autocomplete

<mark style="color:green;">`POST`</mark> `https://api-eu.attraqt.io/search/autocomplete`

Autocomplete the user's queries and get suggestions using the XO Search engine.

#### Request Body

| Name                                    | Type    | Description                                                                                                  |
| --------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------ |
| token<mark style="color:red;">\*</mark> | string  | XO Search token (can be found in the XO Console)                                                             |
| query<mark style="color:red;">\*</mark> | string  | The query string used for the search                                                                         |
| options.numberOfSuggestions             | integer | <p>Number of suggestions to get, at most.</p><p><em>Defaults to 10</em></p><p>(must be between 1 and 20)</p> |
| configurationId                         | string  | Configuration to use. If unspecified, the default configuration will be used                                 |

#### Example autocomplete request

```http
POST https://api-eu.attraqt.io/search/autocomplete HTTP/1.1
Content-Type: application/json; charset=UTF-8

{
  "token": "SEARCH_API_TOKEN",
  "query": "T-Shirt",
  "options": {
    "numberOfSuggestions": 10
  }
}
```

## Response

The search response contains two parts:

| Property          | Type     | Description                                                |
| ----------------- | -------- | ---------------------------------------------------------- |
| **`suggestions`** | `array`  | Array of suggestions. See below for the exact format       |
| **`metadata`**    | `object` | Metadata about the autocomplete request: elapsed time, ... |

### **`suggestions`**

The suggestions based on the input query

| Property         | Type     | Description                               |
| ---------------- | -------- | ----------------------------------------- |
| **`suggestion`** | `string` | A suggestion, prefixed by the input query |

### **`metadata`**

Metadata about the search query and its results.

| Property              | Type     | Description                                         |
| --------------------- | -------- | --------------------------------------------------- |
| **`time`**            | `number` | Time the request took to complete (in milliseconds) |
| **`token`**           | `string` | Search API token used for the request               |
| **`id`**              | `string` | Unique identifier                                   |
| **`configurationId`** | `string` | Search configuration id used                        |

### Example

{% tabs %}
{% tab title="JSON Format" %}

```javascript
{
  "suggestions": [
    { "suggestion": "t-shirt black" },
    { "suggestion": "t-shirt for kids" },
    { "suggestion": "t-shirt on sale" }
  ],
  "metadata": {
    "id": "autocomplete_573e30b1-7d97-46a0-bbe0-110527150534",
    "configurationId": "cddda87b-c99d-45aa-bf45-cd53d2bc8ace",
    "time": 35,
    "token": "MY_SEARCH_TOKEN"
  }
}
```

{% endtab %}
{% endtabs %}
