In a previous post I’ve shown how to to submit a feed using the Amazon Feeds API. In this post I’m going to show how to download a report from Amazon using the Reports API. Download the API from the Amazon Developer Home page.
Before we actually download the report, Amazon requires us to submit a request. The time we need to wait after the request until we can download the report varies based on the report type and possibly other factors.
Below are 2 examples of report types:
- _GET_MERCHANT_LISTINGS_DATA_ – download all inventory.
- _GET_FLAT_FILE_ORDERS_DATA_ – download orders from specified date.
For readability, I created an Report Type enum, with these report type values, which will be used as part of the argument of the below method. This method requests the report.
public RequestReportResult GetRequestReportResult(ReportType reportType)
RequestReportRequest reportRequestRequest = new RequestReportRequest();
reportRequestRequest.Merchant = Merchant;
reportRequestRequest.ReportType = reportType.ToString();
// If this is an order report, get only last week
if (reportType == ReportType._GET_FLAT_FILE_ORDERS_DATA_)
reportRequestRequest.StartDate = DateTime.Now.AddDays(-7);
The GetClient method used here returns a MarketplaceWebServiceClient object. Refer to my previous Amazon post for the code. Also, notice that in the case of a request for an order report, we are setting the StartDate property to specify the date from which we want orders downloaded.
Now we can use this method to get a list of the requests and their statuses:
private List<ReportRequestInfo> GetReportRequests()
GetReportRequestListRequest reportlistReqests = new GetReportRequestListRequest();
reportlistReqests.Merchant = Merchant;
GetReportRequestListResponse reportlistResponse = GetClient().GetReportRequestList(reportlistReqests);
Once the status of our request is _DONE_, we can go ahead and download the report. The data will be saved to a text file. From there you can do whatever you want with it, like import it into your DB.
Here’s the method to download the report. It takes in a RequestReportInfo object (which you get from the GetReportRequests method):
private FileInfo GetReportFile(ReportRequestInfo reportInfo)
GetReportRequest reportRequest = new GetReportRequest();
reportRequest.Merchant = Merchant;
GetReportResponse reportResponse = new GetReportResponse();
string path = HttpContext.Current.Server.MapPath("~/Files/Report-" + reportInfo.ReportType + "-" + reportInfo.GeneratedReportId + ".txt");
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
reportRequest.Report = stream;
reportResponse = GetClient().GetReport(reportRequest);
return new FileInfo(path);
That’s it! Again, there isn’t much documentation on this so I hope this post is found helpful.