In this article I’ll be showing you how to get started developing applications which communicates with the Coinbase API. But first what is Coinbase?
Coinbase is a service that allows you to use bitcoins as a means of payment for goods and services.
Its like Paypal but for bitcoins.
First thing that you need to do is to register an account with Coinbase.
Then go to your email and verify the account.
Accept the license.
Next, click on account settings and then click on the integrations tab:
Next, click on show my API key. You would need to enter your password to verify that you are indeed the account owner. Initially the API key is disabled so you need to enable it as well.
After that, simply copy the API key that’s displayed.
The API key will be used for authenticating requests to the Coinbase API.
There are 2 ways in which a request to the API can be authenticated:
- API key
Authentication using API Key
Authenticating a request via the API Key is the easier way of making requests to the API. All you have to do is to append the api key on each request. The cool thing is you can directly execute a request to the API directly from the browser for methods that can be called via
For example when you want to get the account balance, you simply do something like:
There’s a bunch of other methods which you can call directly from the browser. They’re all listed here. Do note that you can only call a method directly from the browser when its request method is
POST methods cannot be called directly from the browser, you need to use
file_get_contents() to be able to make the request.
Here’s an example of using
curl to generate a payment button, remember to pass the API Key along with the parameters required by the specific API method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
The method that we have used above is the
buttons method. It only requires 3 arguments:
button[name] – the name of the item or service for which you are collecting bitcoins.
button[price_string] – the total price of the item or service.
button[price_currency_iso] – the currency of the price used in the
button[price_string]argument. Examples are
BTCfor the bitcoin currency. It would be easier if
BTCisn’t used so you won’t have to convert. Simply use your local currency and the API will automatically convert it to
BTCdepending on the current exchange rate.
The response would be formatted in JSON:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
To generate the payment button, simply create a div with a class of
coinbase-button then give it a data attribute
data-code using the button code as its value. After that create a new script element and use the
button.js from coinbase:
The response returned above can also be used for generating payment pages. All you have to do is append the button code to the coinbase checkout url:
1 2 3 4
Authentication using OAuth2
Authentication using OAuth2 is a bit difficult. Thankfully there are good people out there who creates libraries that makes our lives easier. One of those libraries is the Coinbase-PHP library.
To use it, simply download the zip file from the Github repository or clone it on your machine. Once the download is done, you can just include it on your working script:
1 2 3
After that, you can go ahead and call the methods available from the library. In the example below were calling the
getOrders() method which simply returns all the orders received by the merchant who owns the API Key that is used in the code. This means that every merchant who plans to integrate Coinbase in their application has to create their own Coinbase app in order to receive an API Key. The API Key will then serve as their identification for each request that is made to the API. Yes this is a bit of a drag for the merchants but that’s just how it works so there’s no choice but to stick with it.
1 2 3 4 5
The response returned from the code above is not the same as the response that you directly get from the API. The library already converts it to an object so there’s no need to. Here’s an example of looping through the orders:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
You’ve learned how to perform requests to the Coinbase API. Requests can be performed directly from the browser or via Curl. You have also learned how to generate payment buttons via the API.