As blockchain becomes more and more widespread and decentralized applications are emerging as solutions in growing number of industries to solve real-world problems there is a need for blockchain(s) to interact with the world outside of their own. The way blockchain itself is designed provides no straight-forward way to interact with standard APIs living on non-blockchain systems.

To tackle this drawback a solution in form of “oracles” is used. Oracles are sources of data living outside of blockchain. They provide a way to access APIs to obtain various kind of data such as current prices of certain goods, weather information, sports match results and so on.

In this post I will describe how to use Oraclize, a “leading oracle service for smart contracts and blockchain applications”. A great advantage of Oraclize is that the existing APIs do not need to be modified in any way in order to be consumed by blockchain applications.

A number of data sources are available to use as oracles:

  • URL
  • random
  • WolframAlpha
  • IPFS
  • computation

Detailed explanation of all data sources is available here. In this post we will focus on the URL as a data source.

Getting the price of Bitcoin

Below you can see a simple Solidity smart contract in which we get the current price of Bitcoin expressed in euros. We will obtain the data from freely-available API provided by cryptocompare. It is a standard API which returns a JSON response in the following form:

 EUR: 5304.35

The smart contract code looks like this:

pragma solidity ^0.4.20;
import "";

contract BitcoinPrice is usingOraclize {
    uint public BitcoinPriceEUR;

    event newOraclizeQuery(string description);
    event newBitcoinPrice(string price);

    function BitcoinPrice() {

    function __callback(bytes32 myid, string result) {
        if (msg.sender != oraclize_cbAddress()) throw;
        BitcoinPriceEUR = parseInt(result, 2);
    function getPrice() payable {
        newOraclizeQuery("Successfully made an Oraclize query. Waiting for a response.");
        oraclize_query("URL", "json(");

Since the latest version of oraclize API smart-contract specifies compiler version to be between 0.4.18 and 0.4.20 we specify 0.4.20 in our contract.

One thing to note is that you should probably use oraclize_getPrice method in order to calculate the cost of executing the call.

For example, if you have set your gas limit for transaction to 100000 then you could use:

uint price = oraclize_getPrice(URL, 100000)

And then compare the price to your account balance.

In this post you saw how simple it is to call standard APIs that are “living” outside of blockchain and get some useful data by using Oraclize. This was a straight-forward and minimal example but there is a lot more to Oraclize such as scheduling queries to be executed in future and authenticity proofs. You can learn more about those and other topics in their official documentation.