Skip to content

External API Client

In this example, you’ll build a Zuno microservice that fetches data from an external API (e.g. JSONPlaceholder) and returns it to the client. This is useful for:

  • Proxying third-party APIs
  • Aggregating data from multiple sources
  • Building microservices that depend on external systems

This example uses a lightweight HTTP client library. You can use any C++ HTTP client you prefer (e.g. cpr, Boost.Beast, or a custom wrapper around libcurl). For simplicity, we’ll assume you’re using cpr via FetchContent.


external-api-client/
├── CMakeLists.txt
└── main.cpp

cmake_minimum_required(VERSION 3.16)
project(external_api_client)
set(CMAKE_CXX_STANDARD 20)
include(FetchContent)
FetchContent_Declare(
zuno
GIT_REPOSITORY https://github.com/ZunoFramework/zuno.git
GIT_TAG main
)
FetchContent_Declare(
cpr
GIT_REPOSITORY https://github.com/libcpr/cpr.git
GIT_TAG 1.10.4
)
FetchContent_MakeAvailable(zuno cpr)
add_executable(external-api main.cpp)
target_link_libraries(external-api PRIVATE zuno cpr::cpr)

#include <zuno/zuno.hpp>;
#include <cpr/cpr.h>;
int main() {
zuno::App app;
app.get("/posts/:id", [](auto& req, auto& res) {
std::string id = req.params["id"];
auto response = cpr::Get(cpr::Url{"https://jsonplaceholder.typicode.com/posts/" + id});
if (response.status_code == 200) {
res.status(200).send(response.text);
} else {
res.status(502).send("Failed to fetch external data");
}
});
app.listen(3000);
}

Terminal window
curl http://localhost:3000/posts/1

You should receive a JSON response from the external API, proxied through your Zuno server.



Zuno makes it easy to build microservices that talk to the outside world—securely and efficiently.