How to Connect WordPress to a Self-Hosted Bitcoin Wallet API
Keep BTC_KEY in the server environment and use the WordPress HTTP API from a private backend function. Replace the fixed placeholder wallet origin with your trusted HTTPS endpoint. The example calls RECEIVE on testnet only and disables redirects. Never accept an arbitrary engine URL from a browser. Add authenticated session creation, rate limits and full address checksum validation before exposing a public endpoint. The BTC KEY may authorize more than RECEIVE, so isolate the engine behind a restricted service. Do not log request headers.
Source code
1<?php
2function stage25_receive_args(string $key): array {
3 if ($key === '' || preg_match('/[\r\n]/', $key)) throw new InvalidArgumentException('Invalid key');
4 return ['headers'=>['X-BTC-KEY'=>$key,'Content-Type'=>'application/json'],
5 'body'=>json_encode(['net'=>'test','type'=>'segwit'], JSON_THROW_ON_ERROR),
6 'timeout'=>10,'redirection'=>0,'sslverify'=>true];
7}
8function stage25_receive(): array {
9 $key=getenv('BTC_KEY');
10 if ($key === false) throw new RuntimeException('Missing credential');
11 $r=wp_remote_post('https://wallet.example.org/api/v5/btc/wallet/receive',stage25_receive_args($key));
12 if (is_wp_error($r) || wp_remote_retrieve_response_code($r)!==200) throw new RuntimeException('Wallet unavailable');
13 $j=json_decode(wp_remote_retrieve_body($r),true,512,JSON_THROW_ON_ERROR);
14 if (($j['ok']??false)!==true || !is_string($j['result']['address']??null) || !str_starts_with($j['result']['address'],'tb1q')) throw new RuntimeException('Invalid response');
15 return ['address'=>$j['result']['address']];
16}
17if (PHP_SAPI === 'cli' && stage25_receive_args('fixture')['redirection']!==0) exit(1);Validation and deployment
Use a staging site and testnet first. Verify the destination, test duplicate payment notifications and keep fulfillment separate from browser requests. This example was checked locally, not inside a live CMS.
Related documentation
https://thamer.org/api
