Real
Real

Reputation: 11

How do i call an API in c# with a api key

So i´ve recently tried working with an api for the first time and i really don´t know what to do with the this api: https://skinbaron.de/misc/apidoc/ .I have already looked tutorials on how to call an api in c# but i still don´t really get what i have to do in my specific case. I´ve tried this: How do I make calls to a REST api using C#?

This question might seem stupid to people that know how to work with stuff like this but i have no expierence with api´s so far.

The exact code i´ve tried:

    private const string URL = "https://api.skinbaron.de/";
    private static string urlParameters = "?api_key=123"; // I have replaced the "123" with my apikey

    static void Main(string[] args)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);

        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync(urlParameters).Result;  
        if (response.IsSuccessStatusCode)
        {

            // do something
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }
        client.Dispose();
    }

Upvotes: 1

Views: 10972

Answers (2)

Hammad Shabbir
Hammad Shabbir

Reputation: 731

You can use Rest sharp. Its really easy to use. This is an third party library used in c# to manage API's. No need to bother with any of the other details used to call API's.

var client = new RestClient("https://api.skinbaron.de/GetBalance");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Accept", "application/json");
request.AddParameter("apikey", "123");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
//Rest of the stuff

Upvotes: 1

Dave Packard
Dave Packard

Reputation: 1

It looks like they're expecting you to POST rather than GET, and to pass a JSON object in the body of your POST, with "apikey" as a property on the JSON object.

Normally, in C# you would create a model class that you would then serialize for your post, but if all you have to post is the apikey I would just serialize a Dictionary object with your apikey as the only member of the collection.

For instance, I think this code might do what you want.

    private const string URL = "https://api.skinbaron.de/";
    private static string urlParameters = "?api_key=123"; // I have replaced the "123" with my apikey
    private static string apiKey = "";

    static void Main(string[] args)
    {
        using (var webClient = new WebClient()) {
            webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");

            var postDictionary = new Dictionary<string, string>() {
                {"apikey", apiKey}
            };

            var responseBody = webClient.UploadString(URL, JsonConvert.SerializeObject(postDictionary));
        }
    }

Upvotes: 0

Related Questions