Prefix many ZIO-HTTP Endpoint Declarations with "api"
14:04 01 Mar 2026

I use the ZIO-http declarative Endpoints, and I love that docs can be generated from them. I want to add a prefix ("api") to all of the endpoints, but I don't want to go through each Endpoint declaration and manually add "api" to all the paths.

I tried to find a "prefix" or "nest" method that could be used on the entire route collection, but the closest method I could find was the "literal" method. When I add literal("api") to the implemented routes collection and then access /api/activities, I get an unexpected error:

Codec Error  
  
There was an error en-/decoding the request/response  
  
MalformedPath  
  
Malformed path /api/activities failed to decode using /activities: Expected path segment "activities" but found end of path  

I'm struggling to figure out what I've missed or screwed up in the following code snippet:

val getActivities = Endpoint((GET / "activities"))  
  .out[CollectionResponse[ActivitySummary]]  
  .outError[ErrorResponse](Status.InternalServerError)  
  .examplesOut(("Response", exampleActivitySummaryResponse()))  
  
val routes = Routes(  
  getActivities.implement(_ => handleGetAll())  
)  
  
val apiRoutes = literal("api") / routes  

As soon as literal("api") is added, I get the error I mentioned, and not using literal("api") works as expected and shows data. Any suggestions or ideas of how to fix this?

Dependency Versions

  • zio-http: 3.8.1
  • zio: 2.1.22
  • Scala: 3.8.1
scala endpoint zio zio-http