Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the 'api_key' and how do I use it correctly

I'm fairly new to restful services, and I've just implemented the test code to get a ServiceStack restful service going with the Swagger plugin working as well, which leads me to my question...

inside swagger-ui/index.html there is a field for 'api_key'. I know the variable name is umm... variable, and I can set it too whatever I like, but I'm slightly confused what it's used for and whether I should be making use of it.

Also, if I do use it, how does servicestack present that value to me on the server side?

Here is the Test Service I've got up and running from the documentation...

    [Api("Hello Web Services")]    
    [Route("/Hello", Summary = @"Noel's ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes")]
    [Route("/Hello/{name}",   Summary = @"N031'5 ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes", Verbs="GET,POST" )] 
    public class Hello
    {
        [ApiMember(Name = "Name", Description = "This is a description", ParameterType = "path", DataType = "string", Verb="GET,POST")]
        public string Name { get; set; }
    }

    public class HelloResponse
    {
        public string Result { get; set; }
    }


    public class HelloService : Service
    {
        public object Any(Hello request)
        {
            return new HelloResponse { Result = "Hello, " + request.Name };
        }
    }
like image 886
Ninjanoel Avatar asked Mar 22 '23 19:03

Ninjanoel


1 Answers

to answer my own follow up request of Esker, here is how to use the API Key thingy...

public class HelloService : Service
{        
    public object Any(Hello request)        
    {
        string api_key = this.Request.Headers["api_key"];            
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
} 

but also required is some extra javascript to include it in the header like so (inside swagger-ui/index.html)...

   $(function () {
        $.ajaxSetup({
            beforeSend: function (jqXHR, settings) {
                jqXHR.setRequestHeader("api_key", $("#input_apiKey").val());
            }
        });
    });

which I found in an answer in this question...

How to get Swagger to send API key as a http instead of in the URL

like image 173
Ninjanoel Avatar answered Mar 31 '23 23:03

Ninjanoel