Skip to main content

Build Your First REST API with ZIO HTTP

Welcome to the ZIO HTTP REST API tutorial! This tutorial is for Scala developers who have never used ZIO HTTP. You should be comfortable with basic Scala (case classes, List, Option, pattern matching) and know what an HTTP request and a status code are. You don't need any prior experience with ZIO HTTP, and the little ZIO the tutorial uses is explained where it appears.

Learning Objectives​

By the end of this tutorial, you will understand:

  • How a ZIO HTTP server turns a request into a response with Routes and handlers
  • How to read path parameters and query parameters from a request
  • How to send and receive JSON with ZIO Schema
  • How to share state between requests with a service provided by a ZLayer
  • How to turn failures into clear error responses, and how to test your routes without starting a server

What We Build​

Over six short parts, we build a small REST API for a collection of books. When it is finished, it answers these requests:

RequestResponse
GET /health200 OK with the text OK
GET /books200 OK with all books as JSON
GET /books?author=...200 OK with the books by that author
GET /books?limit=2200 OK with at most two books
GET /books/1200 OK with book 1 as JSON, or 404 Not Found
POST /books201 Created with the new book, or 400 Bad Request

Each part adds one idea to the same file, BooksApp.scala, and ends with a server you can start and call with curl:

  1. Your First Route: create the project, write a GET /health route and run the server.
  2. Path and Query Parameters: list books, filter them, and look one up by its id.
  3. JSON Bodies: answer with JSON and read a new book from a JSON request body.
  4. A Books Service: keep the books in a service, provided with a ZLayer, so new books are saved.
  5. Error Responses: answer bad input and unknown books with clear JSON errors.
  6. Your First Test: test the routes with ZIO Test, without starting a server.

We recommend reading from top to bottom, because each part builds on the previous one.

Background​

ZIO HTTP models a server as a function: a request goes in, a response comes out. We describe that function with two building blocks. A route pairs a pattern, such as "a GET request for /books/<some number>", with a handler, the code that builds the response. A Routes value collects all our routes; for each request, ZIO HTTP finds the route whose pattern matches and runs its handler. When no pattern matches, the server answers 404 Not Found on its own.

The path of one request through the server we build looks like this:

curl GET /books/2
│
▼
┌───────────────┐ ┌──────────────────────────────────────────────┐
│ Server │ ───► │ Routes │
│ (port 8080) │ │ GET /health ─► handler │
└───────────────┘ │ GET /books ─► handler │
▲ │ GET /books/{id} ◄── matches, id = 2 │
│ │ POST /books ─► handler │
│ └───────────────────┬──────────────────────────┘
│ │ runs the handler
│ ▼
└──────────── Response: 200 OK {"id":2,"title":"Neuromancer",...}

Because a handler is an ordinary value that produces a response, we can also call the routes directly from a test, with no server and no network. Part 6 relies on this.

Prerequisites​

To follow along, you need JDK 17 or later (see Installation), sbt and curl. Part 1 shows how to create the project from an empty folder.

The finished code for every part is in the zio-http-example-rest-api-tutorial project, one package per part, if you want to compare your code with ours or skip ahead.

Let's start with Part 1: Your First Route.