Electrs API - can I get a few examples to help me get started?

Here’s what I’m hoping to do: I have Bitcoin Core, Mempool and Electrs running on my Start9 server and it’s great. I’d like to be able to run scripts on my Mac to fetch data on Bitcoin addresses. I’ve been querying blockchain.info for that for a while, and it works perfectly, but part of the point of having my own server is having my own data, so nobody else can notice I check up on the same addresses (“Oh, those must be his” y’know?)

How do I fetch data from Electrs from my Mac? Can you give me an example to get me started?

I fully admit, I’m in over my head here, but I’m just looking for a place to get started. Please be gentle :slight_smile:

EDIT and UPDATE: Well, that was super-easy. Mempool has an API. Done!

DOUBLE EDIT: Oops. Maybe not. Let me explain my project a bit more in depth. I’m sort of creating a watch only wallet, minus the wallet, for automation. Let’s say I have 20 addresses. On my Mac, I’d like to run a script that gets the price of Bitcoin from mempool.space, gets the total amount of BTC in my 20 addresses, and does the math to give me the total value in USD… and just for the heck of it, check to make sure the total spent is zero.

I can easily do this using the mempool API, but it’s very slow since the mempool API can only check one address at a time, so it cycles through them. It works, but it’s too slow for my needs (I have a hotkey on my Mac assigned to run the script & show me the balance, using Ubersicht). Previously, I’d been using the blockchain.info API which allows multiple addresses, separated by a “I”. That’s fast and works, but part of the point of setting up my own server was to stop checking my balances on somebody else’s server.

Hopefully this makes more sense now.

Should I be using the Bitcoin CLI? The Electrs API? And if so, can I get some guidance?

Mempool is the way to check all your bitcoin queries. There’s a JSON rpc on port 50001 that complies with the latest electrum server protocol. You can probably hack it to make it work but Its main purpose is as an indexer for external wallets that use bitcoind as a backend.

I was hoping for an easy way to check simple things, such as the balances of my addresses, over terminal from my Mac.

I’m not exactly sure what you are looking for here. Generally you are not using your server for a hot on-chain wallet. Wallets are typically on clients. Can you explain what it is that you wish to do here? If you are using Bitcoin Core as a wallet (which we would recommend you do NOT do), then you can just use the bitcoin-cli tooling.

Otherwise, the link @H0mer provided has the tools you need.

No worries. I’ve solved it.

Before installing Start9, I was under the mistaken impression that Electrs had an API which I’d use to query its database (or should I say Index?). Turns out, as @H0mer explained, Mempool has an API for that and it’s well documented. So, THAT was easy! Yay.

Oddly, the only thing I can’t find in the Mempool API for my Start9 server is how to get the price of Bitcoin using either curl or js. Eh, that’s fine. I can just query mempool.space for that.

Overall, my experience using Start9 has been positive - as in through the roof positive. This is great!

Glad to hear it! If you’d like to help us out with an Apollo review, it would be much appreciated. You’ll also get 5k sats with that link.

There is currently no way to query the node software on StartOS for a price (this would require a 3rd party oracle of course), but I would very much like to see the UTXO Oracle software integrated in the future, such that each node can do price discovery in a Sovereign manner. Stay tuned!

Oops. I thought the mempool API was what I wanted, but apparently not. Let me explain my project a bit more in depth. I’m sort of creating a watch only wallet, minus the wallet, for automation. Let’s say I have 20 addresses. On my Mac, I’d like to run a script that gets the price of Bitcoin from mempool.space, gets the total amount of BTC in my 20 addresses, and does the math to give me the total value in USD… and just for the heck of it, check to make sure the total spent is zero.

I can easily do this using the mempool API, but it’s very slow since the mempool API can only check one address at a time, so it cycles through them. It works, but it’s too slow for my needs (I have a hotkey on my Mac assigned to run the script & show me the balance, using Ubersicht). Previously, I’d been using the blockchain.info API which allows multiple addresses, separated by a “I”. That’s fast and works, but part of the point of setting up my own server was to stop checking my balances on somebody else’s server.

Hopefully this makes more sense now.

Should I be using the Bitcoin CLI? The Electrs API? And if so, can I get some guidance?

Mempool is itself reaching out using the APIs of centralized exchanges. Again, price discovery is normally a centralized and outsourced task at this time, and the only Sovereign option I know of at this time is UTXO Oracle. Bitcoin and Electrs have no idea what the markets opinion of value in state-backed currency terms are, nor do they care. UTXO Oracle basically discovers USD price by using the patterns of humans (who tend to make exchanges in whole-dollar amounts, like - $1, 5, 10, 100, 1000, etc). Perhaps in the future, decentralized exchanges like Robosats can assist in distributing price discovery.

You could skip the middle man and use an exchange API, but again, that’s breaking the rule of only using your own server.

Moving to Development

I think the word “price” led you astray. I don’t mind using blockchain.info to get the price of BTC.

I’m trying to figure out a faster way to query specific info for all of my addresses by running a script on my Mac.

I want a quick way to poll 20 addresses for the total amount of sats in each and the total amount of outgoing transactions in each (which, in my case, should be 0 outgoing since these are my hodl addresses from various hardware wallets).

Here’s an example of how to do this with the blockchain.info API:

https://blockchain.info/balance?active=1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq|1MDUoxL1bGvMxhuoDYx6i11ePytECAk9QK|18AHE4ugTKUUwRJ33Khome779Lexk1bgbt|37awXBWZzFH1RGZVTHsXkc14uvrHs2gj2P|16z7dHHC4JvoUyExGLihrQGPqdd6LcmaH1|bc1q5u9520tn0kpkzyztstphhm47u6665wj2f4trzw|1JN2p8JUJqv2eM66Fs2BXuAT5tNtw73d8m|1819CWgd7sJVbpYCPKHh834jpoVRziftx2|bc1qtjcycxwhygzh0y624vm5j0t6mmupfpjcd8t2ma|bc1qtxrw2xww3vzcr4pcqwdk58550q6xgmjcnefa58

Done. One call, ten addresses. Instant info. But I don’t want to be polling my addresses on somebody else’s server.

The mempool API doesn’t have the ability to poll multiple addresses, as far as I know. Each address needs to be looked up individually. It works, but it’s very slow.

The data is on my server. I admit, I’m a noob at this sort of thing. I’m trying to figure out what’s the fastest way of polling multiple addresses. The Bitcoin CLI? The Electrs API? Or is there a way to poll multiple addresses via the mempool API?

I was thrown off by ‘price’ since you said you wanted everything in USD terms. I don’t have a quick answer for you. @George may have some insight, but this is a bit out of scope of StartOS. Have you tried to contact the teams behind electrs or mempool about this? They would much more readily know if/how to do what you are looking for.

Bitcoin doesn’t have an address index, only a transaction index (assuming you turned it on in Services > Bitcoin Core > Config). So you can only get information about certain transactions from the Bitcoin RPC.

For information about transactions to or from a given address, you need to query the electrs RPC. It’s a JSON RPC that complies with the latest Electrum Server protocol. As an example of a useful RPC call given a certain address, here’s the documentation for the call blockchain.scripthash.get_balance:
https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-methods.html#blockchain.scripthash.get_history

Construct your call by converting the address you’re interested in into a reversed scripthash:
https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-basics.html#script-hashes

Then you’d do something like:
echo 'YOUR RPC CALL HERE' | torify nc yourreallylongelectrsaddress.onion 50001

that will pipe the call into netcat which will relay it to the server via tor because it’s invoked with torify.

I have Bitcoin Core fully synced, not pruned. I have Electrs set up and running.

Can I do this without tor, since I’d be using my server on my network? And since I’m the only one who uses my network.

If you implement this:

Or wait for LAN port forwarding in StartOS v0.3.6 (due sometime Soon™),

Then you can remove the torify command and nc directly to your server’s .local on port 50001

I’ve followed the instructions to expose Electrs over LAN and that appears to have been successful.

Is there a guide somewhere for RPC calls? I’m trying things like getbalance, get_balance, get_address_balance, and I keep getting Error: 500

I’m probably missing something obvious here, but can somebody give me an example of an RPC call to get an address balance?

We do not make guides for third party software, except in regard to integration with StartOS. This requires scripting and is not just a straightforward command. This is out of scope of StartOS entirely. Perhaps someone will chime in with more tips, but have you tried the electrs community? They will have more ready answers, and possibly guides for what you want to do.

What you’re trying to do is not exactly a straightforward thing but probably the easiest way is: