Skip to content

Modular REST API

In this example, we’ll build a small REST API using Zuno with a modular structure. You’ll learn how to:

  • Organize routes in separate files
  • Return JSON responses
  • Use FetchContent to fetch Zuno automatically

rest-api/
β”œβ”€β”€ CMakeLists.txt
β”œβ”€β”€ src/
β”‚ β”œβ”€β”€ main.cpp
β”‚ └── routes/
β”‚ └── users.cpp
└── include/
└── routes/
└── users.hpp

cmake_minimum_required(VERSION 3.16)
project(rest_api)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(
zuno
GIT_REPOSITORY https://github.com/ZunoFramework/zuno.git
GIT_TAG main
)
FetchContent_MakeAvailable(zuno)
include_directories(include)
add_executable(rest-api
src/main.cpp
src/routes/users.cpp
)
target_link_libraries(rest-api PRIVATE zuno)

#include <zuno/zuno.hpp>
#include "routes/users.hpp"
int main() {
zuno::App app;
registerUserRoutes(app);
app.listen(3000);
}

#include &lt;zuno/zuno.hpp&gt;
#include "routes/users.hpp"
void registerUserRoutes(zuno::App& app) {
app.get("/users", [](auto& req, auto& res) {
res.json({
{ "id", 1 },
{ "name", "Alice" }
});
});
app.get("/users/:id", [](auto& req, auto& res) {
std::string id = req.params["id"];
res.json({
{ "id", id },
{ "name", "User " + id }
});
});
}

#pragma once
#include <zuno/zuno.hpp>
void registerUserRoutes(zuno::App& app);

Terminal window
cmake -B build
cmake --build build
./build/rest-api

Then test your API:

  • GET /users β†’ returns a list of users
  • GET /users/42 β†’ returns user with ID 42

Try adding:


Zuno makes modular C++ web APIs feel naturalβ€”without sacrificing performance or clarity.