SiddiqSoft.CosmosClient 0.8.6

dotnet add package SiddiqSoft.CosmosClient --version 0.8.6
                    
NuGet\Install-Package SiddiqSoft.CosmosClient -Version 0.8.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SiddiqSoft.CosmosClient" Version="0.8.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SiddiqSoft.CosmosClient" Version="0.8.6" />
                    
Directory.Packages.props
<PackageReference Include="SiddiqSoft.CosmosClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SiddiqSoft.CosmosClient --version 0.8.6
                    
#r "nuget: SiddiqSoft.CosmosClient, 0.8.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SiddiqSoft.CosmosClient@0.8.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SiddiqSoft.CosmosClient&version=0.8.6
                    
Install as a Cake Addin
#tool nuget:?package=SiddiqSoft.CosmosClient&version=0.8.6
                    
Install as a Cake Tool

CosmosClient: Azure Cosmos DB REST Client for Modern C++

Build Status alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Overview

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.

Key Features

  • JSON-First API: nlohmann::json is 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 native WinHTTP on Windows and libcurl on 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.

Table of Contents


Quick Start

Create Document

#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;
}

SQL Query with Parameters

#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;
}

Installation

Via NuGet (Windows)

WARNING

This package has dependencies that must be satisfied via CPM / CMake when compiling.

nuget install SiddiqSoft.CosmosClient

Add to your CMakeLists.txt:

include(pack/CMakeCommonHelpers.cmake)

CPMAddPackage("gh:SiddiqSoft/CosmosClient#3.2.0")

target_link_libraries(your_target PRIVATE cosmoscl::cosmoscl)

Architecture & Modular Header Layout

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 the CosmosClient class interface and including internal modules.
  • private/cosmos_types.hpp: Global logging instance (gCLog) and CosmosOperation enum definitions.
  • private/cosmos_endpoint.hpp: CosmosEndpoint connection string parsing and lock-free atomic URI rotation.
  • private/cosmos_connection.hpp: CosmosConnection struct and atomic primary/secondary failover state.
  • private/cosmos_response.hpp: CosmosResponseType and CosmosIterableResponseType response envelopes.
  • private/cosmos_argument.hpp: CosmosArgumentType request 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, and operator<< stream helpers.

Dependencies

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.


Building

# Clone repository
git clone https://github.com/SiddiqSoft/CosmosClient.git
cd CosmosClient

# Build with CMake preset
cmake --preset default
cmake --build --preset default

CMake Options

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.

Testing

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 macos

Note on Emulator Protocols (http:// vs https://):

  • 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/;).

Documentation

Full documentation site is built with Material MkDocs and published to GitHub Pages:


License

CosmosClient is licensed under the BSD 3-Clause License.

Product Compatible and additional computed target framework versions.
native native is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.8.6 86 8/18/2026
0.8.5 202 6/10/2026
0.8.4 198 6/4/2026
0.8.3 216 5/23/2026
0.8.1 219 5/20/2026
0.7.15 244 5/4/2026
0.7.14 1,906 12/23/2021
0.7.13 1,774 12/21/2021
0.7.12 1,877 12/20/2021
0.7.11 1,909 12/20/2021
0.7.10 1,967 12/18/2021
0.7.9 1,863 12/15/2021
0.7.8 2,207 11/20/2021
0.7.7 2,033 10/11/2021
0.7.6 1,726 10/8/2021
0.7.5 1,963 10/7/2021
0.7.4 2,010 10/5/2021
0.7.3 1,965 10/5/2021
0.7.2 1,993 9/28/2021
0.7.2-pullrequest0008-0005 1,866 9/28/2021
Loading failed