call api from csharp

If you want to call API and submit request there are mainly two methods you can do this

1) By submitting parameters via URL
2) By submitting parameters via xml

In both cases API alaways returns either parameters in query string which you have to collect , or it returns xml response.

Calling API in C# and Calling API in is same the only difference is webform or winform , the task can be accomplished in both type of apps.


In this method first build your url by making a method and pass that url to this method , your url might be like this

string url = “” +your_order_ID + “&CustomerID=” + yourCustomerID;

I am just giving example there are two types of parameters you must supply in url , these parameters list is given in manual of API without manual its very difficult to submit request , Optional parameters and mandatory parameters.
You can skip option parameters (Required Yes/No in manual) but if you miss mandatory parameters then request will be failed.

Below method take url with parameters to submit and return full response from API in name value collection in string format which you can easily use to store either in your db or in your flat text file .

public static Response MakeRequest(string requestUrl)
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
if (response.StatusCode != HttpStatusCode.OK)

StreamReader reader = new StreamReader(resp.GetResponseStream());
string result = reader.ReadToEnd();



Below method take xml request sample to upload to API at some url with some API credential like userid , password , application access key or token . Then return xml from API in string format , later you can write down it on file using xmlwriter class of .net or streamwriter class of .net.

public string MakeRequest(string ReqXML, string url, string RequestType)

try {

string ResponseXML = null;
WebClient Client = new WebClient();
byte[] response = null;
NetworkCredential creds = new NetworkCredential(“youremailgoeshere”, “7e26b3eb4e7044a895f5c352f4e67466”);

// Will Post Data to Create Zone API

Client.Headers.Add(“Content-Type”, “application/xml”);
Client.Credentials = creds;

if (RequestType == “POST”) {
byte[] request = Encoding.ASCII.GetBytes(ReqXML);
response = Client.UploadData(url, RequestType, request);

} else if (RequestType == “GET”) {
response = Client.DownloadData(url);
} else if (RequestType == “PUT”) {
response = Client.DownloadData(url);
} else if (RequestType == “DELETE”) {
byte[] request = Encoding.ASCII.GetBytes(ReqXML);
response = Client.UploadData(url, RequestType, request);

ResponseXML = Encoding.ASCII.GetString(response);

return ResponseXML;

} catch (WebException wex) {
StatusMessage = wex.Message;
} catch (Exception ex) {
return ex.Message;


Thank you

One thought on “call api from csharp”

  1. A great article….a very much helpful amongst all the articles that i found in googling about this topic.

Leave a Reply

Your email address will not be published. Required fields are marked *