liveBook · Manning

Endpoint

As explained in Learn Haskell by Example

An API endpoint is a network-accessible operation identified by a route and HTTP method at which an API accepts requests.

An API endpoint is a network-accessible operation described by a route, an HTTP method, and request or response data. In Servant, endpoints are declared together as one API type. section 17.1.1

Build endpoint definitions by combining:

  • Use (:>) to combine path information with a request description, such as a GET operation. section 17.1.1
  • Use (:<|>) to combine multiple endpoint descriptions into one API. section 17.1.1
  • Specify response formats such as JSON and result types such as SomeType or [TodoEntry]. section 17.1.1
  • Require request bodies with ReqBody; Servant rejects bodies that do not match the declared specification. section 17.1.1
  • Add dynamic route values with Capture, supporting endpoints such as /select/{id} or /toggle/{id}. 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

A complete todo API combines listing, creation, and update endpoints, including JSON bodies, response types, and a captured integer identifier. section 17.1.1

How Learn Haskell by Example covers it

How deep it goes
Specifies endpoint composition and validation, but defers detailed explanation of DataKinds and TypeOperators.
Examples
Uses simple GET routes, JSON responses, POST bodies, and a todo API with entries, addition, and toggling.
What it assumes
Assumes basic Haskell module knowledge while introducing the endpoint operators needed for Servant declarations.
What you can do after
Build it from scratch by declaring Servant endpoints as composable Haskell types.
How it connects
Connects routes with HTTP verbs, JSON, request bodies, captured parameters, and typed API composition.

Also explained in

Compare how 26 books explain endpoint →

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.