Download Amazon Reports Using MWS API

Amazon MWS supports the management of seller orders for items sold on Amazon.com. Orders received can be downloaded and acknowledged using Amazon MWS. A variety of order report formats and actions are supported to meet specific seller needs.

For example, order reports can be set up for automatic generation following a schedule that's optimized to match seller workflow.

Report API  lets you request various reports that help you manage your sales on Amazon. 

What you should know about the Amazon MWS Reports API

Flowchart of the process for submitting and receiving an on-request report to Amazon,

Flowchart 

First, we need to install Amazon MWS c# client library

Once you download client library attach that project with your existing project and write the below code to call Report API.

Call to Request, download and convert the report into a C# class object.
Example to call report:

Note
We should create the report enumuration to use report type globally.
  1. public enum Report_Type  
  2. {  
  3. _GET_FLAT_FILE_OPEN_LISTINGS_DATA_,  
  4. _GET_AMAZON_FULFILLED_SHIPMENTS_DATA_,  
  5. _GET_FLAT_FILE_ACTIONABLE_ORDER_DATA_,  
  6. _GET_FLAT_FILE_ORDER_REPORT_DATA_,  
  7. _GET_FLAT_FILE_ORDERS_DATA_,  
  8. _GET_CONVERGED_FLAT_FILE_ORDER_REPORT_DATA_,  
  9. _GET_FLAT_FILE_PAYMENT_SETTLEMENT_DATA_,  
  10. _GET_PAYMENT_SETTLEMENT_DATA_,  
  11. _GET_MERCHANT_LISTINGS_DATA_,  
  12. _GET_MERCHANT_LISTINGS_DATA_LITE_,  
  13. _GET_MERCHANT_LISTINGS_DATA_LITER_,  
  14. _GET_MERCHANT_CANCELLED_LISTINGS_DATA_,  
  15. _GET_AFN_INVENTORY_DATA_,  
  16. _GET_MERCHANT_LISTINGS_INACTIVE_DATA_,  
  17. _GET_MERCHANT_LISTINGS_DATA_BACK_COMPAT_  
  18. }  
Function to send Request. It will call Get_Report_List_Filtered_By (Report type,MarketplaceID)
  1. public List < Amazon_Open_Listings > RequestListingReport(Report_Type Report_Type, string "MarketPlaceId") {  
  2.     List < Amazon_Open_Listings > list = new List < Amazon_Open_Listings > ();  
  3.     try {  
  4.         var ReportId = Get_Report_List_Filtered_By(Report_Type, 1000, string "Seller ID");  
  5.         if (ReportId != null && ReportId.Count > 0) {  
  6.             //Pass most recent report id and download report  
  7.             var ReportPath = Download_Report(ReportId[0].ReportId, "Seller ID""File path where you want to download report");  
  8.             //convert report to c# class object  
  9.             list.AddRange(Convert_Open_Listings_As_List(ReportPath));  
  10.         }  
  11.         return list;  
  12.     } catch (Exception ex) {  
  13.         return null;  
  14.     }  
  15. }  
This function will accept Report type, Number of results of reports, and Seller ID. It returns Highest (latest) report id.

You can sort result by report id as well as request id, it depends on the situation.
  1. public List < ReportInfo > Get_Report_List_Filtered_By(Report_Type "Your report type"int _number_To_Return, string "Your Seller ID") {  
  2.     try {  
  3.         GetReportListRequest report_List_Request = new GetReportListRequest();  
  4.         report_List_Request.Merchant = "Your Seller ID";  
  5.         report_List_Request.MaxCount = _number_To_Return;  
  6.         // filter by report type also  
  7.         TypeList tl = new TypeList();  
  8.         tl.Type.Add(Report_Type.ToString());  
  9.         report_List_Request.ReportTypeList = tl;  
  10.         // Get the response  
  11.         GetReportListResponse report_List_Response = ReportClient.GetReportList(report_List_Request);  
  12.         GetReportListResult report_List_Result = report_List_Response.GetReportListResult;  
  13.         // Hold the report results  
  14.         List < ReportInfo > retReport = report_List_Result.ReportInfo;  
  15.         // Finally, sort the results to pop the highest report ID to the top  
  16.         // Do a default sort by the report ID (most recent first)  
  17.         retReport = retReport.OrderBy(x => x.ReportId).Reverse().ToList();  
  18.         return retReport;  
  19.     } catch (Exception ex) {  
  20.         return null;  
  21.     }  
  22. }  
Request parameters (All parameters are optional)
  • ReportRequestIdList
    A structured list of ReportRequestIdvalues. If you pass in ReportRequestIdvalues, other query conditions are ignored.

  • ReportTypeList
    A structured list of ReportType enumeration values.

  • ReportProcessingStatusList
    A structured list of report processing statuses by which to filter report requests.

  • MaxCount
    A non-negative integer that represents the maximum number of report requests to return. If you specify a number greater than 100, the request is rejected.

  • RequestedFromDate
    The start of the date range used for selecting the data to report, in ISO 8601 date time format.

  • RequestedToDate
    The end of the date range used for selecting the data to report, in ISO 8601 date time format. 
This function accepts report id, seller id and File path where the report will be downloaded.
  1. public string Download_Report(string _report_ID, string "Seller ID"string BasePath )  
  2.        {  
  3.            GetReportRequest get_Report = new GetReportRequest();  
  4.            get_Report.Merchant = "Seller ID";  
  5.            string path = _report_ID + "_" + Guid.NewGuid();  
  6.            string thePath = BasePath + "\\" + String.Format("{0}.txt", path);  
  7.            get_Report.ReportId = _report_ID;  
  8.   
  9.            get_Report.Report = File.Open(thePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);  
  10.            GetReportResponse report_Response = ReportClient.GetReport(get_Report);  
  11.            get_Report.Report.Close();  
  12.            return thePath;  
  13.        }  

GetReport

It will returns the contents of a report and the Content-MD5 header for the returned report body.

Request parameters (Required)

ReportId
A unique identifier of the report to download, obtained from the GetReportList operation or the GeneratedReportId of a ReportRequest. 
 
This will convert/map our tab-delimited report to C# class model so we can use it as a list of models.
  1. public List<Amazon_Open_Listings> Convert_Open_Listings_As_List(string reportPath)  
  2.        {  
  3.            // create the list to hold the open listing  
  4.            List<Amazon_Open_Listings> openList = new List<Amazon_Open_Listings>();  
  5.   
  6.            // Check to see if the file exists and parse it if it does  
  7.            if (File.Exists(reportPath))  
  8.            {  
  9.                // Get a stream to the report  
  10.                StreamReader reportReader;  
  11.                reportReader = File.OpenText(reportPath);  
  12.   
  13.                // read the file into the list line by line  
  14.                string line;  
  15.                while ((line = reportReader.ReadLine()) != null)  
  16.                {  
  17.                    // Split each line by the tab sequence  
  18.                    string[] columns = line.Split('\t');  
  19.                    // Populate the list with Amazon Open listings  
  20.                    Amazon_Open_Listings listing = new Amazon_Open_Listings();  
  21.                    listing.SKU = columns[3].ToString();  
  22.                    listing.FulFilledBy = "MFN";  
  23.                    listing.Title = columns[0].ToString();  
  24.                    listing.opendate = columns[6].ToString();  
  25.                    listing.ASIN = columns[22].ToString();  
  26.                    listing.Price = columns[4].ToString();  
  27.                    listing.Qty = columns[5].ToString();  
  28.                    listing.Recorded = System.DateTime.Now;  
  29.                    // Add the listing to the list  
  30.                    openList.Add(listing);  
  31.   
  32.                }  
  33.                reportReader.Close();  
  34.            }  
  35.            if (openList != null && openList.Count > 0)  
  36.                return openList.Skip(1).ToList();  
  37.            return openList;  
  38.        }  

Note
Here, we should skip the first line because it contains Headers.

And here is our class/model,
  1. public partial class Amazon_Open_Listings  
  2.   {  
  3.       public string SKU { getset; }  
  4.       public string ASIN { getset; }  
  5.       public string ProfileName { getset; }  
  6.       public int ProfileID { getset; }  
  7.       public string FulFilledBy { getset; }  
  8.       public string opendate { getset; }  
  9.       public string productId { getset; }  
  10.       public string Price { getset; }  
  11.       public string Qty { getset; }  
  12.       public string Report_ID { getset; }  
  13.       public System.DateTime Recorded { getset; }  
  14.       public Nullable<decimal> Lowest_Price { getset; }  
  15.       public string Title { getset; }  
  16.       public Nullable<decimal> AmazonSalesRank { getset; }  
  17.       public string FileUrl { getset; }  
  18.       public string Carrier { getset; }  
  19.       public string Service { getset; }  
  20.       public string TrackingNumber { getset; }  
  21.       public decimal ShippingFee { getset; }  
  22.       public string OrderId { getset; }  
  23.   
  24.   }