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:
- It’s hard to generate 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 lets you discover some samples and test your own code for free on a simulated blockchain. The service is not free and requires you to send funds to make queries, the costs inside described on their documentation.
The service works in 3 steps:
- You send a query to the Oraclize smart contract.
- Oraclize receive your query and make the corresponding request.
- 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 cryptocompare.com public API.
First of all, you need inheritance from the usingOraclize contract. If you don’t know about inheritance you should read our article. To import its 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:
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
Once you imported the Oraclize API smart contract, you need to inherit from the usingOraclize .
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract USDPrice is usingOraclize {
uint public price;
event Log(string text);
function USDPrice() {
Log("Contract created.");
update();
}
function getPrice() constant returns (uint) {
return price;
}
}
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:
function update() payable {
Log("Oraclize query was sent, waiting for the answer..");
oraclize_query("URL","json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD");
}
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 accepts 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.
function __callback(bytes32 _myid, string _result) {
require (msg.sender == oraclize_cbAddress());
Log(_result);
price = parseInt(_result, 2); // let's save it as $ cents
}
Here is our final smart contract:
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract USDPrice is usingOraclize {
uint public price;
event Log(string text);
function USDPrice() {
Log("Contract created.");
update();
}
function getPrice() constant returns (uint) {
return price;
}
function __callback(bytes32 _myid, string _result) {
require (msg.sender == oraclize_cbAddress());
Log(_result);
price = parseInt(_result, 2); // let's save it as $ cents
}
function update() payable {
Log("Oraclize query was sent, waiting for the answer..");
oraclize_query("URL","json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD");
}
}
You can use Remix or your favorite development tool to compile and deploy the contract. Once the contract is deployed you’ll see the events that are emitted from the execution of its constructor code.

After the service call back your contract. you’ll now see the price updated using the getPrice() function:
