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.
DATUM only Support GET requests with uint responses currently , in the test phase
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 integersint256- Signed integersbool- True or False valuesstring- Stringbytes32- Strings and byte values. If you need to return a string, usebytes32. Here’s one method of convertingbytes32tostring. 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
Check if there is a req built for your JobId by calling buildAPIRequest , if it returns a non default response , meaning there is a request built already ,
Either Call requestBuildReq , and the Oracle will build one for you , as follow
Now we need to construct our request with the API Call we wanna perform , as follow
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
With these simple steps , we can use Chainlink's API Oracle to fetch off chain data on chain in a decentralized manner
Last updated
