How to Add a Bitcoin Donation Button to a Website
A donation button can open a Bitcoin payment URI without exposing wallet credentials.
Implementation approach
Use a server-configured, independently validated address. The example checks the character boundary for HTML and URI construction, not the Bitcoin checksum. A fixed address is simple but exposes repeated donations to public correlation.
Source code
1from html import escape
2import re
3
4def button(address):
5 if not re.fullmatch(r'[a-zA-Z0-9]{14,90}', address):
6 raise ValueError('invalid address characters')
7 return '<a href="bitcoin:' + escape(address, quote=True) + '">Donate Bitcoin</a>'
8
9if __name__ == '__main__':
10 assert button('tb1qsyntheticfixture').startswith('<a href="bitcoin:')
11 try: button('x" onclick="bad')
12 except ValueError: pass
13 else: raise AssertionError('unsafe input accepted')Local validation
Run stage23-donation-button.py with Python 3. Fixtures are not valid Bitcoin addresses. This is an educational example, not a production donation service. No SEND operation is included.
Related documentation
https://thamer.org/api
