> 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-parameters/pagination.md).

# Pagination

Implement faster search results using the pagination support

{% hint style="info" %}
This feature is only available for the **Search API**
{% endhint %}

## Parameters

Pagination is supported using 2 parameters: **`limit`** and **`offset`**. As their name suggests, the first one defines the number of results per request's response, and the second defines the index of the first result to fetch.

| **Name**     | Type      | Is Required? | Default value |
| ------------ | --------- | ------------ | ------------- |
| **`limit`**  | `integer` | ✖            | 10            |
| **`offset`** | `integer` | ✖            | 0             |

### `limit`

Set the number of results you want to fetch for each request.

| Minimum Value | Maximum Value |
| ------------- | ------------- |
| 0             | 1000          |

#### Notes

* If the specified **`limit`** is out of bounds, the maximum value is used.

### **`offset`**

This option allows you to set the index of the first retrieved item.

| Minimum Value | Maximum Value |
| ------------- | ------------- |
| 0             | 10 000        |

#### Notes

* **Indexes are 0-based**: **`offset=0`** will return the first item and **`offset=9`** the tenth.
* To have a proper pagination, the **`offset`** should always be a multiple of the **`limit`** parameter.

## Response

All responses from XO Search uses the same format.\
Check [API Reference](/product-discovery/resources/archived-pages/introduction/api.md) page for a more detailed description of this format.

{% content-ref url="/pages/9oz6YlEAkw7zR7txwjhG" %}
[API Reference](/product-discovery/resources/archived-pages/introduction/api.md)
{% endcontent-ref %}

## Usage example

* Fetch the 3rd page of results, with each page containing 10 elements (ie. **`offset`** is set to **20** and **`limit`** is set to **10**)

{% tabs %}
{% tab title="SDK - JS" %}
**NodeJS / NPM example**

```javascript
import { search } from '@attraqt/search';

const query = 'T-shirt';

search.init({ token: SEARCH_API_TOKEN });

const response = await search.query(query, {
  offset: 20,
  limit: 10
});

console.log(response);
```

**HTML example**

```markup
<script type="text/javascript">
    xo.init({
        search: {
            token: SEARCH_API_TOKEN
        }
    });

    xo.search.query('T-Shirt', {
        offset: 20,
        limit: 10
    }).then((response) => {
        console.log(response);
    });
</script>
```

{% endtab %}

{% tab title="API - POST" %}
**HTTP example**

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

{
  "token": "SEARCH_API_TOKEN",
  "query": "T-Shirt",
  "options": {
    "offset": 20,
    "limit": 10
  }
}
```

**`curl` example**

```bash
curl -d "{\"token\":\"${SEARCH_API_TOKEN}\", \"query\":\"T-Shirt\", \"options\":{\"offset\":20, \"limit\":10}}" \
     -H "Content-Type: application/json; charset=UTF-8"                                                         \
     -X POST "https://api-eu.attraqt.io/search"
```

**JavaScript example**

```javascript
const response = await fetch('https://api-eu.attraqt.io/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json; charset=UTF-8'
  },
  body: JSON.stringify({
    token: SEARCH_API_TOKEN,
    query: 'T-Shirt',
    options: {
      offset: 20,
      limit: 10
    }
  })
});

if (response.ok) {
  console.log(await response.json());
}
```

{% endtab %}

{% tab title="API - GET" %}
**HTTP example**

```http
GET https://api-eu.attraqt.io/search/:token?encoded=:encodedParams HTTP/1.1
```

**`curl` example**

```bash
curl "https://api-eu.attraqt.io/search/${SEARCH_API_TOKEN}?encoded=%7B%22query%22%3A%22T-Shirt%22%2C%20%22options%22%3A%7B%22offset%22%3A20%2C%22limit%22%3A10%7D%7D"
```

**JavaScript example**

```javascript
const token = SEARCH_API_TOKEN;
const params = encodeURIComponent(JSON.stringify({
  query: 'T-shirt',
  options: {
    offset: 20,
    limit: 10
  }
}));

const response = await fetch(
  `https://api-eu.attraqt.io/search/${token}?encoded=${params}`
);

if (response.ok) {
  console.log(await response.json());
}
```

{% endtab %}
{% endtabs %}
