API Oracle

Connecting to any API with Chainlink enables your contracts to access to any external data source through our decentralized oracle network. We understand making smart contracts compatible with off-chain data adds to the complexity of building smart contracts.

Whether your contract requires sports results, the latest weather, or any other publicly available data, the Chainlink contract library provides the tools required for your contract to consume it.

Response Types

Make sure to choose an oracle job that supports the data type that your contract needs to consume. Multiple data types are available such as:

  • uint256 - Unsigned integers

  • int256 - Signed integers

  • bool - True or False values

  • string - String

  • bytes32 - Strings and byte values. If you need to return a string, use bytes32. Here’s one method of converting bytes32 to string. Currently, any return value must fit within 32 bytes. If the value is bigger than that, make multiple requests.

  • bytes - Arbitrary-length raw byte data

There are multiple way of responses supported like :

  • Single Word Responses

  • Multi Word Responses

  • Array Responses

  • Large Responses

This guide explains how to make an HTTP GET request to an external API from a smart contract using Chainlink’s Request & Receive Data cycle and receive a single response.

Using API Oracle

  • First step it to figure out the JobID of the API request you wanna make , more info that can be found below depending on the type of request and response

  • Next for interacting with the contracts , install datum-contract and import them as follow

  • Creating the contract instance with the oracle Address for the chain you are working with

  • Defining the jobID which you have decided , Here we are doing GET requests returning uint

  • Now we need to construct our request with the API Call we wanna perform , as follow

The Most important part is to perfectly build the req , by adding all the parameters like , type of req , API Link , response path , and response Customisation Detailed guide on how to create the req method can be found on Chainlink Docs , there are various examples of different API requests being made

  • In the backend , the tasks will be performed and the response will be stored back in the Oracle , which can be queried again as follow :

Full Contracts

Extra API req Examples

Weather Data

Job ids can be found here

Last updated