Skip to main content

LNURL implementation for Python

github-tests-badge github-mypy-badge codecov-badge pypi-badge pypi-versions-badge license-badge

A collection of helpers for building [LNURL][lnurl] support into wallets and services.

LUDS support

Check out the LUDS repository: luds

  • LUD-01 - Base LNURL encoding and decoding
  • LUD-02 - channelRequest base spec
  • LUD-03 - withdrawRequest base spec
  • LUD-04 - Auth base spec
  • LUD-05 - BIP32-based seed generation for auth protocol
  • LUD-06 - payRequest base spec
  • LUD-07 - hostedChannelRequest base spec
  • LUD-08 - Fast withdrawRequest
  • LUD-09 - successAction field for payRequest
  • LUD-10 - aes success action in payRequest
  • LUD-11 - Disposable and storeable payRequests
  • LUD-12 - Comments in payRequest
  • LUD-13 - signMessage-based seed generation for auth protocol
  • LUD-14 - balanceCheck: reusable withdrawRequests
  • LUD-15 - balanceNotify: services hurrying up the withdraw process
  • LUD-16 - Paying to static internet identifiers
  • LUD-17 - Scheme prefixes and raw (non bech32-encoded) URLs
  • LUD-18 - Payer identity in payRequest protocol
  • LUD-19 - Pay link discoverable from withdraw link
  • LUD-20 - Long payment description for pay protocol
  • LUD-21 - verify LNURL-pay payments
  • LUD-23 - addressRequest base spec

Configuration

Developers can force strict RFC3986 validation for the URLs that the library encodes/decodes, using this env var:

LNURL_STRICT_RFC3986 = "0" by default (False)

Basic usage

>>> import lnurl
>>> lnurl.encode('https://service.io/?q=3fc3645b439ce8e7')
Lnurl('LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9', bech32=Bech32('LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9', hrp='lnurl', data=[13, 1, 26, 7, 8, 28, 3, 19, 7, 8, 23, 18, 30, 28, 27, 5, 14, 9, 27, 6, 18, 24, 27, 5, 5, 25, 20, 22, 30, 11, 25, 31, 14, 4, 30, 19, 6, 25, 19, 3, 6, 12, 27, 3, 8, 13, 11, 2, 6, 16, 25, 19, 18, 24, 27, 5, 7, 1, 18, 19, 14]), url=WebUrl('https://service.io/?q=3fc3645b439ce8e7', scheme='https', host='service.io', tld='io', host_type='domain', path='/', query='q=3fc3645b439ce8e7'))
>>> lnurl.decode('LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9')
WebUrl('https://service.io/?q=3fc3645b439ce8e7', scheme='https', host='service.io', tld='io', host_type='domain', path='/', query='q=3fc3645b439ce8e7')

The Lnurl object wraps a bech32 LNURL to provide some extra utilities.

from lnurl import Lnurl

lnurl = Lnurl("LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9")
lnurl.bech32  # "LNURL1DP68GURN8GHJ7UM9WFMXJCM99E5K7TELWY7NXENRXVMRGDTZXSENJCM98PJNWXQ96S9"
lnurl.bech32.hrp  # "lnurl"
lnurl.url  # "https://service.io/?q=3fc3645b439ce8e7"
lnurl.url.host  # "service.io"
lnurl.url.query  # "q=3fc3645b439ce8e7"
lnurl.url.query_params()  # [("q", "3fc3645b439ce8e7")]
dict(lnurl.url.query_params())  # {"q": "3fc3645b439ce8e7"}

query_params() returns a list of (key, value) tuples so query parameters stay lossless and ordered. Use dict(...) when you want convenient mapping-style access and duplicate keys do not matter.

Parsing LNURL responses

You can use a LnurlResponse to wrap responses you get from a LNURL. The different types of responses defined in the [LNURL spec][lnurl-spec] have a different model with different properties (see models.py):

import httpx

from lnurl import Lnurl, LnurlResponse

lnurl = Lnurl('LNURL1DP68GURN8GHJ7MRWW4EXCTNZD9NHXATW9EU8J730D3H82UNV94MKJARGV3EXZAELWDJHXUMFDAHR6WFHXQERSVPCA649RV')
try:
  async with httpx.AsyncClient() as client:
    r = await client.get(lnurl.url)
    res = LnurlResponse.from_dict(r.json())  # LnurlPayResponse
    res.ok  # bool
    res.maxSendable  # int
    res.max_sats  # int
    res.callback.host  # str
    res.callback.query_params()  # list[tuple] [("amount", "1000")]
    dict(res.callback.query_params())  # dict
    res.metadata  # str
    res.metadata.list()  # list
    res.metadata.text  # str
    res.metadata.images  # list
r = requests.get(lnurl.url)

If you have already httpx installed, you can also use the .handle() function directly. It will return the appropriate response for a LNURL.

>>> import lnurl
>>> lnurl.handle('lightning:LNURL1DP68GURN8GHJ7MRWW4EXCTNZD9NHXATW9EU8J730D3H82UNV94CXZ7FLWDJHXUMFDAHR6V33XCUNSVE38QV6UF')
LnurlPayResponse(tag='payRequest', callback=WebUrl('https://lnurl.bigsun.xyz/lnurl-pay/callback/2169831', scheme='https', host='lnurl.bigsun.xyz', tld='xyz', host_type='domain', path='/lnurl-pay/callback/2169831'), minSendable=10000, maxSendable=10000, metadata=LnurlPayMetadata('[["text/plain","NgHaEyaZNDnW iI DsFYdkI"],["image/png;base64","iVBOR...uQmCC"]]'))

You can execute and LNURL with either payRequest, withdrawRequest, addressRequest or login tag using the execute function.

>>> import lnurl
>>> lnurl.execute('lightning:LNURL1DP68GURN8GHJ7MRWW4EXCTNZD9NHXATW9EU8J730D3H82UNV94CXZ7FLWDJHXUMFDAHR6V33XCUNSVE38QV6UF', 100000)

Building your own LNURL responses

For LNURL services, the lnurl package can be used to build valid responses.

from lnurl import CallbackUrl, LnurlWithdrawResponse, MilliSatoshi
from pydantic import TypeAdapter, ValidationError
try:
    res = LnurlWithdrawResponse(
        callback=TypeAdapter(CallbackUrl).validate_python("https://lnurl.bigsun.xyz/lnurl-withdraw/callback/9702808"),
        k1="38d304051c1b76dcd8c5ee17ee15ff0ebc02090c0afbc6c98100adfa3f920874",
        minWithdrawable=MilliSatoshi(1000),
        maxWithdrawable=MilliSatoshi(1000000),
        defaultDescription="sample withdraw",
    )
    res.json()  # str
    res.dict()  # dict
except ValidationError as e:
    print(e.json())

All responses are pydantic models, so the information you provide will be validated and you have access to .json() and .dict() methods to export the data.

Data is exported using :camel: camelCase keys by default, as per spec. Use the LNURL spec field names when parsing and exporting response models.

Will throw and ValidationError if the data is not valid, so you can catch it and return an error response.

CLI

$ uv run lnurl
Usage: lnurl [OPTIONS] COMMAND [ARGS]...

  Python CLI for LNURL decode and encode lnurls

Options:
  --help  Show this message and exit.

Commands:
  decode           decode a LNURL
  encode           encode a URL
  handle           handle a LNURL
  execute          execute a LNURL

Release files for lnurl 2.0.2

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

Source distribution (sdist)

Source distribution for lnurl 2.0.2
File Size Uploaded
lnurl-2.0.2.tar.gz 76.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lnurl 2.0.2
File Interpreter ABI Platform
lnurl-2.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 95.6 kB

Release files / lnurl-2.0.2.tar.gz

Download URL lnurl-2.0.2.tar.gz
Size 76.9 kB
Tags Source
SHA-256 checksum
How to use checksums
64cdfe5af0c37117e91c84a72441824b8f5c6945e5923a16efd69654a06b0c51
BLAKE2b-256 checksum
How to use checksums
453d6665acce88c569652e6640889d6602fda101714adf4505ae05e23eb6eec3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / lnurl-2.0.2-py3-none-any.whl

Download URL lnurl-2.0.2-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4094b4a07e26eb9e5792111e7482a79cfe87c88fddae03c895abd06de5fd06d0
BLAKE2b-256 checksum
How to use checksums
780419e4b145416f5f1b87c77591f9045894777e49aae8131a31f0cf35bb9af9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","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

2.0.2 This release

2 release files

2.0.1

2 release files

2.0.0

2 release files

0.10.4

2 release files

0.10.3

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.8

2 release files

0.6.7

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.6

2 release files

0.3.5

1 release file

0.3.4

1 release file

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 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