I am working on a project where people can contribute to the project in Bitcoin or Ethereum. For each I need a way to be able to display an active balance in each currency's wallet. Something along the lines of this:

You have elected to contribute in BTC to the project. Thank you. Your contributions are most welcome. Please send your BTC to 123abc456def. This wallet currently contains X Bitcoins, while Y have been contributed in total, and the contributed funds have been used for the purposes listed below.

And each time the person accesses the page, X and Y will reflect that wallet at that moment. I'd really prefer not to run a full copy of the blockchain for this. I already have bitcoind running with a 4GB pruned database, and I'm hoping that will be enough.

I need to do all this in PHP. Is there a good PHP toolkit/library/whatever that I can use to call the info?


2 Answers 2


I already have bitcoind running with a 4GB pruned database, and I'm hoping that will be enough.

It is! By running bitcoin-core, you can pull this data in an easy, trustless way.

I would recommend creating and displaying a new address to your patrons each time a new donation is received (so as to avoid address reuse, which is detrimental to your privacy, and perhaps more importantly your patron's privacy), but otherwise you should be able to accomplish your goal without much issue. You could perhaps look at something like btcpayserver to help automate the payment flow on your website.

Here is a general workflow:

  1. Generate a private key that will be used to receive funds, using your wallet of choice (ideally, this would be done in an offline environment)
  2. Import the address(es) that are derived from the keys you generated in step one, into bitcoin-core. You can use bitcoin-cli importaddress to accomplish this step. Note that if the address is brand new and you know for certain it has never received funds before, then you can set rescan to false, to save a fair bit of computational time.
  3. Use the command bitcoin-cli listunspent to return a list of UTXOs that are locked to addresses that the wallet is watching. You can optionally filter the returned info by address, see bitcoin-cli help listunspent for more info.
  4. (Alternatively, you can use bitcoin-cli getreceivedbyaddress, which may be easier if you're re-using a single receiving address)
  5. From there, you'll just need to parse the JSON data returned, and sum up the relevant address balance(s), to be displayed on your webpage.
  • I often wonder about multiple addresses. The issue I see with using multiple addresses is the extra transaction fees each time the funds need to be used. If I get a donation of 50mBTC from user 1, and 60mBTC from user 2,each sending to a different wallet, then I somehow need to consolidate all the funds before they can be used. That would seem to lean towards using a single address. Now if there's a free way of doing that I'm all ears. But that is a concern.
    – Dan Egli
    Apr 16, 2021 at 2:56
  • @DanEgli you misunderstand how bitcoin fees work. Each received transaction create a UTXO, the cost of spending each UTXO as input in another transaction does not depend whatsoever on whether the coins are locked to the same address, or different addresses. There is absolutely no difference in terms of fees.
    – chytrik
    Apr 16, 2021 at 3:00
  • Maybe I am misunderstanding. If so please correct me. But my impression was this: USER A sends 40mBTC to Address 123abc. User 2 send another 40mBTC to address def456. Now, I have 80mBTC but it's split in to two chunks. To consolidate into one spendable lump, I need to maybe send the funds in wallet 2 to wallet 1. But that's an extra transaction, thus incuring extra transaction fees.
    – Dan Egli
    Apr 16, 2021 at 4:55
  • @DanEgli you are assuming that bitcoin uses an 'account' model to keep track of coins, but that is incorrect, it uses a 'UTXO' model. Even if you use the same address, multiple payments will still create multiple UTXOs, which will incur the exact same fees as you would spending multiple UTXOs that are locked to different addresses. Needlessly consolidating funds is never a great idea though, just spend the UTXOs when you need to, no need to consolidate first. See this question for more info: bitcoin.stackexchange.com/questions/49853/…
    – chytrik
    Apr 17, 2021 at 4:16
  • I think I understand you better. But this still leaves me with a quandry. Let me suppose that I have received 600mBTC, but in six increments of 100mBTC each. So I have six UTXOs, that's great. But if they are in six wallets, then how do I send say 450mBTC to someone? Near as I can tell, I'd have to send all 100mBTC from wallets 1-4, then the balance from wallet 5. And each of those would generate a transaction fee.Unless there's a way to make various addresses aliases for the same wallet? I don't care if I use 200,000 addresses as long as I don't have to take the funds from multiple wallets.
    – Dan Egli
    Apr 18, 2021 at 3:11

Do you want the balance shown to be specific to the user viewing the page, or the total balance contributed from all users?

The former requires more work, the latter is trivially simple.

You can use something like the blockstream API, called via cURL in PHP


"chain_stats" contains information for confirmed transactions that you can use to calculate the balance of the address

  • Not per user. Just overall. If the wallet has, say 0.5 BTC, then that's what it shows, regardless of who looks. But as soon as 0.1 BTC was transferred out, then people should see 0.4 BTC in the wallet. The actual list of transactions can be manually created easily.
    – Dan Egli
    Apr 16, 2021 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.