<p>The C API for processing JSON is <strong><a href="https://jansson.readthedocs.org/en/latest/" rel="nofollow">Jansson</a></strong>. C applications like <a href="https://github.com/bitcoin/libblkmaker" rel="nofollow"><code>libblkmaker</code></a> use <a href="https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Command_line_.28cURL.29" rel="nofollow">cURL</a> for making the calls and Jansson for interpreting the JSON that cURL fetches.</p>

<p>For example basic usage (which can be easily modified for Bitcoin RPC), see the Jansson example <a href="https://jansson.readthedocs.org/en/latest/_downloads/github_commits.c" rel="nofollow"><code>github_commits.c</code></a> and <a href="https://jansson.readthedocs.org/en/latest/tutorial.html#the-github-repo-commits-api" rel="nofollow">the associated tutorial</a>.</p>

<p>Here's an example of how to make JSON RPC calls with <a href="https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Command_line_.28cURL.29" rel="nofollow">cURL</a>, the output of which Jansson could process:</p>



<pre><code>#include &lt;stdlib.h&gt;

#include &lt;curl/curl.h&gt;

int main()
{
    CURL *curl = curl_easy_init();
    struct curl_slist *headers = NULL;

    if (curl) {
    const char *data =
        "{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", \"method\": \"getinfo\", \"params\": [] }";

    headers = curl_slist_append(headers, "content-type: text/plain;");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8332/");

    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(data));
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

    curl_easy_setopt(curl, CURLOPT_USERPWD,
             "bitcoinrpcUSERNAME:bitcoinrpcPASSWORD");

    curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);

    curl_easy_perform(curl);
    }
    return 0;
}
</code></pre>

<p>It will print, like the <a href="https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Command_line_.28cURL.29" rel="nofollow">cURL</a> example, something like this:</p>

<blockquote>
  <p>{"result":{"balance":0.000000000000000,"blocks":59952,"connections":48,"proxy":"","generate":false,
       "genproclimit":-1,"difficulty":16.61907875185736},"error":null,"id":"curltest"}</p>
</blockquote>

CC BY-SA 3.0
2015-11-01T06:34:32.360Z
2015-11-09T13:25:03.333Z
2015-11-09T13:25:03.333Z
Geremia
<p>How do I make RPC calls from C?</p>

<p>My question is related to the question: "<a href="https://bitcoin.stackexchange.com/questions/5810/how-do-i-call-json-rpc-api-using-c">How do I call JSON RPC API using C#?</a>"</p>

CC BY-SA 3.0
2015-10-30T10:57:17.147Z
To whomever answers this, please consider also pasting your answer here: https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)
David A. Harding
<json-rpc><c>
How to call JSON RPC API from C?
Geremia
# How to call JSON RPC API from C?

(Asked by: username_0 on 2015-10-30)

How do I make RPC calls from C?

My question is related to the question: "[How do I call JSON RPC API using C#?](https://bitcoin.stackexchange.com/questions/5810/how-do-i-call-json-rpc-api-using-c)"

**Tags:** json-rpc, c

> **Comments:**
> **username_1**: To whomever answers this, please consider also pasting your answer here: https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)  

---

## 1 answers

### Answer by username_0 on 2015-11-01. Score: 4 | Accepted answer

The C API for processing JSON is **[Jansson](https://jansson.readthedocs.org/en/latest/)**. C applications like [`libblkmaker`](https://github.com/bitcoin/libblkmaker) use [cURL](https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Command_line_.28cURL.29) for making the calls and Jansson for interpreting the JSON that cURL fetches.

For example basic usage (which can be easily modified for Bitcoin RPC), see the Jansson example [`github_commits.c`](https://jansson.readthedocs.org/en/latest/_downloads/github_commits.c) and [the associated tutorial](https://jansson.readthedocs.org/en/latest/tutorial.html#the-github-repo-commits-api).

Here's an example of how to make JSON RPC calls with [cURL](https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Command_line_.28cURL.29), the output of which Jansson could process:

```
#include <stdlib.h>

#include <curl/curl.h>

int main()
{
    CURL *curl = curl_easy_init();
    struct curl_slist *headers = NULL;

    if (curl) {
    const char *data =
        "{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", \"method\": \"getinfo\", \"params\": [] }";

    headers = curl_slist_append(headers, "content-type: text/plain;");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8332/");

    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(data));
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

    curl_easy_setopt(curl, CURLOPT_USERPWD,
             "bitcoinrpcUSERNAME:bitcoinrpcPASSWORD");

    curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);

    curl_easy_perform(curl);
    }
    return 0;
}

```

It will print, like the [cURL](https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Command_line_.28cURL.29) example, something like this:

> {"result":{"balance":0.000000000000000,"blocks":59952,"connections":48,"proxy":"","generate":false,
> "genproclimit":-1,"difficulty":16.61907875185736},"error":null,"id":"curltest"}


---

