Fork me on GitHub

@mtkp you need to app the value of api (or the defoned var with defapi) into a ring-adapter (like http-kit) and start it. Here's an example:


I think there should be a minimalistic reloaded repl -enabled template for c-api. Maybe with Mount to keep it newbie-friendly... or an example project at least.


hey, new to compojure-api and swagger but trying to get it set up for my app. got it working yesterday (on 1.1.10) but now trying to add some token-based auth and not really sure what i'm doing. i've added :data {:securityDefinitions {:api_key {:type "apiKey" :name "token" :in "header"}} to my swagger map but not seeing any differences when i open up my swagger page. is there supposed to be a ui element added for me to add my api token to my headers somewhere? am i supposed to do something else?


tried doing it through :header-params which works but it doesn't look like i can provide it for an entire context, only individual endpoints, and i don't want to add that boilerplate to every endpoint that requires auth


to be clear: my auth works, i just want a way to still test my authenticated routes through swagger


Hey, I am using compojure-api for the data-driven resource style endpoints. However I can’t seem to figure out how to mark a query-param as optional. I have tried using s/maybe but that doesn’t seem to work. Any ideas?


@kasuko you should use s/optional-key for it.


(def handler
    {:summary "increments query-param x, defaulting to 1"
     :parameters {:query-params {(s/optional-key :x) Long}}
     :responses {200 {:schema Long}}
     :handler (fn [request]
                (ok (-> request :query-params :x (or 0) inc)))}))

(handler {:request-method :get, :query-params {:x "41"}})
; {:status 200, :headers {}, :body 42, :compojure.api.meta/serializable? true}
(handler {:request-method :get, :query-params {}})
; {:status 200, :headers {}, :body 1, :compojure.api.meta/serializable? true}


ah I see, my bad I was trying to make the value optional but they key to the solution was the key 😛


@csapp there should be a top-bar authentication link, which open a modal where you can enter the api-key


also, you can mount authentication middleware at any level of your routing tree via the :middleware option. either to api (effects all routes), to context (bunch of routes) or to endpoint (just that one. You don’t have to add the :header-param, the top-level swagger :securityDefinitions should effect all. e.g. when it’s given via the ui, it’s passed to all requests.


the actual enforcement of the api-key could be a middleware. Sadly, currently the basic-auth, token-auth & others are separate ring-libs, so you need to collect them together.


If someone would have extra time, would like to see them prepackaged with compojure-api as they are commonly used.