Call Core Lightning API

uenohiro4
Nayuta Engineering Blog
2 min readNov 18, 2023

Let’s call the Core Lightning API in golang!

It used to be called c-lightning in the past, but now it’s referred to as Core Lightning. The abbreviation is CLN, so I’ll use CLN in the following.

LND sample code:

CLN is primarily designed to be accessed through JSON-RPC 2.0 API. While gRPC and REST are also included as standard, they are turned off by default.

The JSON-RPC library for CLN is provided as standard for Python and Rust. However, since I am not familiar with either, I opted to use ‘glightning,’ a Golang library available in the same repository.

I created a regtest environment using Polar.

Bob and Dave are using CLN. Let’s try calling getinfo for Bob.

You need to know the RPC file path for Bob’s node and JSON-RPC. You can obtain it from the properties screen in Polar.

The items such as ‘TLS Cert’ are located towards the bottom. The RPC file is in the same directory as those.

Please write the directory name in getinfo.go's dirRPC.

$ go run .
{"id":"0248b4faf96048926000d8622ea4ae0af863ca752fcd1d5a392bcd32529e0303e6","alias":"bob","color":"0248b4","num_peers":3,"num_pending_channels":0,"num_active_channels":2,"num_inactive_channels":0,"address":[],"binding":[{"type":"ipv4","address":"172.19.0.4","port":19846,"socket":"","service":{"type":"","address":"","port":0},"name":""},{"type":"ipv4","address":"0.0.0.0","port":9735,"socket":"","service":{"type":"","address":"","port":0},"name":""}],"version":"v23.05.2","blockheight":177,"network":"regtest","fees_collected_msat":54,"lightning-dir":"/home/clightning/.lightning/regtest"}

One important point to note is that the return values of glightning are not necessarily identical to the return values in JSON-RPC. For example, looking at the specifications for getinfo it’s apparent that not all values listed under ‘RETURN VALUE’ are displayed.

The return value of glightning.GetInfo() is a NodeInfo, and it retrieves only those fields for which the structure tags match the JSON-RPC return values. If you need additional parameters, it might be easy to extend glightning on your own.

--

--