Search code examples
amazon-selling-partner-api

InvalidInput error with no details calling getItemOffersBatchRequest in SP-API


Amazon SP-API has two API operations for retrieving ItemOffers, one for a single ASIN (getItemOffers) and another for a list of up to 20 ASINs (getItemOffersBatch). I get expected results when calling getItemOffers, but when I call getItemOffersBatch, I get a 400 response with a JSON message without a lot of helpful detail,

{
  code: "InvalidInput",
  message: "InvalidInput",
  details: "",
}

Here is the function that is working. Calling libSeller.getItemOffers(asin) returns an expected document.

libSeller.getItemOffers = async function (asin) {
  try {
    let apiPath='/products/pricing/v0/items/' + asin + '/offers';
    let sellingPartner = new SellingPartnerAPI(mwsConfig);
    let res = await sellingPartner.callAPI({
        api_path:apiPath,
        method:'GET',
        query:{MarketplaceId:account.MarketplaceId, ItemCondition:'New'}
    }).catch(err => {
        console.error(err);
    });
    return res;
  }
  catch (err) {
    console.error(err);
    return null;
  }
}

This is the function call that is not working. Calling libSeller.getItemOffersBatch(['ASIN1','ASIN2']) prints an error message (using valid ASINs).

libSeller.getItemOffersBatch = async function (asins) {
  try   {
    let sellingPartner = new SellingPartnerAPI(mwsConfig);
    let requests=[];
    asins.forEach(asin => { 
        requests.push({'uri':'/products/pricing/v0/items/' + asin + '/offers', method:'GET', MarketplaceId:account.MarketplaceId, ItemCondition:'New'});
    });
    console.log(requests);
    let res = await sellingPartner.callAPI({
        api_path:'/batches/products/pricing/v0/itemOffers',
        method:'POST',
        body:{'getItemOffersBatchRequestBody':{'requests':requests}}
    }).catch(err => {
        console.error(err);
    });
    return res;
  } catch (err) {
    console.error(err);
    return null;
  }
}

The stack trace looks like this:

CustomError: InvalidInput
    at SellingPartner.callAPI (/mnt/c/Users/John/dev/repricer3/node_modules/amazon-sp-api/lib/SellingPartner.js:658:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async libSeller.getItemOffersBatch (/mnt/c/Users/John/dev/repricer3/libs/libSeller.js:172:13) {
  code: 'InvalidInput',
  details: '',
  type: 'error'
}

I'm finding this difficult to troubleshoot with tools because the message is coming from the API call, and I haven't been able to determine anything in my input that appears to be invalid. Has anyone encountered and resolved this problem, or can see any reason for the single-ASIN function to work but fail in the multi-ASIN function?


Solution

  • The problem was in my interpretation of their documentation.

    Throughout most of the API, when addressing POST messages, the documentation describes parameters with the type:"Body" and name:"body". I would assign an attribute named "body" to the configuration object passed into sellingPartner.callAPI which holds the data object posted as JSON to the API.

    Type Name Description Schema
    Body body Link to Schema

    In this particular case, the documentation used the Name "getListingOffersBatchRequestBody" where they would normally just write "body". I took that to mean an object with that property name rather than the root object passed as the body (which is how one treats all the child objects one finds under Schema, but not for the root body object).

    Type Name Description Schema
    Body getListingOffersBatchRequestBody Link to Schema

    The fix was to change the "body" parameter of sellingPartner.callAPI from

    body:{'getItemOffersBatchRequestBody':{'requests':requests}}
    

    to this

    body:{'requests':requests}