DEV Community

Cover image for Create your API using C++
Prayush Adhikari
Prayush Adhikari

Posted on • Edited on

Create your API using C++

Creating a C++ API with Drogon Framework

Introduction

In today's connected world, APIs are the backbone of modern applications, enabling different software systems to communicate with each other. While many developers rely on frameworks like Node.js, Python, or Java for building APIs, C++ is a powerful yet often overlooked option. This tutorial will walk you through creating your own API using C++ and the Drogon framework—a high-performance, production-ready framework for developing web applications.
View my repo on TheBookDB-API for reference: Click Here

What You'll Learn:

  • Setting up a C++ development environment for API creation
  • Creating a basic API using the Drogon framework
  • Managing routes, endpoints, and handling HTTP methods
  • Working with JSON data using JsonCpp
  • Best practices for structuring and deploying your API

Project Overview: TheBookDB

We'll build a simple book database API called TheBookDB. This API will allow users to:

  • Retrieve a List of Books
  • Filter Books
  • Add New Books
  • Update Existing Books
  • Delete Books

Key API Endpoints:

  • GET /books: Retrieve all books in the database
  • GET /books/filter: Filter books based on criteria like publication date and author
  • POST /books: Add a new book to the database
  • PATCH /books/{bookID}: Update an existing book's details
  • DELETE /books/{bookID}: Delete a book from the database
  • PUT /books/{bookID}: Update or add a book

Technologies and Tools

  • C++
  • Drogon Framework
  • JsonCpp
  • Linux (Ubuntu)

Getting Started

  1. Create a new Drogon project:

drogon_ctl create project <project_name>

  1. Create a controller for the Book API:

drogon_ctl create controller Book

  1. Navigate to the project directory and build:

cd <project_name>
mkdir build
cd build
cmake ..
make

  1. Run the project:

./<project_name>

  1. Prepare your books.csv file and place it in the root directory.

  2. Update CMakeLists.txt to copy the CSV file to the build directory:

configure_file(${CMAKE_SOURCE_DIR}/books.csv
${CMAKE_BINARY_DIR}/books.csv COPYONLY)

Implementation

Book.h

Click here to view Book.h

Book.cpp

Click here to view Book.cpp

Running the API

After completing the implementation, build and run the application. Check the config.json file to find the port on which your application will run.

Join the PrayushDev Community

Subscribe to the weekly PrayushDev newsletter for:

  • Bite-sized introductions to hot tech topics
  • Insider tips and tricks from the world of development
  • Early access to upcoming "React Unmasked" episodes
  • A chance to be part of a growing community of passionate devs

Subscribe to PrayushDev

Support My Journey as a Microsoft Student Ambassador

Help me reach my goal as a Microsoft Student Ambassador:

  1. Visit my special link: Microsoft Student Ambassador
  2. Explore the page and learn about the initiatives
  3. Share the link with your friends and colleagues

My goal is to reach 250 visitors. Your support contributes to tech education and community building.

Thank you for being part of this journey!

Top comments (2)

Collapse
 
biancainsk profile image
Bianca Ionescu

Awesome article! Thanks a ton for sharing this. I really enjoyed it!

Collapse
 
yunesh_shrestha_92d55c598 profile image
Yunesh Shrestha

Great. Everyone should give a try.