How to Generate a Bitcoin Receive Address for a Website
Keep the wallet credential on the backend. The browser requests a checkout session, not arbitrary wallet operations. The documented WAB RECEIVE endpoint accepts net and type; its result contains an address.
Implementation approach
Use a fixed loopback engine URL, a timeout and a bounded response. Do not retry automatically after an ambiguous response. The address returned here is trusted engine data; the example does not independently validate its checksum. Persist the address against the authenticated order before displaying it. For priced invoices prefer WATCH CREATE.
Source code
1import json
2import os
3import urllib.request
4
5class NoRedirect(urllib.request.HTTPRedirectHandler):
6 def redirect_request(self, *args, **kwargs):
7 raise ValueError("redirect refused")
8
9def receive(opener=None):
10 if opener is None:
11 opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect()).open
12 key = os.environ['BTC_KEY']
13 req = urllib.request.Request('http://127.0.0.1:7070/api/v5/btc/wallet/receive', data=b'{"net":"test","type":"segwit"}', headers={'X-BTC-KEY':key,'Content-Type':'application/json'}, method='POST')
14 with opener(req, timeout=5) as response:
15 raw = response.read(65537)
16 if len(raw)>65536: raise ValueError('oversized response')
17 data=json.loads(raw)
18 if data.get('ok') is not True: raise ValueError('receive failed')
19 address=data['result']['address']
20 if not isinstance(address,str) or not address.startswith('tb1q'): raise ValueError('unexpected address')
21 return address
22
23if __name__=='__main__':
24 import io
25 from unittest.mock import patch
26 with patch.dict(os.environ, {'BTC_KEY':'synthetic'}):
27 assert receive(lambda req,timeout: io.BytesIO(b'{"ok":true,"result":{"address":"tb1qfixture"}}'))=='tb1qfixture'Local validation
Run python3 stage22-receive-address.py. The assertions use synthetic fixtures, not real Bitcoin addresses or payments. This is a reference example, not a complete production checkout. No SEND operation is included.
Related documentation
https://thamer.org/api
https://thamer.org/learn/bitcoin-ai-agents-advanced-research-guide
