You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Yann Esposito (Yogsototh) c7daceca04 removed min bound on servant version 5 years ago
example removed min bound on servant version 5 years ago
src/Servant Update for swagger2-1.1.1 5 years ago
test Add custom schema example for Hackage API example 5 years ago
.gitignore Ignore swagger.json 5 years ago Bump version and add changelog 5 years ago
LICENSE Add BSD3 LICENSE content 6 years ago Expand README on trying out and contributing 5 years ago
Setup.hs Added setup.hs and source repo info 6 years ago
servant-swagger.cabal removed min bound on servant version 5 years ago
stack.yaml Fix to make it work with the current unerleased servant 0.5 version 5 years ago


This project converts servant APIs into Swagger 2.0 conforming JSON.

Given the following servant API, servant-swagger generates the following json.


{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE TypeOperators              #-}
module Main where

import Control.Lens
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as BL8
import Data.Proxy
import Data.Swagger
import GHC.Generics
import Servant
import Servant.Swagger

-- Types
data Todo = Todo
  { created     :: Int
  , description :: String
  } deriving (Show, Eq, Generic)

instance ToJSON Todo

newtype TodoId = TodoId String deriving (FromText, Generic)

-- API
type API = "todo" :> Capture "id" TodoId :> Get '[JSON] Todo

-- Swagger Doc
swagDoc :: Swagger
swagDoc = toSwagger (Proxy :: Proxy API)
  & info.infoTitle   .~ "Todo API"
  & info.infoVersion .~ "1.0"
  & info.infoDescription ?~ "This is an API that tests servant-swagger support for a Todo"
  & info.infoLicense ?~ License "MIT" (Just (URL ""))

-- Documentation and annotations
instance ToParamSchema TodoId

instance ToSchema Todo where
  declareNamedSchema proxy = do
    (name, schema) <- genericDeclareNamedSchema defaultSchemaOptions proxy
    return (name, schema
      & schemaDescription ?~ "This is some real Todo right here"
      & schemaExample ?~ toJSON (Todo 100 "get milk"))

-- Main, create swaggger.json
main :: IO ()
main = BL8.writeFile "swagger.json" (encode swagDoc)


      "title":"Todo API",
      "description":"This is an API that tests servant-swagger support for a Todo"
            "description":"get milk"
         "description":"This is some real Todo right here",
                  "description":"id not found"

Try it out

All generated swagger specifications can be interactively viewed on Swagger Editor.

Ready-to-use specification can be served as JSON and interactive API documentation can be displayed using Swagger UI.

Many Swagger tools, including server and client code generation for many languages, can be found on Swagger’s Tools and Integrations page.


  • Q: How is this project different from the swagger package on hackage ?
    • A: This package is based on the latest Swagger 2.0 API


We are happy to receive bug reports, fixes, documentation enhancements, and other improvements.

Please report bugs via the github issue tracker.