Chainlink Developers

Welcome to the Chainlink documentation site. You'll find comprehensive guides and documentation to help you start working with Chainlink as quickly as possible, as well as support if you get stuck. Click here for an introductory walkthrough on how to create a Chainlink request on the Ropsten test network!

Request data using Chainlink

Chainlink will provide your smart contract with access to any external API you want to connect your smart contract with. You can begin by trying a Chainlink out on an Ethereum testnet.

Developers wanting to add external data to their contracts will need to import the Chainlinked contract into their project. The Chainlinked contract contains helper methods in order to simplify the process of making a Chainlink request on-chain for external data.

To obtain testnet LINK, visit the faucets below:

View the Addresses and Job Specs for information specific to your network.

If you have any questions, feel free to ask us on Gitter!

Add Chainlink to your project

Start off by adding Chainlink to your project which is hosted on GitHub.

With NPM:

$ npm install github:smartcontractkit/chainlink --save

With Yarn:

$ yarn add github:smartcontractkit/chainlink

Connect your smart contract to the Chainlink Network

Import Chainlinked.sol into your contract so you can inherit the Chainlinked behavior.

import "chainlink/solidity/contracts/Chainlinked.sol";

contract MyContract is Chainlinked {
  constructor()
    public
  {
    setLinkToken(0x20fE562d797A42Dcb3399062AE9546cd06f63280);
    setOracle(0x261a3F70acdC85CfC2FFc8badE43b1D42bf75D69);
    ...
import "chainlink/solidity/contracts/Chainlinked.sol";

contract MyContract is Chainlinked {
  constructor()
    public
  {
    setLinkToken(0x01BE23585060835E02B77ef475b0Cc51aA1e0709);
    setOracle(0xf18455e70984e8fda0ADbe2c8dD21509DBeFA218);
    ...
import "chainlink/solidity/contracts/Chainlinked.sol";

contract MyContract is Chainlinked {
  constructor()
    public
  {
    setLinkToken(0xa36085F69e2889c224210F603D836748e7dC0088);
    setOracle(0x4bd72fA5Bb512c1E1f5B65E6E09509C93aC01b45);
    ...

Requesting data

When a request is created on-chain from your smart contract for the available job specs, you will need to specify parameters about the data you want Chainlink to retrieve. For details on which parameters to specify, see our Adapters page.

Specifying parameters is accomplished by using methods within ChainlinkLib.Run which takes a key (as a string) and value. We have the following method available for different value types:

  • add: Adds a string
  • addBytes: Adds bytes
  • addInt: Adds a int256
  • addUint: Adds a uint256
  • addStringArray: Adds an array of strings

In the example below, we specify a "url" key for the HttpGet adapter, a "path" key for the JsonParse adapter, and a "times" key for the Multiply adapter.

function requestEthereumPrice(string _currency) public onlyOwner returns (bytes32 requestId) {
  ChainlinkLib.Run memory run = newRun(jobId, this, "fulfillEthereumPrice(bytes32,uint256)");
  run.add("url", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,JPY");
  run.add("path", _currency);
  run.addInt("times", 100);
  requestId = chainlinkRequest(run, LINK(1));
}

Here is another example displaying the use of a dot-delimited string for the JsonParse adapter.

function requestEthereumLastMarket() public onlyOwner returns (bytes32 requestId) {
  ChainlinkLib.Run memory run = newRun(jobId, this, "fulfillEthereumLastMarket(bytes32,bytes32)");
  run.add("url", "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD");
  run.add("path", "RAW.ETH.USD.LASTMARKET");
  requestId = chainlinkRequest(run, LINK(1));
  }

Finally, the example below shows using an array of strings to use the given input parameter in the JsonParse adapter.

function requestEthereumChange(string _currency) public onlyOwner returns (bytes32 requestId) {
  ChainlinkLib.Run memory run = newRun(jobId, this, "fulfillEthereumChange(bytes32,int256)");
  run.add("url", "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD,EUR,JPY");
  string[] memory path = new string[](4);
  path[0] = "RAW";
  path[1] = "ETH";
  path[2] = _currency;
  path[3] = "CHANGEPCTDAY";
  run.addStringArray("path", path);
  run.addInt("times", 1000000000);
  requestId = chainlinkRequest(run, LINK(1));
}

Receiving data

In the example above requestedDataCallback is the callback method specified in the requestEthereumPrice method.

Be sure to add the checkChainlinkFulfillment modifier to ensure that only Chainlink can call this method. This modifier is included with the Chainlinked contract.

function requestedDataCallback(bytes32 _requestId, uint256 _reportedPrice)
  public checkChainlinkFulfillment(_requestId)
{
  calculatePayout(_reportedPrice); // run your calculation with the reported data
  ...

With your Chainlinked contract written, you're now ready to get the Addresses and Job Specs!