liveBook · Manning

Servant

As explained in Functional Design and Architecture: Examples in Haskell

servant-client is a Haskell library that derives client functions for making HTTP requests from Servant API specifications.

Servant provides a typed, declarative way to describe HTTP APIs in Haskell. It fits an architecture where command-line business logic remains channel-agnostic while HTTP clients and servers are added through dependency injection. section 14.1 section 14.1.1

A single API type describes routes, request payloads, content formats, HTTP methods, and response types. The same schema can drive both a typed HTTP server and a compatible client. section 14.1.1

A practical Servant integration separates responsibilities:

  • Declare endpoints with type-level combinators such as routes, request bodies, JSON formats, and POST responses. section 14.1.1
  • Derive client functions from the API type; supplied payloads produce ClientM actions that represent HTTP calls. section 14.1.3
  • Hide library access behind application interfaces and interpret those operations within the surrounding framework. section 14.1.3

AstroAPI client

An AstroAPI can expose meteor and asteroid POST routes. Applying Servant's client derivation yields meteor :: MeteorTemplate -> ClientM MeteorId and asteroid :: AsteroidTemplate -> ClientM AsteroidId; application code then executes these actions through a framework operation and maps ClientError into a domain error. section 14.1.1 section 14.1.3

Figure 14.1 The Servant web server and the HTTP model

Use the architecture diagram to see how the shared HTTP model connects the Servant client, server, and application framework. section 14.1

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains type-level API schemas, generated client actions, and framework integration, but leaves uncommon requirements such as streaming difficult.
Examples
Uses meteor and asteroid POST endpoints, JSON payload types, generated ClientM functions, and reporting-error conversion.
What you can do after
Build it from scratch: define a shared API type and derive client operations for framework-managed HTTP requests.
How it connects
Connects embedded domain-specific languages, type-level programming, HTTP clients, HTTP servers, dependency injection, and free monadic interfaces.

Also explained in

Compare how 2 books explain Servant →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.