@magento/search-sdk

📚 JavaScript library for querying the Live Search API.

Usage no npm install needed!

<script type="module">
  import magentoSearchSdk from 'https://cdn.skypack.dev/@magento/search-sdk';
</script>

README

Live Search SDK

📚 JavaScript library for querying Magento's Live Search API.

Table of Contents

Installation

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

CDN

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

NPM

npm install @magento/search-sdk

Usage

Create a client

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

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

API Reference

Documentation on each of the methods exposed by the Search SDK.

MagentoLiveSearch

Creates a search client for querying the Live Search API.

Populate all fields with information specific to your Magento account 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

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 string 1 2
filter [] [] [{attribute: "color", eq: "red"}]
sort [] [] [{attribute: "price", direction: "ASC"}]
Response
{
    "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
            }
        }
    }