Print

Zoho Creator / eBay: Get all Active Products

What?
This is an article to document a function used in Zoho Creator to retrieve the Product IDs of all the active products in a client's eBay store.

Why?
The use-case was that I wanted to retrieve a list of all the listed active products in a fixed price item listing. The example below is a function which, if given the page number and the number of entries per page, returns these in JSON as a Zoho Map datatype.

How?
I'm not going to go into detail on how I create an access token to query the eBay Trading API as you can read this in my article: Joellipman: Zoho Creator: Push to eBay Listings. You will note that I use the access token function (one that regenerates an access token or reuses if not expired). Thereafter, I use the GetMyeBaySelling API Call with specific criteria to only return the active listings as well as specific fields so my function doesn't get overwhelmed with the amount of data in the response:
copyraw
map API.fn_eBayQuery_GetActiveProducts(int p_Page, int p_PerPage)
{
	/*
    Function: fn_eBayQuery_GetActiveProducts()
    Purpose: Fetches current listings / active products
    Date Created:   2021-10-13 (Joellipman.com - Joel Lipman)
                    - Initial release

    More Info:
    - API Explorer Test Tool: https://developer.ebay.com/DevZone/build-test/test-tool/default.aspx?index=0&env=production&api=trading
    - GetMyeBaySelling Documentation: https://developer.ebay.com/devzone/xml/docs/reference/ebay/getmyebayselling.html
    */
	v_Page = ifnull(p_Page,1);
	v_PerPage = ifnull(p_PerPage,200);
	m_Output = Map();
	r_Api = API_Integration[Connection_Name == "eBay API (Production)"];
	if(r_Api.count() > 0)
	{
		v_AccessToken = thisapp.API.fn_eBayConnect_AccessToken();
		v_TradingAPIVersion = r_Api.API_Version;
		v_Endpoint = "https://api.ebay.com/ws/api.dll";
		//
		// build header
		m_Headers = Map();
		m_Headers.put("X-EBAY-API-SITEID",3);
		m_Headers.put("X-EBAY-API-COMPATIBILITY-LEVEL",v_TradingAPIVersion);
		v_ApiCall = "GetMyeBaySelling";
		m_Headers.put("X-EBAY-API-CALL-NAME",v_ApiCall);
		m_Headers.put("X-EBAY-API-IAF-TOKEN",v_AccessToken);
		//
		// build params
		m_Params = Map();
		m_Params.put("WarningLevel","High");
		m_Params.put("ErrorLanguage","en_GB");
		m_Params.put("DetailLevel","ItemReturnCategories");
		//
		// include fixed price items
		m_ActiveList = Map();
		m_ActiveList.put("Include","true");
		m_ActiveList.put("ListingType","FixedPriceItem");
		m_Pagination = Map();
		m_Pagination.put("PageNumber",v_Page);
		m_Pagination.put("EntriesPerPage",v_PerPage);
		m_ActiveList.put("Pagination",m_Pagination);
		// uncomment this when all products retrieved and to run this to just get latest products
		// m_ActiveList.put("Sort","ItemIDDescending");
		m_ActiveList.put("Sort","ItemID");
		m_Params.put("ActiveList",m_ActiveList);
		//
		// exclude sold items
		m_SoldList = Map();
		m_SoldList.put("Include","false");
		m_Params.put("SoldList",m_SoldList);
		//
		// exclude unsold items (ended before purchase)
		m_UnsoldList = Map();
		m_UnsoldList.put("Include","false");
		m_Params.put("UnsoldList",m_UnsoldList);
		//
		// exclude variations
		m_Params.put("HideVariations","true");
		//
		// restrict what fields to return
		l_OutputFields = List();
		l_OutputFields.add("BuyItNowPrice");
		l_OutputFields.add("ItemID");
		l_OutputFields.add("SKU");
		l_OutputFields.add("StartTime");
		l_OutputFields.add("TotalNumberOfPages");
		l_OutputFields.add("TotalNumberOfEntries");
		m_Params.put("OutputSelector",l_OutputFields);
		//
		// convert to xml and replace root nodes
		x_Params = m_Params.toXML();
		x_Params = x_Params.toString().replaceFirst("<root>","<?xml version=\"1.0\" encoding=\"utf-8\"?><" + v_ApiCall + "Request xmlns=\"urn:ebay:apis:eBLBaseComponents\">");
		x_Params = x_Params.toString().replaceFirst("</root>","</" + v_ApiCall + "Request>");
		//
		// send the request XML as a string
		r_ResponseXML = invokeurl
		[
			url :v_Endpoint
			type :POST
			parameters:x_Params
			headers:m_Headers
		];
		//
		// output response
		info r_ResponseXML;
		//
		// if successful then read the response
		if(r_ResponseXML.contains("<Ack>Success</Ack>"))
		{
			// 
			// init
			l_JSONItems = List();
			//
			// parse the data
			v_MainNode = "ActiveList";
			x_MainNode = r_ResponseXML.subString(r_ResponseXML.indexOf("<" + v_MainNode),r_ResponseXML.lastIndexOf("</" + v_MainNode) + v_MainNode.length() + 3);
			l_Items = x_MainNode.executeXPath("//ItemArray/*").toXmlList();
			for each x_Item in l_Items
            {
				m_Item = Map();
				m_Item.put("ID", x_Item.executeXPath("//ItemID/text()").toLong());
				m_Item.put("Currency", x_Item.executeXPath("//BuyItNowPrice/@currencyID").executeXPath("/currencyID/text()"));
				m_Item.put("Price", x_Item.executeXPath("//BuyItNowPrice/text()").toDecimal());
				m_Item.put("SKU", x_Item.executeXPath("//SKU/text()"));
				m_Item.put("StartTime", x_Item.executeXPath("//ListingDetails/StartTime/text()").getPrefix(".").replaceFirst("T", " ", true));
				l_JSONItems.add(m_Item);
            } 
			m_Output.put("Items", l_JSONItems);
			v_PageNode = "PaginationResult";
			x_PageNode = r_ResponseXML.subString(r_ResponseXML.indexOf("<" + v_PageNode),r_ResponseXML.lastIndexOf("</" + v_PageNode) + v_PageNode.length() + 3);
			m_Output.put("TotalEntries", x_PageNode.executeXPath("//TotalNumberOfPages/text()").toLong());
			m_Output.put("TotalPages", x_PageNode.executeXPath("//TotalNumberOfEntries/text()").toLong());
		}
	}
	return m_Output;
}
  1.  map API.fn_eBayQuery_GetActiveProducts(int p_Page, int p_PerPage) 
  2.  { 
  3.      /* 
  4.      Function: fn_eBayQuery_GetActiveProducts() 
  5.      Purpose: Fetches current listings / active products 
  6.      Date Created:   2021-10-13 (Joellipman.com - Joel Lipman) 
  7.                      - Initial release 
  8.   
  9.      More Info: 
  10.      - API Explorer Test Tool: https://developer.ebay.com/DevZone/build-test/test-tool/default.aspx?index=0&env=production&api=trading 
  11.      - GetMyeBaySelling Documentation: https://developer.ebay.com/devzone/xml/docs/reference/ebay/getmyebayselling.html 
  12.      */ 
  13.      v_Page = ifnull(p_Page,1)
  14.      v_PerPage = ifnull(p_PerPage,200)
  15.      m_Output = Map()
  16.      r_Api = API_Integration[Connection_Name == "eBay API (Production)"]
  17.      if(r_Api.count() > 0) 
  18.      { 
  19.          v_AccessToken = thisapp.API.fn_eBayConnect_AccessToken()
  20.          v_TradingAPIVersion = r_Api.API_Version; 
  21.          v_Endpoint = "https://api.ebay.com/ws/api.dll"
  22.          // 
  23.          // build header 
  24.          m_Headers = Map()
  25.          m_Headers.put("X-EBAY-API-SITEID",3)
  26.          m_Headers.put("X-EBAY-API-COMPATIBILITY-LEVEL",v_TradingAPIVersion)
  27.          v_ApiCall = "GetMyeBaySelling"
  28.          m_Headers.put("X-EBAY-API-CALL-NAME",v_ApiCall)
  29.          m_Headers.put("X-EBAY-API-IAF-TOKEN",v_AccessToken)
  30.          // 
  31.          // build params 
  32.          m_Params = Map()
  33.          m_Params.put("WarningLevel","High")
  34.          m_Params.put("ErrorLanguage","en_GB")
  35.          m_Params.put("DetailLevel","ItemReturnCategories")
  36.          // 
  37.          // include fixed price items 
  38.          m_ActiveList = Map()
  39.          m_ActiveList.put("Include","true")
  40.          m_ActiveList.put("ListingType","FixedPriceItem")
  41.          m_Pagination = Map()
  42.          m_Pagination.put("PageNumber",v_Page)
  43.          m_Pagination.put("EntriesPerPage",v_PerPage)
  44.          m_ActiveList.put("Pagination",m_Pagination)
  45.          // uncomment this when all products retrieved and to run this to just get latest products 
  46.          // m_ActiveList.put("Sort","ItemIDDescending")
  47.          m_ActiveList.put("Sort","ItemID")
  48.          m_Params.put("ActiveList",m_ActiveList)
  49.          // 
  50.          // exclude sold items 
  51.          m_SoldList = Map()
  52.          m_SoldList.put("Include","false")
  53.          m_Params.put("SoldList",m_SoldList)
  54.          // 
  55.          // exclude unsold items (ended before purchase) 
  56.          m_UnsoldList = Map()
  57.          m_UnsoldList.put("Include","false")
  58.          m_Params.put("UnsoldList",m_UnsoldList)
  59.          // 
  60.          // exclude variations 
  61.          m_Params.put("HideVariations","true")
  62.          // 
  63.          // restrict what fields to return 
  64.          l_OutputFields = List()
  65.          l_OutputFields.add("BuyItNowPrice")
  66.          l_OutputFields.add("ItemID")
  67.          l_OutputFields.add("SKU")
  68.          l_OutputFields.add("StartTime")
  69.          l_OutputFields.add("TotalNumberOfPages")
  70.          l_OutputFields.add("TotalNumberOfEntries")
  71.          m_Params.put("OutputSelector",l_OutputFields)
  72.          // 
  73.          // convert to xml and replace root nodes 
  74.          x_Params = m_Params.toXML()
  75.          x_Params = x_Params.toString().replaceFirst("<root>","<?xml version=\"1.0\" encoding=\"utf-8\"?><" + v_ApiCall + "Request xmlns=\"urn:ebay:apis:eBLBaseComponents\">")
  76.          x_Params = x_Params.toString().replaceFirst("</root>","</" + v_ApiCall + "Request>")
  77.          // 
  78.          // send the request XML as a string 
  79.          r_ResponseXML = invokeUrl 
  80.          [ 
  81.              url :v_Endpoint 
  82.              type :POST 
  83.              parameters:x_Params 
  84.              headers:m_Headers 
  85.          ]
  86.          // 
  87.          // output response 
  88.          info r_ResponseXML; 
  89.          // 
  90.          // if successful then read the response 
  91.          if(r_ResponseXML.contains("<Ack>Success</Ack>")) 
  92.          { 
  93.              // 
  94.              // init 
  95.              l_JSONItems = List()
  96.              // 
  97.              // parse the data 
  98.              v_MainNode = "ActiveList"
  99.              x_MainNode = r_ResponseXML.subString(r_ResponseXML.indexOf("<" + v_MainNode),r_ResponseXML.lastIndexOf("</" + v_MainNode) + v_MainNode.length() + 3)
  100.              l_Items = x_MainNode.executeXPath("//ItemArray/*").toXmlList()
  101.              for each x_Item in l_Items 
  102.              { 
  103.                  m_Item = Map()
  104.                  m_Item.put("ID", x_Item.executeXPath("//ItemID/text()").toLong())
  105.                  m_Item.put("Currency", x_Item.executeXPath("//BuyItNowPrice/@currencyID").executeXPath("/currencyID/text()"))
  106.                  m_Item.put("Price", x_Item.executeXPath("//BuyItNowPrice/text()").toDecimal())
  107.                  m_Item.put("SKU", x_Item.executeXPath("//SKU/text()"))
  108.                  m_Item.put("StartTime", x_Item.executeXPath("//ListingDetails/StartTime/text()").getPrefix(".").replaceFirst("T", " ", true))
  109.                  l_JSONItems.add(m_Item)
  110.              } 
  111.              m_Output.put("Items", l_JSONItems)
  112.              v_PageNode = "PaginationResult"
  113.              x_PageNode = r_ResponseXML.subString(r_ResponseXML.indexOf("<" + v_PageNode),r_ResponseXML.lastIndexOf("</" + v_PageNode) + v_PageNode.length() + 3)
  114.              m_Output.put("TotalEntries", x_PageNode.executeXPath("//TotalNumberOfPages/text()").toLong())
  115.              m_Output.put("TotalPages", x_PageNode.executeXPath("//TotalNumberOfEntries/text()").toLong())
  116.          } 
  117.      } 
  118.      return m_Output; 
  119.  } 
If I give it the parameter values of page 1 and 5 entries per page, the first info will display to me the XML response:
copyraw
<?xml version="1.0" encoding="UTF-8"?>
<GetMyeBaySellingResponse
	xmlns="urn:ebay:apis:eBLBaseComponents">
	<Timestamp>2021-10-13T19:29:49.314Z</Timestamp>
	<Ack>Success</Ack>
	<Version>1173</Version>
	<Build>E1173_CORE_APISELLING_19187371_R1</Build>
	<ActiveList>
		<ItemArray>
			<Item>
				<BuyItNowPrice currencyID="GBP">39.95</BuyItNowPrice>
				<ItemID>1234567890</ItemID>
				<ListingDetails>
					<StartTime>2019-11-06T16:56:14.000Z</StartTime>
				</ListingDetails>
				<SellingStatus/>
				<ShippingDetails/>
				<SKU>TEST-001</SKU>
			</Item>
			<Item>
				<BuyItNowPrice currencyID="GBP">42.95</BuyItNowPrice>
				<ItemID>2345678901</ItemID>
				<ListingDetails>
					<StartTime>2020-01-15T16:39:36.000Z</StartTime>
				</ListingDetails>
				<SellingStatus/>
				<ShippingDetails/>
				<SKU>TEST-002</SKU>
			</Item>
			<Item>
				<BuyItNowPrice currencyID="GBP">151.95</BuyItNowPrice>
				<ItemID>3456789012</ItemID>
				<ListingDetails>
					<StartTime>2020-03-07T11:22:09.000Z</StartTime>
				</ListingDetails>
				<SellingStatus/>
				<ShippingDetails/>
				<SKU>TEST-003</SKU>
			</Item>
			<Item>
				<BuyItNowPrice currencyID="GBP">37.95</BuyItNowPrice>
				<ItemID>4567890123</ItemID>
				<ListingDetails>
					<StartTime>2020-03-23T15:17:30.000Z</StartTime>
				</ListingDetails>
				<SellingStatus/>
				<ShippingDetails/>
				<SKU>TEST-004</SKU>
			</Item>
			<Item>
				<BuyItNowPrice currencyID="GBP">47.95</BuyItNowPrice>
				<ItemID>5678901234</ItemID>
				<ListingDetails>
					<StartTime>2020-03-23T15:17:32.000Z</StartTime>
				</ListingDetails>
				<SellingStatus/>
				<ShippingDetails/>
				<SKU>TEST-005</SKU>
			</Item>
		</ItemArray>
		<PaginationResult>
			<TotalNumberOfPages>105</TotalNumberOfPages>
			<TotalNumberOfEntries>522</TotalNumberOfEntries>
		</PaginationResult>
	</ActiveList>
</GetMyeBaySellingResponse>
  1.  <?xml version="1.0" encoding="UTF-8"?> 
  2.  <GetMyeBaySellingResponse 
  3.      xmlns="urn:ebay:apis:eBLBaseComponents"> 
  4.      <Timestamp>2021-10-13T19:29:49.314Z</Timestamp> 
  5.      <Ack>Success</Ack> 
  6.      <Version>1173</Version> 
  7.      <Build>E1173_CORE_APISELLING_19187371_R1</Build> 
  8.      <ActiveList> 
  9.          <ItemArray> 
  10.              <Item> 
  11.                  <BuyItNowPrice currencyID="GBP">39.95</BuyItNowPrice> 
  12.                  <ItemID>1234567890</ItemID> 
  13.                  <ListingDetails> 
  14.                      <StartTime>2019-11-06T16:56:14.000Z</StartTime> 
  15.                  </ListingDetails> 
  16.                  <SellingStatus/> 
  17.                  <ShippingDetails/> 
  18.                  <SKU>TEST-001</SKU> 
  19.              </Item> 
  20.              <Item> 
  21.                  <BuyItNowPrice currencyID="GBP">42.95</BuyItNowPrice> 
  22.                  <ItemID>2345678901</ItemID> 
  23.                  <ListingDetails> 
  24.                      <StartTime>2020-01-15T16:39:36.000Z</StartTime> 
  25.                  </ListingDetails> 
  26.                  <SellingStatus/> 
  27.                  <ShippingDetails/> 
  28.                  <SKU>TEST-002</SKU> 
  29.              </Item> 
  30.              <Item> 
  31.                  <BuyItNowPrice currencyID="GBP">151.95</BuyItNowPrice> 
  32.                  <ItemID>3456789012</ItemID> 
  33.                  <ListingDetails> 
  34.                      <StartTime>2020-03-07T11:22:09.000Z</StartTime> 
  35.                  </ListingDetails> 
  36.                  <SellingStatus/> 
  37.                  <ShippingDetails/> 
  38.                  <SKU>TEST-003</SKU> 
  39.              </Item> 
  40.              <Item> 
  41.                  <BuyItNowPrice currencyID="GBP">37.95</BuyItNowPrice> 
  42.                  <ItemID>4567890123</ItemID> 
  43.                  <ListingDetails> 
  44.                      <StartTime>2020-03-23T15:17:30.000Z</StartTime> 
  45.                  </ListingDetails> 
  46.                  <SellingStatus/> 
  47.                  <ShippingDetails/> 
  48.                  <SKU>TEST-004</SKU> 
  49.              </Item> 
  50.              <Item> 
  51.                  <BuyItNowPrice currencyID="GBP">47.95</BuyItNowPrice> 
  52.                  <ItemID>5678901234</ItemID> 
  53.                  <ListingDetails> 
  54.                      <StartTime>2020-03-23T15:17:32.000Z</StartTime> 
  55.                  </ListingDetails> 
  56.                  <SellingStatus/> 
  57.                  <ShippingDetails/> 
  58.                  <SKU>TEST-005</SKU> 
  59.              </Item> 
  60.          </ItemArray> 
  61.          <PaginationResult> 
  62.              <TotalNumberOfPages>105</TotalNumberOfPages> 
  63.              <TotalNumberOfEntries>522</TotalNumberOfEntries> 
  64.          </PaginationResult> 
  65.      </ActiveList> 
  66.  </GetMyeBaySellingResponse> 
and the output gives me a JSON Map that looks similar to the following:
copyraw
{
  "Items": [
    {
      "ID": 1234567890,
      "Currency": "GBP",
      "Price": 39.95,
      "SKU": "TEST-001",
      "StartTime": "2019-11-06 16:56:14"
    },
    {
      "ID": 2345678901,
      "Currency": "GBP",
      "Price": 42.95,
      "SKU": "TEST-002",
      "StartTime": "2020-01-15 16:39:36"
    },
    {
      "ID": 3456789012,
      "Currency": "GBP",
      "Price": 151.95,
      "SKU": "TEST-003",
      "StartTime": "2020-03-07 11:22:09"
    },
    {
      "ID": 4567890123,
      "Currency": "GBP",
      "Price": 37.95,
      "SKU": "TEST-004",
      "StartTime": "2020-03-23 15:17:30"
    },
    {
      "ID": 5678901234,
      "Currency": "GBP",
      "Price": 47.95,
      "SKU": "TEST-005",
      "StartTime": "2020-03-23 15:17:32"
    }
  ],
  "TotalEntries": "105",
  "TotalPages": "522"
}
  1.  { 
  2.    "Items": [ 
  3.      { 
  4.        "ID": 1234567890, 
  5.        "Currency": "GBP", 
  6.        "Price": 39.95, 
  7.        "SKU": "TEST-001", 
  8.        "StartTime": "2019-11-06 16:56:14" 
  9.      }, 
  10.      { 
  11.        "ID": 2345678901, 
  12.        "Currency": "GBP", 
  13.        "Price": 42.95, 
  14.        "SKU": "TEST-002", 
  15.        "StartTime": "2020-01-15 16:39:36" 
  16.      }, 
  17.      { 
  18.        "ID": 3456789012, 
  19.        "Currency": "GBP", 
  20.        "Price": 151.95, 
  21.        "SKU": "TEST-003", 
  22.        "StartTime": "2020-03-07 11:22:09" 
  23.      }, 
  24.      { 
  25.        "ID": 4567890123, 
  26.        "Currency": "GBP", 
  27.        "Price": 37.95, 
  28.        "SKU": "TEST-004", 
  29.        "StartTime": "2020-03-23 15:17:30" 
  30.      }, 
  31.      { 
  32.        "ID": 5678901234, 
  33.        "Currency": "GBP", 
  34.        "Price": 47.95, 
  35.        "SKU": "TEST-005", 
  36.        "StartTime": "2020-03-23 15:17:32" 
  37.      } 
  38.    ], 
  39.    "TotalEntries": "105", 
  40.    "TotalPages": "522" 
  41.  } 

Additional:
See my other articles for integrating eBay API with Zoho Creator:
Category: Zoho :: Article: 778
,