Amazon FBA Fee Using Amazon MWS API In C#

In this article, we will learn about getting the Amazon FBA Fee using the Amazon MWS API in C#. Every seller needs to know their FBA fee in order to improve himself/herself regarding selling products on Amazon. Amazon provides us the GetMyFeeEstimate API call in products API.
 
Below are a few articles related to MWS API you can refer to.

Prerequisites

  • Knowledge of C#
  • You must have the Amazon credentials like SellerId, Access Key, etc.
So, let’s get started.
 
Firstly, install the NuGet package for products API.
 
Install-Package MWSProductsCSharpClientLibrary -Version 2017.3.22
 
Starting from _Layout.cshtml file. 
  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4.     <meta charset="utf-8" />    
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">    
  6.     <title>@ViewBag.Title - My ASP.NET Application</title>    
  7.     @Styles.Render("~/Content/css")    
  8.     @Scripts.Render("~/bundles/modernizr")    
  9.     @Scripts.Render("~/bundles/jquery")    
  10.     @Scripts.Render("~/bundles/bootstrap")    
  11.     <style>    
  12.         #loading {    
  13.             position: fixed;    
  14.             top: -50%;    
  15.             left: -50%;    
  16.             width: 200%;    
  17.             height: 200%;    
  18.             background: rgba(241, 241, 241, 0.48);    
  19.             z-index: 2000;    
  20.             overflow: hidden;    
  21.         }    
  22.             #loading img {    
  23.                 position: absolute;    
  24.                 top: 0;    
  25.                 left: 0;    
  26.                 right: 0;    
  27.                 bottom: 0;    
  28.                 margin: auto;    
  29.             }    
  30.     </style>    
  31. </head>    
  32. <body>    
  33.     <div id="loading">    
  34.         <img src="~/Content/spinner.gif" />    
  35.     </div>    
  36.     <div class="navbar navbar-inverse navbar-fixed-top">    
  37.         <div class="container">    
  38.             <div class="navbar-header">    
  39.                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">    
  40.                     <span class="icon-bar"></span>    
  41.                     <span class="icon-bar"></span>    
  42.                     <span class="icon-bar"></span>    
  43.                 </button>    
  44.                 @Html.ActionLink("Amazon FBA Fees""Index""Home"new { area = "" }, new { @class = "navbar-brand" })    
  45.             </div>    
  46.             <div class="navbar-collapse collapse">    
  47.                 <ul class="nav navbar-nav">    
  48.                     <li>@Html.ActionLink("Home""Index""Home")</li>    
  49.                 </ul>    
  50.             </div>    
  51.         </div>    
  52.     </div>    
  53.     <div class="container body-content">    
  54.         @RenderBody()    
  55.         <hr />    
  56.         <footer>    
  57.             <p>© @DateTime.Now.Year - My ASP.NET Application</p>    
  58.         </footer>    
  59.     </div>    
  60.     @RenderSection("scripts", required: false)    
  61. </body>    
  62. </html>    
  63. <script>    
  64.     function showLoader() {    
  65.         $('#loading').show();    
  66.     }    
  67.     function hideLoader() {    
  68.         $('#loading').fadeOut();    
  69.     }    
  70.     function getUrl() {    
  71.         var getUrl = window.location;    
  72.         var baseUrl = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[0];    
  73.         return baseUrl;    
  74.     }    
  75.     $(document).ready(function () {    
  76.         hideLoader();    
  77.     });    
  78. </script>    
Navigate to View -> Home -> Index.cshtml.
  1. @{    
  2.     ViewBag.Title = "Home Page";    
  3. }    
  4. <div class="row" style="margin-top:5%">    
  5.     <div class="form-group col-md-4">    
  6.         <label>Enter ASIN</label>    
  7.         <input type="text" class="form-control" id="asin" />    
  8.     </div>    
  9. </div>    
  10. <div class="row">    
  11.     <div class="form-group col-md-4">    
  12.         <input type="button" class="btn btn-success" id="calculate" value="Calculate Fees" />    
  13.     </div>    
  14. </div>    
  15. <hr />    
  16. <script>    
  17.     $(document).on("click""#calculate"function () {    
  18.         if ($('#asin').val() == "") {    
  19.             alert("ASIN is required");    
  20.             $('#asin').focus();    
  21.             return;    
  22.         }    
  23.         else {    
  24.             showLoader();    
  25.             $.ajax({    
  26.                 url: getUrl() + '/Home/GetFeeEstimateData',    
  27.                 type: 'POST',    
  28.                 data: { 'ASIN': $('#asin').val() },    
  29.                 success: function (res) {    
  30.                     hideLoader();    
  31.                     if (res.result)    
  32.                         alert("FBA Fee is " + res.fee + " " + res.currency);    
  33.                     else    
  34.                         alert("Something went wrong. Please contact administrator");    
  35.                 },    
  36.                 error: function (err) {    
  37.                     hideLoader();    
  38.                     alert(err);    
  39.                 }    
  40.             });    
  41.         }    
  42.     });    
  43. </script>    
We have to add the credentials in the Web.Config file present in the root directory.
  1. <add key="ServiceURL" value="https://mws.amazonservices.com" />  
  2. <add key="SellerId" value="seller-id" />  
  3. <add key="MarketplaceID" value="marketplace-id" />  
  4. <add key="AccessKeyID" value="access-key" />  
  5. <add key="SecretKey" value="secret-key" />  
Finally, the code which makes it possible to call through the API.
  1. using MarketplaceWebServiceProducts;  
  2. using MarketplaceWebServiceProducts.Model;  
  3. using System;  
  4. using System.Configuration;  
  5. using System.IO;  
  6. using System.Web.Mvc;  
  7. using System.Xml;  
  8. namespace GetMyFeeEstimateForAmazonMWS.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         public MarketplaceWebServiceProductsConfig _productConfig;  
  13.         public MarketplaceWebServiceProducts.MarketplaceWebServiceProducts _productClient;  
  14.         public static string sellerId = "";  
  15.         public static string marketplaceID = "";  
  16.         public static string accessKeyID = "";  
  17.         public static string secretKey = "";  
  18.         public static string serviceURL = "";  
  19.         public HomeController()  
  20.         {  
  21.             sellerId = ConfigurationManager.AppSettings["SellerId"];  
  22.             marketplaceID = ConfigurationManager.AppSettings["MarketplaceID"];  
  23.             accessKeyID = ConfigurationManager.AppSettings["AccessKeyID"];  
  24.             secretKey = ConfigurationManager.AppSettings["SecretKey"];  
  25.             serviceURL = ConfigurationManager.AppSettings["ServiceURL"];  
  26.             _productConfig = new MarketplaceWebServiceProductsConfig { ServiceURL = serviceURL };  
  27.             _productClient = new MarketplaceWebServiceProductsClient("MWS""1.0", accessKeyID, secretKey, _productConfig);  
  28.         }  
  29.         public ActionResult Index()  
  30.         {  
  31.             return View();  
  32.         }  
  33.         public JsonResult GetFeeEstimateData(string ASIN)  
  34.         {  
  35.             try  
  36.             {  
  37.                 StreamWriter BaseTag;  
  38.                 var uploadRootFolder = AppDomain.CurrentDomain.BaseDirectory + "\\Reports";  
  39.                 Directory.CreateDirectory(uploadRootFolder);  
  40.                 var directoryFullPath = uploadRootFolder;  
  41.                 string targetFile = Path.Combine(directoryFullPath, "FeeEstimateResponse_" + Guid.NewGuid() + ".xml");  
  42.                 var response = InvokeGetMyFeesEstimate(ASIN);  
  43.                 BaseTag = System.IO.File.CreateText(targetFile);  
  44.                 BaseTag.Write(response.ToXML());  
  45.                 BaseTag.Close();  
  46.                 XmlDocument root = new XmlDocument();  
  47.                 root.Load(targetFile);  
  48.                 XmlNodeList elemList1 = root.GetElementsByTagName("FeeAmount");  
  49.                 var data = elemList1[3].InnerText;  
  50.                 var currency = data.Substring(0, 3);  
  51.                 var fee = data.Substring(3);  
  52.                 return Json(new { result = true, currency = currency, fee = fee }, JsonRequestBehavior.AllowGet);  
  53.             }  
  54.             catch (Exception ex)  
  55.             {  
  56.                 return Json(new { result = false }, JsonRequestBehavior.AllowGet);  
  57.             }  
  58.         }  
  59.         public GetMyFeesEstimateResponse InvokeGetMyFeesEstimate(string ASIN)  
  60.         {  
  61.             try  
  62.             {  
  63.                 GetMyFeesEstimateRequest request = new GetMyFeesEstimateRequest();  
  64.                 request.SellerId = sellerId;  
  65.                 request.MWSAuthToken = "example";  
  66.                 FeesEstimateRequestList feesEstimateRequestList = new FeesEstimateRequestList();  
  67.                 feesEstimateRequestList.FeesEstimateRequest.Add(new FeesEstimateRequest  
  68.                 {  
  69.                     MarketplaceId = marketplaceID,  
  70.                     IdType = "ASIN",  
  71.                     IdValue = ASIN,  
  72.                     PriceToEstimateFees = new PriceToEstimateFees { ListingPrice = new MoneyType { Amount = 0M, CurrencyCode = "USD" } },  
  73.                     Identifier = "request_" + Guid.NewGuid().ToString(),  
  74.                     IsAmazonFulfilled = true  
  75.                 });  
  76.                 request.FeesEstimateRequestList = feesEstimateRequestList;  
  77.                 return _productClient.GetMyFeesEstimate(request);  
  78.             }  
  79.             catch (Exception ex)  
  80.             {  
  81.                 return null;  
  82.             }  
  83.         }  
  84.     }  
  85. }  
Output
 
Amazon FBA Fee Using Amazon MWS API In C#
You can download the source code here.
 
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.


Similar Articles