Part 1: Your First Route
In this part, we create an empty sbt project, write a server with one route, GET /health, and call it with curl. A health route is the smallest useful endpoint: it tells a caller that the server is up.
1. Create the Project
A ZIO HTTP application is an ordinary sbt project. Create a folder named books-api with this layout:
books-api/
├── build.sbt
├── project/
│ └── build.properties
└── src/
└── main/
└── scala/
└── BooksApp.scala
The project/build.properties file pins the sbt version:
sbt.version=1.13.1
The build.sbt file sets the Scala version and adds ZIO HTTP, which also brings in ZIO and ZIO Schema:
scalaVersion := "2.13.18"
// ZIO HTTP: HTTP server and client (JVM)
libraryDependencies += "dev.zio" %% "zio-http" % "3.12.0"
2. Write the First Route
Now let's write the server. Put this code in src/main/scala/BooksApp.scala:
import zio._
import zio.http._
object BooksApp extends ZIOAppDefault {
val routes: Routes[Any, Response] =
Routes(
Method.GET / "health" -> handler(Response.text("OK")),
)
def run = Server.serve(routes).provide(Server.default)
}
Each line has one job:
import zio._andimport zio.http._bring in ZIO and the whole ZIO HTTP API.object BooksApp extends ZIOAppDefaultmakesBooksAppa runnable ZIO program; ZIO calls itsZIOAppDefault#runmethod at startup.Method.GET / "health"is a route pattern: it matchesGETrequests whose path is/health.handler(Response.text("OK"))is the handler for that pattern; it answers every matching request with a200 OKresponse whose body is the textOK.->joins the pattern and the handler into one route.Routes(...)collects our routes. Its type,Routes[Any, Response], says the routes need nothing from the environment (Any) and that every failure is already aResponse.Server.serve(routes)starts the server with these routes and keeps it running until the program stops.provide(Server.default)supplies the server itself, with the default configuration, which listens on port 8080.
Server.serve only accepts routes whose error type is Response (or Nothing). That rule guarantees that every request gets an HTTP answer. Our routes cannot fail yet, so Response is all we need for now; Part 5 shows how to turn real failures into responses.
3. Run It
Let's start the server from the books-api folder:
sbt run
After sbt compiles the code, ZIO HTTP logs two lines (shortened here) that confirm the server is listening:
timestamp=... level=INFO thread=#zio-fiber-... message="Starting the server..." location=BooksApp.run ...
timestamp=... level=INFO thread=#zio-fiber-... message="Server started" location=BooksApp.run ...
In a second terminal, call the health route. The -i flag makes curl print the status line and the headers too:
curl -i localhost:8080/health
The server answers 200 OK with the body OK (we leave the date header out of every response shown in this tutorial):
HTTP/1.1 200 OK
content-type: text/plain
content-length: 2
OK
Notice that Response.text also sets the content-type header for us. Now try a path we have not defined yet:
curl -i localhost:8080/books
No route matches /books, so ZIO HTTP answers 404 Not Found with an empty body, without any code from us:
HTTP/1.1 404 Not Found
content-length: 0
Stop the server with Ctrl+C when you are done.
Recap
In this part, we created a ZIO HTTP project and learned the three pieces every ZIO HTTP server has: a route pattern (Method.GET / "health"), a handler that builds the response, and Server.serve, which runs the routes on port 8080.
In Part 2: Path and Query Parameters, we add routes that list books and read values from the request's path and query string.