Getting data from the internet with Oraclize

Smart contracts can interact with data living in the blockchain but it is impossible to get informations from the web. Using Oraclize, we’ll see how your smart contracts can get data from outside the blockchain and execute any API call. Some use cases can be:

  • As seen in our article about the lottery, it’s hard to generating completely random data on the blockchain can be a problem, you could use Oraclize to get a random number from outside the blockchain.
  • In crowd sales, it could be interesting to know the current $ price of Ethereum or other tokens.
  • In a betting case, it could be interesting to get the result of a sport event from an external website.

First of all, you need to understand that reading data from the internet and manipulating money depending on the result requires to thrust an external source. Always keep in mind that you should have a way to pause and retrieve the funds in the smart contract in case of problems or change to the API you are reading from.

To try and test how Oraclize works, we’ll use the Remix interface they provide here. This interface let’s you discover some samples and test your own code for free on a simulated blockchain. The service is not free and require you to send funds to make queries, the costs inside described on their documentation.

The service works in 3 steps:

  1. You send a query to the Oraclize smart contract.
  2. Oraclize receive your query and make the corresponding request.
  3. Once they receive the data, they call a callback function of your smart contract where you’ll be able to access the requested data.


For this example we will write a simple smart contract that will fetch the current price of Ethereum in US dollars from the public API.

First of all, you need inherit form the usingOraclize  contract. If you don’t know about inheritance you should read our article. To import it’s source you can read and copy the content of the file on Github. Or import it directly from the repository putting the import at the beginning of your code file:

Once you imported the Oraclize API smart contract, you need to inherit from the usingOraclize .

This contract won’t do much, we now need to add the code to retrieve the price. using Oraclize is done in two step. The first is to inform the Oraclize about what we want by calling the oraclize_query  function. Once called with the parameters describing our query, the Oraclize smart contract will transfer the result to us by calling a function named __callback  we need to implement in our code.

We’ll first write our update function that will tell Oraclize about what we want. In our case:

You can note our update function has the payable  modifier as we should accept Ethereum to be sent as the oraclize_query  function require to send funds to the contract. Also note that first query made by a contract is free, so you can include call the update  function from your smart contract constructor without sending funds.

The query is specifying to retrieve a JSON content and directly retrieve one property of the object: the USD price.

The oraclize_query  accept two string parameters, the first string define the type of request and today we’ll use an URL  but you can also query the result of a WolframAlpha  formula or a IPFS  multi hash. It is also possible to add a third parameter in order to make the query delayed.

We now have to write our callback to receive the price:

The callback must be named __callback , the first parameter is the id of the request that can be saved when you make the request so you could handle different requests in the same callback. The second parameter is the result of your request. We use the parseInt  function that is inherited from the API file.

Here is our final smart contract:

From the Remix interface you first need to press the Compile  button to compile the contract to bytecode:

Then hit Create  button to publish your smart contract to the blockchain:


Once the contract is published, you will see the Log event appear:

If you click on the getPrice  button, you’ll see the price value appear in cents:


3 thoughts on “Getting data from the internet with Oraclize

Leave a Comment