Implementing Amazon API Signed Requests Helper in ASP.Net and C#


'Download Download Sample


As many of you know, Starting August 15, 2009, all Amazon API requests must be authenticated using signatures.There is a whole bunch of documentation on Amazon website that describes how to authenticate requests. Visit following URL for more information.

But as a newbie to Amazon APIs it was lot more difficult for me to build a working sample in ASP.Net and C#. After hours of search, I came across following article that helped me build my first successful sample. This article provided me a helper class in VB that can be used to generate a signed URL. I had to convert it in C#.


Download my sample code from above link and open it with Visual Studio 2005 or 2008 as a website. I have used .Net Framework 2.0 for this sample. But it should work fine with version 3.5 as well.

Most of the sample code is self explanatory. Comments are written inline with source code which should help you get started.

Following are key notes for this sample to work.

Go to Default.aspx.cs file and change your access key and secret key in following lines

private const string MY_AWS_ACCESS_KEY_ID = '<Enter />';
private const string MY_AWS_SECRET_KEY = '<Enter />';

If you are behind corporate firewall or proxy, you may need to authenticate HTTP requests. For that go to Web.config file and enter your windows user name and password in app.Settings.

<add key="WindowsUserName" value="''/" />
<add key="WindowsPassword" value="" />

Source Code Explanation

Most important part in this sample are following lines which are used to generate signed URL.

//Use SignedRequesthelper class to generate signed request.

SignedRequestHelper helper = new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION);

IDictionary requestParams = new Dictionary();

requestParams['Service'] = 'AWSECommerceService';

//Leave following line commented if you want to use latest version of Amazon API. You can uncomment this line to use a specific version.
//requestParams['Version'] = '2009-03-31';

requestParams['Operation'] = 'ItemSearch';
requestParams['SearchIndex'] = selectCategories.Value;
requestParams['Keywords'] = txtSearch.Text;

//Get signed URL in a variable
string requestUrl = helper.Sign(requestParams);

As you can see requestParams is an array which holds all the parameters to be passed along with request url.

Once you have signed URL ready, you can use WebRequest class to get response from Amazon web service.

DataSet GetData(string signedurl)
   //Create a request object using signed URL.
   WebRequest request = HttpWebRequest.Create(signedurl);
   //Get response in a stream
   Stream responseStream = request.GetResponse().GetResponseStream();

   DataSet DS = new DataSet();
   //Read returned resonpse stream into a dataset.
   //Note: You can also use XMLDocument element here to read response.

   return DS;

As you can see in above code, we get response in XML format which is then read into a Dataset. Dataset stores it as Tables.

Note that I am not referencing Amazon Web Service in this project. It could have been lot more easier to just use web methods to get Items. But unfotunately I could not find any way of signing a request with webservice.

Once we have returned data into dataset we can just bind these tables with Grid View or whatever way you want to use it.

Final Notes

In my sample I have just implemented search service from amazon. You can also implement Item Lookup in similar way. You just need to change request parameters in my sample. Feel free to modify it as you desire.

Posted: 8 years 7 months ago by Levitikon #209
Levitikon's Avatar
Found and fixed a big bug.
In your PercentEncodeRfc3986 method, you need to set str = str.Replace(...).
The Replace method just returns a string. You need to set it back to itself for all the replace methods to stick. Also, all "+" need to be replaced with "%20"
Posted: 8 years 7 months ago by manish123 #200
manish123's Avatar
Hi Shabdar,
Your code is working for me.
I have worked 3.5 and used repeter to bind amazon data.
Can you provide me code so that i can handle price, image of amazon product.
I am new for amazon and that's very urject for me.
Please help me to integrate amazon store.

Posted: 9 years 3 months ago by shabdar #87
shabdar's Avatar
Do you get this error consistencly for a particular ISBN or it just appears randomly? I am aware of such error when you try to access Amazon Web Services too frequently within short period of time. But if it happens just for particular ISBN means it is restricted.
Posted: 9 years 3 months ago by #85's Avatar

This code works great but I get sometimes 403 error (Forbidden) when sending some request like
for example when I'm querying Amazon with the ISBN number of a book (ex: ISBN: 9780813343747).
Any thoughts on the probable causes of the problem ?