The servant-elm package

[Tags:bsd3, library, test]

Please see

[Skip to Readme]


Change log
Dependencies base (>=4.7 && <5), elm-export (>=0.5), lens, servant (>=0.8), servant-elm, servant-foreign (>=0.8), text, wl-pprint-text [details]
License BSD3
Copyright 2015-2016 Matt Bray
Author Matt Bray
Category Web
Home page
Source repository head: git clone
Uploaded Sun Feb 12 20:58:55 UTC 2017 by mattjbray
Distributions LTSHaskell:, NixOS:, Stackage:, Tumbleweed:
Downloads 367 total (21 in the last 30 days)
0 []
Status Docs available [build log]
Last success reported on 2017-02-12 [all 1 reports]
Hackage Matrix CI




examplesBuild the example programs.DisabledAutomatic
integrationBuild the integration tests (requires an Elm installation).DisabledAutomatic

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Maintainer's Corner

For package maintainers and hackage trustees

Readme for servant-elm

Readme for servant-elm-

Servant Elm

Build Status

Generate Elm functions to query your Servant API!

Elm type generation coutesy of krisajenkins/elm-export.


Servant Elm is available on Hackage.


First, some language pragmas and imports.

{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators     #-}

import           Elm          (Spec (Spec), specsToDir, toElmDecoderSource,
import           GHC.Generics (Generic)
import           Servant.API  ((:>), Capture, Get, JSON)
import           Servant.Elm  (ElmType, Proxy (Proxy), defElmImports,

We have some Haskell-defined types and our Servant API.

data Book = Book
    { name :: String
    } deriving (Generic)

instance ElmType Book

type BooksApi = "books" :> Capture "bookId" Int :> Get '[JSON] Book

Now we can generate Elm functions to query the API:

spec :: Spec
spec = Spec ["Generated", "MyApi"]
             : toElmTypeSource    (Proxy :: Proxy Book)
             : toElmDecoderSource (Proxy :: Proxy Book)
             : generateElmForAPI  (Proxy :: Proxy BooksApi))

main :: IO ()
main = specsToDir [spec] "my-elm-dir"

Let's save this as example.hs and run it:

$ stack runghc example.hs
Writing: my-elm-dir/Generated/MyApi.elm

Here's what was generated:

module Generated.MyApi exposing (..)

import Json.Decode exposing (..)
import Json.Decode.Pipeline exposing (..)
import Json.Encode
import Http
import String

type alias Book =
    { name : String

decodeBook : Decoder Book
decodeBook =
    decode Book
        |> required "name" string

getBooksByBookId : Int -> Http.Request (Book)
getBooksByBookId capture_bookId =
        { method =
        , headers =
        , url =
            String.join "/"
                [ ""
                , "books"
                , capture_bookId |> toString |> Http.encodeUri
        , body =
        , expect =
            Http.expectJson decodeBook
        , timeout =
        , withCredentials =

See examples for a complete usage example, or take a look at mattjbray/servant-elm-example-app for an example project using this library.


$ git clone
$ cd servant-elm
$ stack test
$ stack test --flag servant-elm:integration

To build all examples:

$ make examples

To run an example:

$ cd examples/e2e-tests
$ elm-reactor
# Open http://localhost:8000/elm/Main.elm