Skip to main content

pyln-client: A python client library for lightningd

This package implements the Unix socket based JSON-RPC protocol that lightningd exposes to the rest of the world. It can be used to call arbitrary functions on the RPC interface, and serves as a basis for plugins written in python.

Installation

pyln-client is available on pip:

pip install pyln-client

Alternatively you can also install the development version to get access to currently unreleased features by checking out the Core Lightning source code and installing into your python3 environment:

git clone https://github.com/ElementsProject/lightning.git
cd lightning/contrib/pyln-client
uv sync

This will add links to the library into your environment so changing the checked out source code will also result in the environment picking up these changes. Notice however that unreleased versions may change API without warning, so test thoroughly with the released version.

Examples

Using the JSON-RPC client

"""
Generate invoice on one daemon and pay it on the other
"""
from pyln.client import LightningRpc
import random

# Create two instances of the LightningRpc object using two different Core Lightning daemons on your computer
l1 = LightningRpc("/tmp/lightning1/lightning-rpc")
l5 = LightningRpc("/tmp/lightning5/lightning-rpc")

info5 = l5.getinfo()
print(info5)

# Create invoice for test payment
invoice = l5.invoice(100, "lbl{}".format(random.random()), "testpayment")
print(invoice)

# Get route to l1
route = l1.getroute(info5['id'], 100, 1)
print(route)

# Pay invoice
print(l1.sendpay(route['route'], invoice['payment_hash']))

Writing a plugin

Plugins are programs that lightningd can be configured to execute alongside the main daemon. They allow advanced interactions with and customizations to the daemon.

#!/usr/bin/env python3
from pyln.client import Plugin

plugin = Plugin()

@plugin.method("hello")
def hello(plugin, name="world"):
    """This is the documentation string for the hello-function.

    It gets reported as the description when registering the function
    as a method with `lightningd`.

    If this returns (a dict), that's the JSON "result" returned.  If
    it raises an exception, that causes a JSON "error" return (raising
    pyln.client.RpcException allows finer control over the return).
    """
    greeting = plugin.get_option('greeting')
    s = '{} {}'.format(greeting, name)
    plugin.log(s)
    return s


@plugin.init()
def init(options, configuration, plugin):
    plugin.log("Plugin helloworld.py initialized")
    # This can also return {'disabled': <reason>} to self-disable,
	# but normally it returns None.


@plugin.subscribe("connect")
def on_connect(plugin, connect, **kwargs):
    plugin.log("Received connect event for peer {}".format(connect))


plugin.add_option('greeting', 'Hello', 'The greeting I should use.')
plugin.run()

Metadata

Release files for pyln-client 26.6.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyln-client 26.6.9
File Size Uploaded
pyln_client-26.6.9.tar.gz 92.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyln-client 26.6.9
File Interpreter ABI Platform
pyln_client-26.6.9-py3-none-any.whl Python 3 none any Details

Total release size: 130.5 kB

Release files / pyln_client-26.6.9.tar.gz

Download URL pyln_client-26.6.9.tar.gz
Size 92.5 kB
Tags Source
SHA-256 checksum
How to use checksums
881212d80accfb6d1d6cc000bfc4e1b98a837a6ca5b543f46d37d396929fbe99
BLAKE2b-256 checksum
How to use checksums
50b8b9ba64daf6af79e6894dbb5fb3b0742e699f800ddb1dde0da2f1b80e8616
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pyln_client-26.6.9-py3-none-any.whl

Download URL pyln_client-26.6.9-py3-none-any.whl
Size 38.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a15e2494b8a580844296d2d6020bdb258b54e57f2a52c4c65993246ea720a26f
BLAKE2b-256 checksum
How to use checksums
1e41588fa6fc0f3f78577100615a5496659c35ce18ef8bf5f2e522e5251473f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

26.6.9 This release

2 release files

26.6.8

2 release files

26.6.7

2 release files

26.6.6

2 release files

26.6.2

2 release files

26.6

2 release files

26.4.2

2 release files

26.4.1

2 release files

26.4

2 release files

25.12

2 release files

25.9.1

2 release files

25.9

2 release files

25.5

2 release files

25.2.2

2 release files

25.2

2 release files

24.11

2 release files

24.8.2

2 release files

24.8

2 release files

24.5

2 release files

24.2

2 release files

23.11

2 release files

23.8

2 release files

23.5.2

2 release files

23.5

2 release files

23.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.8.2

2 release files

0.8.0

2 release files

0.7.3

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page