Search…
Guide
We'll take you through the steps required, so that you can start accepting Crypto in your projects today.

Rundown

Let us tell you exactly how BlockchainAPI works.
The way it works is like this:
    1.
    The developer creates an address using BlockchainAPI's BTC or ETH endpoint. This will return a unique address for your customers to pay to. (Because it's unique, it means we can track it).
    2.
    When a payment is sent to an address generated by BlockchainAPI, our system will automatically notice the in-coming funds and forward them to the destination address you supplied when creating a unique address through our endpoint.
    3.
    At the stage the payment is forwarded and hits 1 confirmation, data is sent to your callback URL you supplied when generating a unique address.
    4.
    From there, you can dig into the values of the data sent by BlockchainAPI to make sure the payment amount is correct, the destination address is correct, etc.
It's really simple. No long, tiring pieces of code. No registration. Straight to the point.
Within 1 single GET request, you can have customers paying you.
If you'd like to catch the payment information sent back to you, you'll need 1 extra piece of code in your projects... the callback checker.

Generating an address

Like we said above, the first thing to do is generate a unique address for your customers to pay to.
The following code uses CURL to send a GET request to BlockchainAPI's create endpoint.
PHP
1
<?php
2
$secret = "7j0ap91o99cxj8k9";
3
$my_address = "1LisLsZd3bx8U1NYzpNHqpo8Q6UCXKMJ4z";
4
$addressType = "bech32"; // bech32 | legacy | p2sh-segwit
5
$my_callback_url = "http://example.com/callback?invoice_id=1234&secret=" . $secret;
6
$api_base = "https://blockchainapi.org/api/btc";
7
8
$curl = curl_init();
9
curl_setopt_array($curl, array(
10
CURLOPT_RETURNTRANSFER => 1,
11
CURLOPT_URL => $api_base . "?method=create&address_type=".$addressType."address=" . $my_address . "&callback=" . $my_callback_url
12
));
13
14
$response = curl_exec($curl);
15
$http_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
16
curl_close($curl);
17
18
if ($http_status_code == 200) {
19
20
$decoded = json_decode($response, true);
21
22
echo "Please send the payment to the following Bitcoin address: " . $decoded["success"]["input_address"];
23
echo "The current estimated fee is: ".$decoded["success"]["estimated_transaction_fee"].' BTC';
24
25
} else {
26
echo "Sorry, an error occurred: " . $response["error"];
27
}
28
?>
Copied!
Use the below image to help you understand which parameters are required and which are optional.
We can see that the only 2 required parameters are address and callback... the rest have default values in place for if no values are entered.
Running the above code, returned this to us in json.
We can see that we have a value called input_address.
The input_address is the unique address that was generated by BlockchainAPI's system.
The destination_address is the address we supplied in the code above under address. It's the address that payments will be forwarded to once they've been received by BlockchainAPI.
From this point, you can fully accept Crypto into your applications.
If however, you would like to get notified about the payments sent to your unique address, you'll need some additional code... not much but a little bit more. (See the section below).
JavaScript
1
{
2
"success": {
3
"fee_percent": 1,
4
"destination": "3EfVLg7GiUcrW9PMMB7ktqL5yTk4VFjBda",
5
"input_address": "3JCUUPmbGeg2tJtYV77JJjhc8VLn3VnAgp",
6
"callback_url": "https%3A%2F%2Fwww.goole.org%2Fuser%2Fdonate%2Fcoin.received%3Finvoice_id%3D1308%26amp%3Bsecret%3D9zi8vF8W2GLpFHaRui4611",
7
"estimated_transaction_fee": "0.00001000",
8
"miners_fee": "0.00002200",
9
"tor": "false",
10
"setting": "catch_instant"
11
}
12
}
Copied!

Checking Callbacks

So with the above code, you'll be able to fully accept Crypto into your projects... however, you will not be able to check the payment details such as the amount sent, the address sent to, the fees sent etc.
This is where the callback checker comes into play.
When BlockchainAPI forwards a payment, it sends data along with it, to the callback URL supplied when creating the unique address in step 1.
Let's check an example of the data that is sent to us from BlockchainAPI.
Everything in the left column are POST values that're sent to your callback URL when a payment is forwarded to you. It is up to you if you wish to use all of these values but they're there for reasons such as confirming the amount sent or confirming the destination address matches yours.
You'll need to make a callback file and place something like the following inside it:
PHP
1
<?php
2
// Payment Settings
3
$my_address = "1LisLsZd3bx8U1NYzpNHqpo8Q6UCXKMJ4z";
4
$desiredConfirmations = 3;
5
6
// If you're using a secret, put it in here
7
$secret = "7j0ap91o99cxj8k9";
8
9
// Start of checks
10
if(!isset($_GET['secret'])) {
11
die('No secret found.');
12
}
13
14
if ($_GET["secret"] !== $secret) die();
15
16
// Make sure we have all the POST variables we want to get
17
if(!isset($_POST['input_address']) || !isset($_POST['input_transaction_hash']) || !isset($_POST['transaction_hash']) || !isset($_POST['value']) || !isset($_POST['confirmations']) || !isset($_POST['destination_address'])) {
18
die('One of more of the POST variables was not set in the request to our callback url.');
19
}
20
21
if ($_POST["destination_address"] !== $my_address) die();
22
23
// Now we can show them the 200 status code as they've passed our checks
24
http_response_code(200);
25
26
$input_address = $_POST["input_address"];
27
$input_transaction_hash = $_POST["input_transaction_hash"];
28
$transaction_hash = $_POST["transaction_hash"];
29
$value_in_btc = $_POST["value"];
30
$confirmations = $_POST["confirmations"];
31
32
// Confirmation check to make sure the confirmations is above our desired amount
33
if($confirmations >= $desiredConfirmations) {
34
// We should store the payment as soon as we receive it inside this callback file
35
// We can later update details such as confirms if needed
36
// {{ Do your magic here }}
37
} else {
38
// Transaction has not reached our desired number of confirmations.
39
// Keep waiting for confirmations to be larger
40
}
41
?>
Copied!
Checking the code, we can see that we define my_address and secret so we can make sure the values here match the values we set when creating a unique address in step 1.
Let's call it some sort of security measure.
Looking further down the code, we can see that we parse a 200 http response code after our checks. We can tell the server "Yeah good mate, all checks passed!".
In the final step of the code, we can see that we check the confirmations value.
With Bitcoin, payments are not Completed until they've reached 6 confirmations.
BlockchainAPI gives you the ability to set this to whatever you want... we'll post the confirmations integer for you to check whatever you like.

It's that easy. 1....2..... done!

Last modified 3mo ago