For example, I have two addresses and each of them has received 1 BTC. Now, I want to transfer 1.5 BTC to another address. How can I achieve this using the Blockcypher create transaction API?
2 Answers
Blockcypher API shows only the example of how to compose a transaction with a single input, therefore, it might look like the array of addresses will do the job, but no. If you'll do this by passing addresses into one array of inputs:
curl -d '{"inputs":[{"addresses":["addr1", "addr2"]}],"outputs":[{"addresses":["addr3"],"value":100000000}]}' https://api.blockcypher.com/v1/btc/test3/txs/new
You'll have only zero element grabbed from the addresses array from the inputs:
"tx": {
"block_height": -1,
"block_index": -1,
"hash": "002407bd35a6d0026cf0f5a018bd27311702560c03f1f64478105a547c91d895",
"addresses": [
"addr1", (inputs.addresses[0])
"addr3" (outputs.addresses[0])
] ...
(Please note that I haven't included the whole tx blocks, only the ones containing the addresses.)
API will consider that you're using only one input, and may throw you an error if the value will be too big than the input's wallet balance.
"Not enough funds in 1 inputs to pay for 1 outputs, missing -X"
To create the tx with several inputs, you'll have to pass arrays of addresses separately:
curl -d '{"inputs":[{"addresses":["addr1"]}, {"addresses":["addr2"]}],"outputs":[{"addresses":["addr3"],"value":100000000}]}' https://api.blockcypher.com/v1/btc/test3/txs/new
This will give you the correct response, and the correct amount of inputs:
"tx": {
"block_height": -1,
"block_index": -1,
"hash": "ebcc8132c33a672cdd695a8953db503954abefa18bfd4727ea44f67b92c5ab6c",
"addresses": [
"addr1",
"addr2",
"addr3"
] ...
In conclusion, it seems that the API itself is designed that way, at least at the moment of writing this answer.
Disclaimer: I have not tried this, I just glanced at the API documentation. H/T to eirlis for providing the correct syntax.
As the API documentation for Creating Transactions specifies, you provide the addresses that you received your funds to in the inputs
parameter. The API documentation has this example using curl:
curl -d '{"inputs":[{"addresses": ["CEztKBAYNoUEEaPYbkyFeXC5v8Jz9RoZH9"]}],"outputs":[{"addresses": ["C1rGdt7QEPGiwPMFhNKNhHmyoWpa5X92pn"], "value": 1000000}]}' https://api.blockcypher.com/v1/bcy/test/txs/new
For multiple addresses, you need to provide each address in a separate object with the property addresses
to the inputs object: {"inputs":[{"addresses":"addr1"}, {"addresses":"addr2"}],"outputs":…}
. According to the documentation, the response will be an unsigned transaction which you'll have to sign locally with the corresponding private keys. The BlockCypher documentation links to their "signer tool" at this point.
Note: all value not explicitly assigned to a recipient in a Bitcoin transaction will go to transaction fees. If you are using two 1 BTC inputs and are sending 1.5 BTC to a recipient, be sure that you add a second output to assign your 0.5 BTC – transaction fees
of change to one of your own addresses. Otherwise you will pay 0.5 BTC to the miner confirming your transaction. Be sure to verify that the transaction is doing what you intend to before signing it.
I would firmly recommend that you build a few transactions with testnet coins to get the hang of it.