pythonpython-3.xamazon-selling-partner-api

Python - why is Amazon SP-API responding with "invalid marketplaceIds provided" when patching package dimensions?


I'm working on integrating the Amazon SP API to update item dimensions for a specific SKU. However, I'm encountering an error that states "invalid marketplaceIds provided".

I have verified that I'm passing the correct marketplace ID, but the error persists. Below is the minimal code I'm using to reproduce the issue:

from sp_api import ListingItems
from sp_api.base.marketplaces import Marketplaces
from sp_api.base import SellingApiException

class AmazonProductManager:
    def __init__(self, credentials: AmazonAuth, marketplace: Marketplaces = Marketplaces.DE):
        self.creds = credentials
        self.credentials = dict(
            refresh_token=self.creds.refresh_token,
            lwa_app_id=self.creds.lwa_app_id,
            lwa_client_secret=self.creds.lwa_client_secret,
            aws_secret_key=self.creds.aws_secret_key,
            aws_access_key=self.creds.aws_access_key,
            role_arn=self.creds.role_arn
        )
        self.marketplace = marketplace

    def update_item_dimensions(self, seller_id: str, sku: str,
                               item_dimensions: dict = None,
                               package_dimensions: dict = None):
        """Update item dimensions."""
        try:
            payload = {
                "productType": "PRODUCT",
                "requirements": "LISTING",
                "attributes": {
                    # Dimensions are added here
                }
            }

            if isinstance(item_dimensions, dict):
                if item_dimensions:
                    payload['attributes']['item_dimensions'] = item_dimensions

            if isinstance(package_dimensions, dict):
                if package_dimensions:
                    payload['attributes']['package_dimensions'] = package_dimensions

            response = ListingsItems(credentials=self.credentials,
                                     marketplace=self.marketplace).put_listings_item(
                sellerId=seller_id, sku=sku, body=payload)

            return response.payload
        except SellingApiException as e:
            print(f"Error updating item dimensions: {e}")
            return None

Details:

Request_body:

{'productType': 'PRODUCT',
 'requirements': 'LISTING',
 'MarketplaceId': 'A1PA6795UKMFR9',
 'MarketplaceIds': ['A1PA6795UKMFR9'],
 'marketplaceIds': ['A1PA6795UKMFR9'],
 'marketplace_ids': ['A1PA6795UKMFR9'],
 'attributes': {'package_dimensions': {'height': '19',
                                       'length': '108',
                                       'unit': 'CM',
                                       'weight': '28',
                                       'weight_unit': 'KG',
                                       'width': '67'}}
}

Response: Error updating item dimensions: [{'code': 'InvalidInput', 'message': "Invalid 'marketplaceIds' provided.", 'details': ''}]

Has anyone encountered a similar issue? What could be causing this error, and how can I resolve it? Any help would be greatly appreciated!


Solution

  • Just in case anyone stombles along this question in the future: I have found the answer!

    A couple of things were wrong in the request:

    1. marketplaceIds must be present in the patch_listings_item call.
    2. the call should actually be patch_listing_item and not put_listing_item, which was the case in my initial approach.
    3. Perhaps the most important of all the payload needs to be correct.
    4. If item dimensions or package dimensions return NULL when calling CatalogItems().get_catalog_item(asin=asin) op needs to be "add" otherwise "replace"

    The code below is tested and works as of the time of writing this answer.

    from sp_api import ListingItems
    from sp_api.base.marketplaces import Marketplaces
    from sp_api.base import SellingApiException
    
    class AmazonProductManager:
        def __init__(self, credentials: AmazonAuth, marketplace: Marketplaces = Marketplaces.DE):
            self.creds = credentials
            self.credentials = dict(
                refresh_token=self.creds.refresh_token,
                lwa_app_id=self.creds.lwa_app_id,
                lwa_client_secret=self.creds.lwa_client_secret,
                aws_secret_key=self.creds.aws_secret_key,
                aws_access_key=self.creds.aws_access_key,
                role_arn=self.creds.role_arn
            )
            self.marketplace = marketplace
    
        def update_item_dimensions(self, seller_id: str, sku: str,
                                   item_dimensions: dict = None,
                                   package_dimensions: dict = None):
            """Update item dimensions."""
            try:
                payload = {
                    "productType": "PRODUCT",
                    "requirements": "LISTING_PRODUCT_ONLY",
                    "identifiers": asin,
                    "patches": [
                        {
                            "op": "replace",
                            "path": "/attributes/item_dimensions",
                            "values": [
                                {
                                    "marketplace_id": self.marketplace.marketplace_id,
                                    "height": {
                                        "unit": "centimeters",
                                        "value": float(item_dimensions.get("height"))
                                    },
                                    "length": {
                                        "unit": "centimeters",
                                        "value": float(item_dimensions.get("length"))
                                    },
                                    "width": {
                                        "unit": "centimeters",
                                        "value": float(item_dimensions.get("width"))
                                    }
                                }
                            ]
                        },
                        {
                            "op": "replace",
                            "path": "/attributes/item_weight",
                            "values": [
                                {
                                    "marketplace_id": self.marketplace.marketplace_id,
                                    "unit": "kilograms",
                                    "value": float(item_dimensions.get("weight"))
                                }
                            ]
                        },
                        {
                            "op": "replace",
                            "path": "/attributes/item_package_dimensions",
                            "value": [
                                {
                                    "marketplace_id": self.marketplace.marketplace_id,
                                    "length": {
                                        "unit": "centimeters",
                                        "value": float(package_dimensions.get("length"))
                                    },
                                    "width": {
                                        "unit": "centimeters",
                                        "value": float(package_dimensions.get("width"))
                                    },
                                    "height": {
                                        "unit": "centimeters",
                                        "value": float(package_dimensions.get("height"))
                                    }
                                }
                            ]
                        },
                        {
                            "op": "replace",
                            "path": "/attributes/item_package_weight",
                            "value": [
                                {
                                    "marketplace_id": self.marketplace.marketplace_id,
                                    "unit": "kilograms",
                                    "value": float(package_dimensions.get("weight"))
                                }
                            ]
                        }
                    ]
                }
    
                response = ListingsItems(credentials=self.credentials,
                                         marketplace=self.marketplace).patch_listings_item(
                    sellerId=seller_id, sku=sku, body=payload, marketplaceIds=self.marketplace.marketplace_id)
    
                return response.payload
            except SellingApiException as e:
                print(f"Error updating item dimensions: {e}")
                return None