Introduction: Why Write a REST API in C++?
C++ is traditionally associated with system programming, games, and high-load applications. However, modern C++ (starting from C++11/17/20 standards) provides all the necessary tools for rapid web service development. Writing a REST API in C++ can be justified when maximum performance, low memory consumption, or integration with existing C++ code (e.g., computer vision libraries or numerical calculations) is required.
In this guide, we will create a full-fledged REST API in C++ in 30 minutes. For this, we will use the lightweight and elegant library Crow. It requires no complex configuration, supports routing, JSON (via json.hpp), and middleware. By the end of the article, you will have a working server with three endpoints.
Step 1: Setting Up the Environment and Installing Crow
For this to work, we need a compiler with C++17 support (g++ 7+, clang 5+, or MSVC 2017+) and the CMake build system. Crow is a "header-only" library, which simplifies installation: just download one header file.
Installation
- Download the latest version of Crow from GitHub (or use
git clone). - Copy the
includefolder into your project. - Install
nlohmann/json(also header-only) for working with JSON.
Project Structure
my_rest_api/├── CMakeLists.txt├── main.cpp└── include/ ├── crow.h └── json.hppCMakeLists.txt
cmake_minimum_required(VERSION 3.10)project(my_rest_api)
set(CMAKE_CXX_STANDARD 17)
include_directories(include)
add_executable(my_rest_api main.cpp)target_link_libraries(my_rest_api pthread)Build: mkdir build && cd build && cmake .. && make
Step 2: Creating a Basic Server and Routes
Crow uses a simple DSL for defining routes. Let's create a server that listens on port 8080 and handles GET and POST requests.
Basic Server Code
#include "crow.h"#include <string>
int main() { crow::SimpleApp app;
// Simple GET route CROW_ROUTE(app, "/")([](){ return "Welcome to the REST API in C++!"; });
// GET with path parameter CROW_ROUTE(app, "/user/<int>")([](int user_id){ return crow::response("User ID: " + std::to_string(user_id)); });
app.port(8080).multithreaded().run();}This code creates two endpoints. crow::SimpleApp is the main application class. The multithreaded() method enables request handling in a thread pool. Start the server and check http://localhost:8080/ in your browser.
Parameter Types in Routes
Crow supports the following parameter types in the path:
<int>— integer<string>— string<path>— path remainder (e.g.,/files/<path>)<float>— floating-point number
Step 3: Working with JSON and POST Requests
A real REST API must be able to accept and return JSON. Let's include nlohmann/json and implement CRUD-like operations.
Example: Simple In-Memory "Database"
#include "crow.h"#include "json.hpp"#include <unordered_map>#include <memory>
using json = nlohmann::json;
struct Task { int id; std::string title; bool completed;};
std::unordered_map<int, std::shared_ptr<Task>> tasks;int next_id = 1;
int main() { crow::SimpleApp app;
// GET /tasks — get all tasks CROW_RO