Skip to content

How to write APIs for C++ SDK Appwrite? #85

@pooranjoyb

Description

@pooranjoyb

Discussed in #84

Originally posted by pooranjoyb July 26, 2025

Few things to know before writing a new API for the SDK :

  • C++ SDK doesn't officially exist for Appwrite, hence we're doing it ourselves.
  • We'll have to maintain the code-quality and SDK structure that Appwrite follows in their SDK repositories.
  • Coding's gonna be hard, so learn basic OOPs concepts.

Now say we need to write a new api called getTopic which is a part of Messaging module.

  • Firstly we'll check the Official API reference how they're calling the API.
  • Then, we'll go to existing Appwrite SDKs repository (say Python-SDK, JS-SDK) and see how they've implemented that API.

For your reference
https://github.com/appwrite/sdk-for-python/blob/503de4b759c7b2611281c019d78e11aed47644f0/appwrite/services/messaging.py#L1881-L1911

  • Once we get an idea of the parameters/api-endpoint etc; we'll start writing our own api-code.
  • Based on our research of the API we're working on, we'll declare the API in the respective module with necessary parameters.

For your reference

std::string getTopic(const std::string &topicId);

  • Now we'll write the API to getTopics from the Appwrite Console.

For your reference

std::string Messaging::getTopic(const std::string &topicId) {
if (topicId.empty()) {
throw AppwriteException("Missing required parameter: 'topicId'");
}
std::string url = Config::API_BASE_URL + "/messaging/topics/" + topicId;
std::vector<std::string> headers = Config::getHeaders(projectId);
headers.push_back("X-Appwrite-Key: " + apiKey);
std::string response;
int statusCode = Utils::getRequest(url, headers, response);
if (statusCode == HttpStatus::OK) {
return response;
} else {
throw AppwriteException(
"Error fetching topic. Status code: " + std::to_string(statusCode) +
"\n\nResponse: " + response);
}
}

  • Now we'd need to test the API that we've created, hence we'll create an example-file.cpp to verify it's working.

For your reference

#include "Appwrite.hpp"
#include <iostream>
int main() {
std::string projectId = "66fbb5a100070a3a1d19";
std::string apiKey = "";
Appwrite appwrite(projectId, apiKey);
std::string topicId = "67b3048600077f40b8a7";
try {
std::string response = appwrite.getMessaging().getTopic(topicId);
std::cout << "Topic Fetched! \nResponse: " << response << std::endl;
} catch (const AppwriteException& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}
return 0;
}

  • Okay, at this point we're all set to test the API, but before that we need to add an entry to the Makefile, to compile and test it.

For your reference

cpp-sdk-appwrite/Makefile

Lines 249 to 252 in 4f812f5

# Messaging - Topics
getTopic: $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp
@mkdir -p ./$(TESTS_DIR)
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/getTopic $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp $(LDFLAGS)

  • Now we'll compile and run our API.

    # Linux make getTopic
    # Windows mingw32-make getTopic
  • This will create the binary for the API in ./tests/ directory.

  • Finally we'll run our API and debug it further based on my requirement.

cd tests/ ./getTopic

Summary

You’ve now:

  • Investigated the API from the docs and SDKs
  • Declared and implemented the getTopic API
  • Created a test case
  • Updated the build pipeline
  • Successfully compiled and executed your implementation

🎉 You’ve added a new API to the C++ Appwrite SDK!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions