migros-api-wrapper icon indicating copy to clipboard operation
migros-api-wrapper copied to clipboard

How to display the searched product details?

Open chillbert opened this issue 2 years ago • 3 comments

I am looking for specific products and then iterate over their ids to get their details:

import { MigrosAPI } from "migros-api-wrapper";

main();
async function main() {
    const guestInfo = await MigrosAPI.account.oauth2.getGuestToken();
    const responseProductSearch = await MigrosAPI.products.productSearch.searchProduct({
        query: "kochsalz",
        language: "de"
    },
    { leshopch: guestInfo.token })
    console.log(responseProductSearch);
    const productIds = responseProductSearch.productIds;

// Function to get product details by ID
async function getProductDetails(productId) {
    console.log("ID",productId)
    try {
        const productDetails = await MigrosAPI.products.productDisplay.getProductDetails({ id: productId }, { leshopch: guestInfo.token });
        return productDetails;
    } catch (error) {
        console.error(`Error fetching details for Product ID: ${productId}`, error.message);
    }
}

// Loop through product IDs and fetch details
for (const productId of productIds) {
    const productDetails = await getProductDetails(productId);
    if (productDetails) {
        console.log(`Product Details: ${JSON.stringify(productDetails)}`);
    } else {
        console.log(`Failed to fetch details for Product ID: ${productId}`);
    }
}
}

Here the products found with their productIds:

{
    "productIds": [
        100001090,
        100006775,
        100047495,
        100047814
    ],
    "sponsoredProducts": [],
    "categories": [
        {
            "id": 7494735,
            "name": "Pasta, Würzmittel & Konserven",
            "numberOfProducts": 4,
            "sortPriority": 5,
            "parentSortPriority": 1,
            "path": "/DS/Pastacondimentscannedfood",
            "type": "MAIN",
            "slug": "pasta-wurzmittel-konserven",
            "addAllItemsToBasket": false,
            "level": 2
        }
    ],
    "features": [
        {
            "id": "category",
            "name": "Kategorien",
            "type": "STRING",
            "slug": "category",
            "numberOfProducts": 4,
            "values": [
                {
                    "value": "Pasta, Würzmittel & Konserven",
                    "slug": "7494735",
                    "numberOfProducts": 4
                }
            ]
        },
        {
            "id": "brand",
            "name": "Marke",
            "type": "STRING",
            "slug": "brand",
            "numberOfProducts": 4,
            "values": [
                {
                    "value": "M-Classic",
                    "slug": "m-classic",
                    "numberOfProducts": 3
                },
                {
                    "value": "Jura-Sel",
                    "slug": "jura-sel",
                    "numberOfProducts": 1
                }
            ]
        },
        {
            "id": "CATEGORY_LEVEL_4",
            "name": "category_level_4",
            "type": "SPLITTED",
            "slug": "category-level-4",
            "numberOfProducts": 4,
            "values": [
                {
                    "value": "Salz",
                    "slug": "salz",
                    "numberOfProducts": 2
                },
                {
                    "value": "Anderer Fisch",
                    "slug": "anderer-fisch",
                    "numberOfProducts": 1
                },
                {
                    "value": "Essig & Balsamico",
                    "slug": "essig-balsamico",
                    "numberOfProducts": 1
                }
            ]
        }
    ],
    "labels": [
        {
            "id": "PRODUCT_OF_SWITZERLAND",
            "name": "Schweizer Produkt",
            "slug": "product_of_switzerland",
            "numberOfProducts": 1
        },
        {
            "id": "DIETARY_BOOLEAN_FILTER_VEGAN",
            "name": "Vegan",
            "slug": "vegan",
            "numberOfProducts": 2
        }
    ],
    "groupedLabels": [
        {
            "id": "PRODUCT_SPECIAL_NUTRITION",
            "name": "Spezielle Ernährung",
            "slug": "product_special_nutrition",
            "values": [
                {
                    "id": "vegan",
                    "name": "Vegan",
                    "slug": "vegan",
                    "numberOfProducts": 2
                }
            ]
        }
    ],
    "migrosLabels": [
        {
            "id": "L57",
            "value": {
                "en": "V-Label vegan",
                "de": "V-Label vegan",
                "fr": "V-Label vegan",
                "it": "V-Label vegan"
            },
            "slug": {
                "en": "v-label-vegan",
                "de": "v-label-vegan",
                "fr": "v-label-vegan",
                "it": "v-label-vegan"
            },
            "numberOfProducts": 1
        }
    ],
    "additionalFields": {},
    "numberOfProducts": 4,
    "offset": 4
}

now how do I iterate over these Ids? 100001090, 100006775, 100047495, 100047814

Since I get a bad request error:

ID 100001090
Product ID: {"timestamp":"2024-09-02T10:13:40.274+00:00","status":400,"error":"Bad Request","path":"/product-display/public/v2/product-detail"}
ID 100006775
Product ID: {"timestamp":"2024-09-02T10:13:40.320+00:00","status":400,"error":"Bad Request","path":"/product-display/public/v2/product-detail"}
ID 100047495
Product ID: {"timestamp":"2024-09-02T10:13:40.370+00:00","status":400,"error":"Bad Request","path":"/product-display/public/v2/product-detail"}
ID 100047814
Product ID: {"timestamp":"2024-09-02T10:13:40.400+00:00","status":400,"error":"Bad Request","path":"/product-display/public/v2/product-detail"}

chillbert avatar Sep 02 '24 10:09 chillbert

    const productCardsOptions: IProductCardsOptions = {
      uids: "100001090" // ["XXXXXXX", "YYYYYYY"]
    };
    const response = await MigrosAPI.products.productDisplay.getProductDetails(
      productCardsOptions,
      { leshopch: guestInfo.token }
    );

also you can send an array of uids so you don't spam their api.

aliyss avatar Sep 02 '24 15:09 aliyss

may I call you my hero? :D thx it worked.

Is the API an official one? Anyone can use also for commercial apps? what are the limitations?

And are you aware of similar APIs for Lidl, Aldi, Coop and co.? thanks again <3

chillbert avatar Sep 04 '24 12:09 chillbert

No. Yes. Imagination. Only my private repos. Thank you ^^

aliyss avatar Sep 07 '24 04:09 aliyss