npm package: @magento/search-sdk
Description: 📚 JavaScript library for querying the Live Search API.
Author: Magento Live Search
Latest version: 0.8.9

# Live Search SDK

📚 JavaScript wrapper around the Adobe [Live Search API][graphql].

**Note:**

**This project has been deprecated.**

**New features available in the Adobe Live Search API and Adobe Catalog Service API will not be propagated to the SDK.**

**The instructions below outline the steps to convert your requests from using the Live Search SDK to using the Live Search GraphQL API.**

## Replacing the Live Search SDK with Live Search GraphQL API:

### Initialization

**SDK: Create a client**

```javascript
import MagentoLiveSearch from "@magento/search-sdk";

const search = new MagentoLiveSearch({
    environmentId: "beb38e17-2969-46bb-b294-e140ec60c212",
    websiteCode: "base",
    storeCode: "main_website_store",
    storeViewCode: "default",
    apiKey: "search_gql",
});
```

**API: Initialize**

```javascript
const apiURL = "https://commerce.adobe.io/search/graphql";

const headers = {
    "Magento-Environment-Id": "beb38e17-2969-46bb-b294-e140ec60c212",
    "Magento-Website-Code": "base",
    "Magento-Store-Code": "main_website_store",
    "Magento-Store-View-Code": "default",
    "X-Api-Key": { your_api_key },
};
```

### Set parameters

**SDK: Pass in additional parameters**

```javascript
const params = {
    phrase: "yoga",
    filter: [{ attribute: "color", eq: "red" }],
    context: { customerGroup: "b6589fc6ab0dc82cf12099d1c2d40ab994e8410c" },
};
```

**API: Pass in additional parameters**

```javascript
const phrase = "yoga";
const filter = [{ attribute: "color", eq: "red" }];
const context = { customerGroup: "b6589fc6ab0dc82cf12099d1c2d40ab994e8410c" };
```

### Make request

**SDK: Call products function**

```javascript
search.productSearch(params);
```

**API: Make API call**

```javascript
import graphql from "./graphql";

const params = {
    phrase: phrase,
    page_size: 20,
    current_page: 1,
    sort: [],
    filter: filter,
    context: context,
};

const query = `
    query productSearch(
        $phrase: String!
        $page_size: Int
        $current_page: Int
        $filter: [SearchClauseInput!]
        $sort: [ProductSearchSortInput!]
        $context: QueryContextInput
    ) {
        productSearch(
            phrase: $phrase
            page_size: $page_size
            current_page: $current_page
            filter: $filter
            sort: $sort
            context: $context
        ) {
            total_count
            items {
                product {
                    ...Products
                }
                highlights {
                    ...Highlights
                }
            }
            facets {
                ...Facets
            }
            suggestions
            related_terms
            page_info {
                ...PageInfo
            }
        }
    }
    ${Facets}
    ${Highlights}
    ${PageInfo}
    ${Products}
`;

const response = await graphql.query<ProductSearchResponse>(
    apiURL,
    query,
    headers,
    params,
);
```

\*For `Facets`, `Highlights`, `PageInfo`, and `ProductInfo`, refer to [fragments](./src/graphql/fragments.ts)

## Table of Contents (Deprecated)

-   [Installation](#installation)
    -   [CDN](#cdn)
    -   [NPM](#npm)
-   [Usage](#usage)
    -   [Create a client](#create-a-client)
    -   [Product search](#product-search)
-   [API Reference](#api-reference)
    -   [MagentoLiveSearch](#magentolivesearch)
    -   [productSearch](#productsearch)

## Installation (Deprecated)

This SDK can be loaded from a `<script>` tag or installed with a package manager.

### CDN (Deprecated)

```html
<script src="https://search-sdk.magento-ds.com/sdk.js"></script>
```

### NPM (Deprecated)

```shell
npm install @magento/search-sdk
```

## Usage (Deprecated)

### Create a client

```javascript
import MagentoLiveSearch from "@magento/search-sdk";

const search = new MagentoLiveSearch({
    environmentId: "beb38e17-2969-46bb-b294-e140ec60c212",
    websiteCode: "base",
    storeCode: "main_website_store",
    storeViewCode: "default",
    apiKey: "search_gql",
});
```

### Product search

```javascript
search.productSearch({ phrase: "yoga" });
```

## API Reference (Deprecated)

Documentation on each of the methods exposed by the [Search SDK][npm].

### `MagentoLiveSearch`

Creates a search client for querying the [Live Search API][graphql].

Populate all fields with information specific to your [Magento account][magento] and storefront. The `apiKey` parameter should be hard coded to `search_gql` for now. More advanced API keys are coming soon.

| Parameter       | Type     | Required | Example                                |
| --------------- | -------- | :------: | -------------------------------------- |
| `environmentId` | `string` |    ✅    | `beb38e17-2969-46bb-b294-e140ec60c212` |
| `websiteCode`   | `string` |    ✅    | `base`                                 |
| `storeCode`     | `string` |    ✅    | `main_website_store`                   |
| `storeViewCode` | `string` |    ✅    | `default`                              |
| `apiKey`        | `string` |    ✅    | `search_gql`                           |

Only retrieving what is needed for your search will improve performance time.

---

### `productSearch`

Best used on a search page to retrieve products and facets for filtering.

| Parameter      | Type                        | Required | Default                                              | Example                                                       |
| -------------- | --------------------------- | :------: | ---------------------------------------------------- | ------------------------------------------------------------- |
| `phrase`       | `string`                    |    ✅    |                                                      | `yoga`                                                        |
| `page_size`    | `number`                    |          | `20`                                                 | `10`                                                          |
| `current_page` | `int`                       |          | 1                                                    | 2                                                             |
| `filter`       | `[SearchClauseInput!]`      |          | `[]`                                                 | `[{attribute: "color", eq: "red"}]`                           |
| `sort`         | `[ProductSearchSortInput!]` |          | `[]`                                                 | `[{attribute: "price", direction: "ASC"}]`                    |
| `context`      | `QueryContextInput`         |          | `[]`                                                 | `{customerGroup: "b6589fc6ab0dc82cf12099d1c2d40ab994e8410c"}` |
| `data`         | `QueryData`                 |          | `{products: true, facets: true, suggestions: false}` | `{products: true, facets: true, suggestions: true}`           |

<details>
<summary><b>Response</b></summary>

```json
{
    "extensions": {
        "request-id": "Y0g2lhSafEQfQx0Kt9ewCjHObG6yWF8r"
    },
    "data": {
        "productSearch": {
            "total_count": 58,
            "items": [
                {
                    "product": {
                        "__typename": "SimpleProduct",
                        "id": 47,
                        "uid": "47",
                        "name": "Yoga Adventure",
                        "sku": "240-LV06",
                        "description": null,
                        "short_description": null,
                        "attribute_set_id": null,
                        "meta_title": null,
                        "meta_keyword": null,
                        "meta_description": null,
                        "image": {
                            "url": "//master-7rqtwti-eragxvhtzr4am.us-4.magentosite.cloud/media/catalog/product/l/t/lt03.jpg",
                            "label": null,
                            "position": null,
                            "disabled": null
                        },
                        "small_image": null,
                        "thumbnail": null,
                        "new_from_date": null,
                        "new_to_date": null,
                        "created_at": null,
                        "updated_at": null,
                        "price_range": {
                            "minimum_price": {
                                "fixed_product_taxes": null,
                                "regular_price": {
                                    "value": null,
                                    "currency": "USD"
                                },
                                "final_price": {
                                    "value": 22,
                                    "currency": "USD"
                                },
                                "discount": null
                            },
                            "maximum_price": null
                        },
                        "gift_message_available": null,
                        "canonical_url": "//master-7rqtwti-eragxvhtzr4am.us-4.magentosite.cloud/yoga-adventure.html",
                        "media_gallery": null,
                        "custom_attributes": null
                    },
                    "highlights": [
                        {
                            "attribute": "name",
                            "value": "Yoga Adventure",
                            "matched_words": []
                        }
                    ]
                }
            ],
            "facets": [
                {
                    "title": "Price zz",
                    "attribute": "price",
                    "buckets": [
                        {
                            "__typename": "ScalarBucket",
                            "title": "32.0",
                            "id": "32.0",
                            "count": 5
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "29.0",
                            "id": "29.0",
                            "count": 4
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "45.0",
                            "id": "45.0",
                            "count": 4
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "0.0",
                            "id": "0.0",
                            "count": 3
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "24.0",
                            "id": "24.0",
                            "count": 3
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "39.0",
                            "id": "39.0",
                            "count": 3
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "19.0",
                            "id": "19.0",
                            "count": 2
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "28.0",
                            "id": "28.0",
                            "count": 2
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "38.4",
                            "id": "38.4",
                            "count": 2
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "42.0",
                            "id": "42.0",
                            "count": 2
                        },
                        {
                            "__typename": "StatsBucket",
                            "title": "",
                            "min": 0,
                            "max": 92
                        }
                    ]
                },
                {
                    "title": "Color",
                    "attribute": "color",
                    "buckets": [
                        {
                            "__typename": "ScalarBucket",
                            "title": "Blue",
                            "id": "Blue",
                            "count": 22
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Black",
                            "id": "Black",
                            "count": 17
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Red",
                            "id": "Red",
                            "count": 13
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Green",
                            "id": "Green",
                            "count": 12
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Purple",
                            "id": "Purple",
                            "count": 11
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Gray",
                            "id": "Gray",
                            "count": 9
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Orange",
                            "id": "Orange",
                            "count": 8
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "White",
                            "id": "White",
                            "count": 6
                        }
                    ]
                },
                {
                    "title": "Categories",
                    "attribute": "categories",
                    "buckets": [
                        {
                            "__typename": "ScalarBucket",
                            "title": "collections",
                            "id": "collections",
                            "count": 41
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "collections/yoga-new",
                            "id": "collections/yoga-new",
                            "count": 32
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "women",
                            "id": "women",
                            "count": 23
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "gear",
                            "id": "gear",
                            "count": 20
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "women/tops-women",
                            "id": "women/tops-women",
                            "count": 17
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "promotions",
                            "id": "promotions",
                            "count": 15
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "men",
                            "id": "men",
                            "count": 11
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "men/bottoms-men",
                            "id": "men/bottoms-men",
                            "count": 11
                        }
                    ]
                },
                {
                    "title": "Activity",
                    "attribute": "activity",
                    "buckets": [
                        {
                            "__typename": "ScalarBucket",
                            "title": "Gym",
                            "id": "Gym",
                            "count": 16
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Yoga",
                            "id": "Yoga",
                            "count": 15
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Athletic",
                            "id": "Athletic",
                            "count": 9
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Sports",
                            "id": "Sports",
                            "count": 6
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Recreation",
                            "id": "Recreation",
                            "count": 4
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "School",
                            "id": "School",
                            "count": 4
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Outdoor",
                            "id": "Outdoor",
                            "count": 3
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Travel",
                            "id": "Travel",
                            "count": 3
                        }
                    ]
                },
                {
                    "title": "Material",
                    "attribute": "material",
                    "buckets": [
                        {
                            "__typename": "ScalarBucket",
                            "title": "Polyester",
                            "id": "Polyester",
                            "count": 23
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Nylon",
                            "id": "Nylon",
                            "count": 15
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Spandex",
                            "id": "Spandex",
                            "count": 13
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "CoolTech™",
                            "id": "CoolTech™",
                            "count": 9
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Cotton",
                            "id": "Cotton",
                            "count": 8
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Organic Cotton",
                            "id": "Organic Cotton",
                            "count": 8
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Rayon",
                            "id": "Rayon",
                            "count": 7
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "Wool",
                            "id": "Wool",
                            "count": 7
                        }
                    ]
                },
                {
                    "title": "Size",
                    "attribute": "size",
                    "buckets": [
                        {
                            "__typename": "ScalarBucket",
                            "title": "L",
                            "id": "L",
                            "count": 17
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "M",
                            "id": "M",
                            "count": 17
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "S",
                            "id": "S",
                            "count": 17
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "XL",
                            "id": "XL",
                            "count": 16
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "XS",
                            "id": "XS",
                            "count": 16
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "32",
                            "id": "32",
                            "count": 14
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "33",
                            "id": "33",
                            "count": 11
                        },
                        {
                            "__typename": "ScalarBucket",
                            "title": "34",
                            "id": "34",
                            "count": 11
                        }
                    ]
                }
            ],
            "suggestions": [
                "yoga",
                "yoga adventure",
                "yoga bag",
                "yoga brick",
                "yoga companion kit"
            ],
            "related_terms": null,
            "page_info": {
                "current_page": 1,
                "page_size": 1,
                "total_pages": 58
            }
        }
    }
}
```

</details>