How to Write a REST API in C++ in 30 Minutes: A Practical Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Write a REST API in C++?



REST API (Representational State Transfer) is an architectural style for interaction between components of a distributed application over a network. Typically, Python (Flask, FastAPI), JavaScript (Express.js), or Java are chosen for writing REST APIs. However, C++ remains undeservedly overlooked. Which is a shame.



Modern libraries such as Crow, cpp-httplib, and Drogon allow you to create high-performance REST APIs in C++ in literally half an hour. Thanks to C++'s speed and minimal memory consumption, such APIs are ideal for:



  • microservices with high performance requirements;
  • embedded systems and IoT devices;
  • handling a large number of concurrent requests;
  • implementing gateways and proxy servers.


In this article, we will practically create a simple REST API in C++ using the Crow library. You will see that it is no more difficult than in Python, but many times faster in terms of performance.



Environment Setup and Installing the Crow Library



For our project, we will use Crow — a minimalistic and very fast C++ micro-web-framework. It supports routing, middleware, JSON serialization, and works on Windows, Linux, and macOS.



What You Will Need

  • C++17 compiler or newer (GCC 8+, Clang 7+, MSVC 2019+);
  • CMake version 3.14+;
  • The asio library (installed automatically with Crow);
  • The nlohmann/json library (for working with JSON).


Installing Crow



Crow is a header-only library. This means you don't need to compile it — just copy the header files into your project. The easiest way is via CMake FetchContent.



Create the project structure:

rest-api-cpp/├── CMakeLists.txt├── src/│   └── main.cpp└── include/


Contents of the CMakeLists.txt file:

cmake_minimum_required(VERSION 3.14)project(rest_api_cpp)

set(CMAKE_CXX_STANDARD 17)

# Include Crowinclude(FetchContent)FetchContent_Declare( crow GIT_REPOSITORY https://github.com/ipkn/crow.git GIT_TAG master)FetchContent_MakeAvailable(crow)

# Include nlohmann/jsonFetchContent_Declare( json GIT_REPOSITORY https://github.com/nlohmann/json.git GIT_TAG v3.11.2)FetchContent_MakeAvailable(json)

add_executable(rest_api src/main.cpp)target_link_libraries(rest_api PRIVATE Crow::Crow nlohmann_json::nlohmann_json)


After that, run:

mkdir build && cd buildcmake ..make


Everything is ready for writing code.



Creating a Basic REST API in C++ with Crow



Let's write a simple API for managing a task list (Task Manager). It will support four main HTTP methods: GET, POST, PUT, DELETE.



Server Skeleton and GET Endpoint



#include <crow.h>#include <nlohmann/json.hpp>#include <vector>#include <string>

using json = nlohmann::json;

struct Task { int id; std::string title; bool completed;};

std::vector<Task> tasks = { {1, "Learn Crow", false}, {2, "Write REST API", false}};int next_id = 3;

int main() { crow::SimpleApp app;

// GET /tasks — get all tasks CROW_ROUTE(app, "/tasks") .methods("GET"_method) ([](){ json j = json::array(); for (const auto& t : tasks) { j.push_back({{"id", t.id}, {"title", t.title}, {"completed", t.completed}}); } return crow::response(j.dump()); });

app.port(8080).multithreaded().run();}


This code starts an HTTP server on port 8080. When a GET request is made to /tasks

Blogs

Book Recommendations