Amazon MWS

How To Download Amazon Reports Using MWS API

In  this tutorial, we will learn about downloading Amazon MWS report using the Report API using C#.

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

For example, order reports can be set up for the 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,

 

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 enumeration to use report type globally.

public enum Report_Type  
{  
_GET_FLAT_FILE_OPEN_LISTINGS_DATA_,  
_GET_AMAZON_FULFILLED_SHIPMENTS_DATA_,  
_GET_FLAT_FILE_ACTIONABLE_ORDER_DATA_,  
_GET_FLAT_FILE_ORDER_REPORT_DATA_,  
_GET_FLAT_FILE_ORDERS_DATA_,  
_GET_CONVERGED_FLAT_FILE_ORDER_REPORT_DATA_,  
_GET_FLAT_FILE_PAYMENT_SETTLEMENT_DATA_,  
_GET_PAYMENT_SETTLEMENT_DATA_,  
_GET_MERCHANT_LISTINGS_DATA_,  
_GET_MERCHANT_LISTINGS_DATA_LITE_,  
_GET_MERCHANT_LISTINGS_DATA_LITER_,  
_GET_MERCHANT_CANCELLED_LISTINGS_DATA_,  
_GET_AFN_INVENTORY_DATA_,  
_GET_MERCHANT_LISTINGS_INACTIVE_DATA_,  
_GET_MERCHANT_LISTINGS_DATA_BACK_COMPAT_  
}

Function to send Request. It will call Get_Report_List_Filtered_By (Report type,MarketplaceID)

public List < Amazon_Open_Listings > RequestListingReport(Report_Type Report_Type, string "MarketPlaceId") {  
    List < Amazon_Open_Listings > list = new List < Amazon_Open_Listings > ();  
    try {  
        var ReportId = Get_Report_List_Filtered_By(Report_Type, 1000, string "Seller ID");  
        if (ReportId != null && ReportId.Count > 0) {  
            //Pass most recent report id and download report  
            var ReportPath = Download_Report(ReportId[0].ReportId, "Seller ID", "File path where you want to download report");  
            //convert report to c# class object  
            list.AddRange(Convert_Open_Listings_As_List(ReportPath));  
        }  
        return list;  
    } catch (Exception ex) {  
        return null;  
    }  
}

This function will accept Report type, Number of results of reports, and Seller ID. It returns Highest (latest) report id.

You can sort the results by report id as well as request id, it depends on the situation.

public List < ReportInfo > Get_Report_List_Filtered_By(Report_Type "Your report type", int _number_To_Return, string "Your Seller ID") {  
    try {  
        GetReportListRequest report_List_Request = new GetReportListRequest();  
        report_List_Request.Merchant = "Your Seller ID";  
        report_List_Request.MaxCount = _number_To_Return;  
        // filter by report type also  
        TypeList tl = new TypeList();  
        tl.Type.Add(Report_Type.ToString());  
        report_List_Request.ReportTypeList = tl;  
        // Get the response  
        GetReportListResponse report_List_Response = ReportClient.GetReportList(report_List_Request);  
        GetReportListResult report_List_Result = report_List_Response.GetReportListResult;  
        // Hold the report results  
        List < ReportInfo > retReport = report_List_Result.ReportInfo;  
        // Finally, sort the results to pop the highest report ID to the top  
        // Do a default sort by the report ID (most recent first)  
        retReport = retReport.OrderBy(x => x.ReportId).Reverse().ToList();  
        return retReport;  
    } catch (Exception ex) {  
        return null;  
    }  
}

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.
public string Download_Report(string _report_ID, string "Seller ID", string BasePath )  
       {  
           GetReportRequest get_Report = new GetReportRequest();  
           get_Report.Merchant = "Seller ID";  
           string path = _report_ID + "_" + Guid.NewGuid();  
           string thePath = BasePath + "\\" + String.Format("{0}.txt", path);  
           get_Report.ReportId = _report_ID;  
  
           get_Report.Report = File.Open(thePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);  
           GetReportResponse report_Response = ReportClient.GetReport(get_Report);  
           get_Report.Report.Close();  
           return thePath;  
       }

GetReport

It will return 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.
public List<Amazon_Open_Listings> Convert_Open_Listings_As_List(string reportPath)  
       {  
           // create the list to hold the open listing  
           List<Amazon_Open_Listings> openList = new List<Amazon_Open_Listings>();  
  
           // Check to see if the file exists and parse it if it does  
           if (File.Exists(reportPath))  
           {  
               // Get a stream to the report  
               StreamReader reportReader;  
               reportReader = File.OpenText(reportPath);  
  
               // read the file into the list line by line  
               string line;  
               while ((line = reportReader.ReadLine()) != null)  
               {  
                   // Split each line by the tab sequence  
                   string[] columns = line.Split('\t');  
                   // Populate the list with Amazon Open listings  
                   Amazon_Open_Listings listing = new Amazon_Open_Listings();  
                   listing.SKU = columns[3].ToString();  
                   listing.FulFilledBy = "MFN";  
                   listing.Title = columns[0].ToString();  
                   listing.opendate = columns[6].ToString();  
                   listing.ASIN = columns[22].ToString();  
                   listing.Price = columns[4].ToString();  
                   listing.Qty = columns[5].ToString();  
                   listing.Recorded = System.DateTime.Now;  
                   // Add the listing to the list  
                   openList.Add(listing);  
  
               }  
               reportReader.Close();  
           }  
           if (openList != null && openList.Count > 0)  
               return openList.Skip(1).ToList();  
           return openList;  
       }

Note

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

And here is our class/model,
public partial class Amazon_Open_Listings  
  {  
      public string SKU { get; set; }  
      public string ASIN { get; set; }  
      public string ProfileName { get; set; }  
      public int ProfileID { get; set; }  
      public string FulFilledBy { get; set; }  
      public string opendate { get; set; }  
      public string productId { get; set; }  
      public string Price { get; set; }  
      public string Qty { get; set; }  
      public string Report_ID { get; set; }  
      public System.DateTime Recorded { get; set; }  
      public Nullable<decimal> Lowest_Price { get; set; }  
      public string Title { get; set; }  
      public Nullable<decimal> AmazonSalesRank { get; set; }  
      public string FileUrl { get; set; }  
      public string Carrier { get; set; }  
      public string Service { get; set; }  
      public string TrackingNumber { get; set; }  
      public decimal ShippingFee { get; set; }  
      public string OrderId { get; set; }  
  
  }

Please give your valuable feedback/comments/questions about this article below. Please let me know how you like and understand this article and how I could improve it.

Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

View Comments

  • ReportClient does not exist in the current context erroe message comes. pls help

    • Public static MarketplaceWebServiceClient ReportClient;
      public static MarketplaceWebServiceConfig _reportConfig;

      _reportConfig = new MarketplaceWebServiceConfig
      {

      ServiceURL = "https://mws.amazonservices.com"
      };

      ReportClient = new MarketplaceWebServiceClient("accessKeyID", "secretKey", "project name", "1.0", _reportConfig);

Share
Published by
Faisal Pathan

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago