Getting Started with BlockCypher APIs
A quick start guide
This guide will help you get started with BlockCypher! Learn how to create a BlockCypher API key, make your first request, do your first transaction on the BlockChain and get to building!
Steps to get started with BlockCypher APIsIn this short tutorial, you'll learn how to use BlockCypher APIs. Here are the steps that we are going to follow:
Get your free API token
Make a request
Create your first app
Start building
1. Get your free API tokenGetting a free API token is easy.
First create an account on our developer portal.
Copy-paste the token and save it somewhere. In this tutorial, we will always refer to our token as $TOKEN.
That's it, you have you free API token. Now, let's do our first request!
2. Make a requestYou can interact with the different blockchains through our BlockCypher infrastructure using a REST API. We assume here that you know how to use the terminal on your computer.
In this example, we are going to query the Bitcoin blockchain to know what is the current height and current block hash. We will do so from the terminal using curl.
Note: You can use BlockCypher APIs without a token but you will be more rate limited.
Request
> curl ?token=$TOKEN
Response
{
"name":"BTC.main",
"height":722792,
"hash":"000000000000000000070dd94adfd2d3e11c3c57aed0b752f73e6d6f9c1ebe43",
"time":"2022-02-11T16:53:29.772484772Z",
"latest_url":"https://api.blockcypher.com/v1/btc/main/blocks/000000000000000000070dd94adfd2d3e11c3c57aed0b752f73e6d6f9c1eb",
"previous_hash":"0000000000000000000521c2e096c5734c9dafa22064acadbb7c2eb1a908ff2d",
"previous_url":"https://api.blockcypher.com/v1/btc/main/blocks/0000000000000000000521c2e096c5734c9dafa22064acadbb7c2eb1a908ff2d",
"peer_count":1306,
"unconfirmed_count":11205,
"high_fee_per_kb":18969,
"medium_fee_per_kb":7250,
"low_fee_per_kb":3778,
"last_fork_height":714367,
"last_fork_hash":"0000000000000000000b2e70d7675bc7b4e89d384d0e6e1a7ecc2779e1d93244",
}
Congrats! You did your first query on BlockCypher APIs. If you have no idea what you are you reading check out the in the documentation.
Easy huh? And that's only the beginning! In our next section, we will build our first app using BlockCypher APIs.
3. Create your first app 3.1 SetupFor this example, we are going to use BlockCypher Python SDK. We assume that you have a computer with Python3 installed.
In this example app, we will use the BlockCypher Testnet (BCY). It's similar to Bitcoin Testnet but with a more predictable behavior and faster block time. BlockCypher Testnet is ideal to prototype your apps before deploying them.
First, let's install the library:
> pip3 install blockcypher
This should take a few seconds. After that, you're done with the installation part.
Now, let's create a Python file. We will call it start.py.
> touch start.py
Now, open the file in your favorite text editor and write the following.
import blockcypher
token = "$TOKEN"
last_height = blockcypher.get_latest_block_height(coin_symbol='bcy',api_key=token)
print("The latest BCY block height is:", last_height)
When running the code you should get something similar to the following (with a higher block height of course):
> python3 test.py
The latest BCY block height is: 155273
Now that we are comfortable with the SDK, let's generate a keypair. Just add the following line at the end of your file.
keypair = blockcypher.generate_new_address(coin_symbol='bcy',api_key=token)
print("My address is", keypair['address'])
This query will generate a keypair containing a private key (hex and wif format), a public key and an address.
> python3 test.py
The latest BCY block height is: 155273
My address is CFZozU7kbehVbgyXmtQ7hxSNhyXsczz8bP
Now let's request some testnet coins.
3.2 Get testnet coinsGetting some testnet coins is easy. Just query our faucet by adding the following line to your test.py file.
faucet_tx = blockcypher.send_faucet_coins(
address_to_fund=keypair['address'],satoshis=100000,coin_symbol='bcy',api_key=token)
print("Faucet txid is", faucet_tx['tx_ref'])
This query will request 100000 bcy satoshis from the faucet. This query will generate a keypair containing a private key (hex and wif format), a public key and an address.
> python3 test.py
The latest BCY block height is: 155321
My address is BvCU7QsbkgKdzUXE26FMF5diPP5Ra3ZkdE
Faucet txid is 77f00a267357b6ae183359654a4681a1633b5c30bfc00b5d29d055e111fa016d
You can see this transaction on our BCY Explorer. Now let's do a transaction from this address.
3.3 Do a transactionDoing a transaction using the python SDK is easy. We are going to send back 100 satoshis to the faucet. The faucet address is CFr99841LyMkyX5ZTGepY58rjXJhyNGXHf. Now let's add the following to our code:
faucet_addr = "CFr99841LyMkyX5ZTGepY58rjXJhyNGXHf"
tx_ref = blockcypher.simple_spend(
from_privkey=keypair['private'],to_address=faucet_addr,to_satoshis=100,coin_symbol='bcy',api_key=token)
print("Txid is", tx_ref)
Running all the above code should give you the following result:
> python3 test.py
The latest BCY block height is: 155321
My address is C3JjurQh9XDNWjoPm9VeNqqzuxB2UAXKN3
Faucet txid is 77f00a267357b6ae183359654a4681a1633b5c30bfc00b5d29d055e111fa016d
Txid is ab1560a2bc466005ea2bd3ae817ecf7f52aeaf202ca5a19b9cb8610c4c0512c8
Easy isn't it? You can see this transaction on our BCY Explorer.
3.4 ConclusionCongrats! You just wrote your first blockchain app using BlockCypher APIs 🎉.
This was a short example on what you can do with the BlockCypher APIs. But here we've just scratched the surface.
Here this example was done with Python SDK but we have several SDK. Check it out!
4. Start buildingNow that you are familiar with the BlockCypher APIs, it's time to build!
Please read the official documentation for more info and if you have any questions contact us.