CosmosClient is a header-only Modern C++23 client library for the Azure Cosmos DB REST API. Designed with nlohmann::json as a first-class API metaphor, it abstracts token signature calculation, regional endpoint resolution, payload serialization, and pagination behind a clean, expressive C++ interface.
- JSON-First API:
nlohmann::jsonis a first-class citizen for documents, configuration, queries, and headers. - Modern C++23: Utilizes C++23 standard features (
std::format, aggregate initialization, structured bindings,std::expected). - Thread-Safe Failover & Rotation: Lock-free atomic connection rotation (
rotate(),rotateReadUri()) safe under concurrent workloads. - Header-Only: Easy integration with no compilation overhead.
- Cross-Platform: Built on top of
restcl, supporting nativeWinHTTPon Windows andlibcurlon Unix/Linux/macOS. - Full Cosmos SQL API: Complete support for Databases, Containers (Collections), Documents, SQL Queries, and Stored Procedures.
- Async Operations: Built-in non-blocking asynchronous operation dispatch via thread pool with worker exception safety.
- Auto Token Signing: Automatic HMAC-SHA256 authorization token generation for Azure Cosmos DB REST requests.
#include "nlohmann/json.hpp"
#include "siddiqsoft/cosmoscl.hpp"
int main() {
siddiqsoft::CosmosClient client;
// Configure client with Azure Portal connection string
client.configure({
{"connectionStrings", {"AccountEndpoint=https://myaccount.documents.azure.com:443/;AccountKey=dGhpcyBpcyBhIHNhbXBsZSBrZXk=;"}},
{"partitionKeyNames", {"/id"}}
});
// Create a new document
auto resp = client.createDocument({
.database = "mydb",
.collection = "items",
.partitionKey = "item-101",
.document = {
{"id", "item-101"},
{"name", "Modern C++ Sensor"},
{"status", "active"}
}
});
if (resp.statusCode == 201) {
std::cout << "Document created! Request Charge (RUs): " << resp.requestCharge << std::endl;
}
return 0;
}#include "nlohmann/json.hpp"
#include "siddiqsoft/cosmoscl.hpp"
int main() {
siddiqsoft::CosmosClient client;
client.configure({
{"connectionStrings", {"AccountEndpoint=https://myaccount.documents.azure.com:443/;AccountKey=...;"}},
{"partitionKeyNames", {"/id"}}
});
auto resp = client.queryDocuments({
.database = "mydb",
.collection = "items",
.query = "SELECT * FROM c WHERE c.status = @status",
.parameters = { {{"name", "@status"}, {"value", "active"}} }
});
if (resp.statusCode == 200) {
for (const auto& doc : resp.document["Documents"]) {
std::cout << "Doc ID: " << doc["id"] << ", Name: " << doc["name"] << std::endl;
}
}
return 0;
}WARNING
This package has dependencies that must be satisfied via CPM / CMake when compiling.
nuget install SiddiqSoft.CosmosClientAdd to your CMakeLists.txt:
include(pack/CMakeCommonHelpers.cmake)
CPMAddPackage("gh:SiddiqSoft/CosmosClient#3.2.0")
target_link_libraries(your_target PRIVATE cosmoscl::cosmoscl)CosmosClient is structured as a header-only library using a main facade header (siddiqsoft/cosmoscl.hpp) and single-responsibility sub-headers in siddiqsoft/private/:
cosmoscl.hpp: Public facade header defining theCosmosClientclass interface and including internal modules.private/cosmos_types.hpp: Global logging instance (gCLog) andCosmosOperationenum definitions.private/cosmos_endpoint.hpp:CosmosEndpointconnection string parsing and lock-free atomic URI rotation.private/cosmos_connection.hpp:CosmosConnectionstruct and atomic primary/secondary failover state.private/cosmos_response.hpp:CosmosResponseTypeandCosmosIterableResponseTyperesponse envelopes.private/cosmos_argument.hpp:CosmosArgumentTyperequest payload structure and callback type definitions.private/operations/: Single-responsibility operation headers (database_ops.hpp,collection_ops.hpp,document_ops.hpp,query_ops.hpp,region_ops.hpp).private/cosmos_serializers.hpp:nlohmann::to_json,std::formatter, andoperator<<stream helpers.
CosmosClient depends on lightweight header-only libraries and platform HTTP components.
See dependencies.md or docs/integration/dependencies.md for the full interactive Mermaid diagram and breakdown.
# Clone repository
git clone https://github.com/SiddiqSoft/CosmosClient.git
cd CosmosClient
# Build with CMake preset
cmake --preset default
cmake --build --preset default| Option | Default | Description |
|---|---|---|
cosmoscl_BUILD_TESTS |
OFF |
Build unit and integration test suite (BUILD_TESTS). |
restcl_DEBUG_TRACE |
OFF |
Enable detailed HTTP header and payload trace logging to std::cerr. |
To run tests against the local Azure Cosmos DB Emulator (using Docker or Podman):
# 1. Start the Linux vNext Emulator container
./tests/setup-emulator.sh
# 2. Build and run tests using script or CMake
./tests/build_and_test.sh debug macosNote on Emulator Protocols (
http://vshttps://):
- vNext Linux Emulator (
azure-cosmos-emulator:vnext-latest): Serves unencrypted HTTP on port 8081 (AccountEndpoint=http://localhost:8081/;).- Windows / Classic Emulator: Serves HTTPS on port 8081 with a self-signed TLS certificate (
AccountEndpoint=https://localhost:8081/;).
Full documentation site is built with Material MkDocs and published to GitHub Pages:
CosmosClient is licensed under the BSD 3-Clause License.