Minimal C/C++ libcurl IPify API example in C++

This simple program uses the libcurl easy API to make a HTTPS request to ipify to get the current IP address.

#include <iostream>
#include <cstdio>
#include <curl/curl.h>

int main(int argc, char **argv)
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, ""); // Get IP address
        res = curl_easy_perform(curl);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](void *contents, size_t size, size_t nmemb, void *userp) {
            // Write (stream) to stdout
            fwrite(contents, size, nmemb, stdout);

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

        curl_easy_cleanup(curl); // Clean up the CURL handle

    return 0;

Compile using

g++ -o curltest curltest.cpp -lcurl