liveBook · Manning

Contract

As explained in Learn Haskell by Example

An application programming interface is a defined set of operations and data representations through which software components interact.

An application programming interface (API) defines how software components communicate. A web API exposes named HTTP endpoints, their request methods, and the data they accept or return. A todo-list API, for example, sends JSON between a server and a command-line client. section 16.1 section 17.1

A CRUD API organizes operations for creating, reading, updating, and deleting data. The same pattern can support questionnaire services, appointment planners, booking platforms, and store backends. section 16.1

A typesafe API can specify these details before its server implementation:

  • Routes and HTTP verbs, such as GET, POST, PATCH, and DELETE. section 17.1.1
  • Response formats and types, such as a JSON-encoded value or no-content response. section 17.1.1
  • Request bodies and captured path values, with invalid request bodies rejected automatically. section 17.1.1
  • Multiple endpoints combined into one API definition. section 17.1.1

A todo-list API

A single API type can declare an entries route that returns todo entries, an add route that accepts a JSON TodoEntry and returns its identifier, and a toggle route that captures an integer identifier. In Servant, :> extends one endpoint description, while :<|> joins endpoint descriptions. section 17.1.1

Listing 17.1 A type for the todo list API server
{-# LANGUAGE TypeOperators #-}     #1
{-# LANGUAGE DataKinds #-}        #1
module Todos.Api where

import Servant.API
import Todos.Types

type TodoApi =
  "entries" :> Get '[JSON] [TodoEntry]        #2
    :<|> "add" :> ReqBody '[JSON] TodoEntry :>
 PostCreated '[JSON] Int                       #3
    :<|> "toggle" :> Capture "id" Int :> PatchNoContent    #4

This declaration collects the read, add, and toggle operations of a todo-list service in one type. section 17.1.1

Keeping the machine-facing API separate from a human-facing HTML page can simplify generated clients: a middleware wrapper can handle the root page while other requests continue to the API server. Servant can also derive client functions from the API definition. section 17.2.2 section 17.4

How Learn Haskell by Example covers it

How deep it goes
Builds typed endpoint declarations and middleware decisions, while deferring explanations of DataKinds and TypeOperators.
Examples
Uses a todo-list service with entries, add, and toggle endpoints plus an HTML root page.
What it assumes
Assumes JSON encoding and introduces network-facing API definition without requiring low-level network programming.
What you can do after
Build it from scratch as a typed Servant API with validated requests and derived client functions.
How it connects
Connects HTTP, JSON, CRUD, Servant, middleware, WAI Application, and generated clients.

Also explained in

Compare how 44 books explain contract →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.