Skip to content

C API Client Example

This example demonstrates how to send an HTTP request to the Core API using the libcurl library in C.

It includes installation instructions, a basic implementation example, and compilation steps.


Install libcurl

Debian / Ubuntu

sudo apt-get install libcurl4-openssl-dev

macOS (Homebrew)

brew install curl

Example Request

#include <stdio.h>
#include <curl/curl.h>

int main(void) {
    const char *base_url = "https://website.core-software.co.uk";
    const char *token = "YOUR_TOKEN_HERE";

    char url[256];
    snprintf(url, sizeof(url), "%s/api/v1", base_url);

    char auth_header[256];
    snprintf(auth_header, sizeof(auth_header),
             "Authorization: Bearer %s", token);

    CURL *curl = curl_easy_init();
    if (!curl) {
        fprintf(stderr, "Failed to initialise curl\n");
        return 1;
    }

    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, auth_header);
    headers = curl_slist_append(headers, "Content-Type: application/json");

    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    CURLcode res = curl_easy_perform(curl);

    if (res != CURLE_OK) {
        fprintf(stderr, "Request failed: %s\n",
                curl_easy_strerror(res));
    }

    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);

    return 0;
}

Compilation

Compile the program using gcc:

gcc api_query.c -o api_query -lcurl

Notes

  • Ensure the libcurl development package is installed before compiling
  • Replace YOUR_TOKEN_HERE with a valid API token
  • The example sends a basic GET request using Bearer token authentication
  • Error handling is intentionally minimal for demonstration purposes