NEW

Connect the world's APIs to Web3 with Chainlink Functions. Get started

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 then receive large responses.

Example

This example shows how to:

  • Call an API and fetch the response that is an arbitrary-length raw byte data.

IPFS is a decentralized file system for storing and accessing files, websites, applications, and data. For this example, we stored in IPFS a JSON file that contains arbitrary-length raw byte data. To check the response, directly paste the following URL in your browser: https://ipfs.io/ipfs/QmZgsvrA1o1C8BGCrx6mHTqR1Ui1XqbCrtbMVrRLHtuPVD?filename=big-api-response.json Alternatively, run the following command in your terminal:

curl -X 'GET' \
  'https://ipfs.io/ipfs/QmZgsvrA1o1C8BGCrx6mHTqR1Ui1XqbCrtbMVrRLHtuPVD?filename=big-api-response.json' \
  -H 'accept: application/json'

The response should be similar to the following:

{
  "image": "0x68747470733a2f2f697066732e696f2f697066732f516d5358416257356b716e3259777435444c336857354d736a654b4a4839724c654c6b51733362527579547871313f66696c656e616d653d73756e2d636861696e6c696e6b2e676966"
}

Fetch the value of image. To consume an API, your contract must import ChainlinkClient.sol. This contract exposes a struct named Chainlink.Request, which your contract can use to build the API request. The request must include the following parameters:

  • Link token address
  • Oracle address
  • Job id
  • Request fee
  • Task parameters
  • Callback function signature

Making a GET request will fail unless your deployed contract has enough LINK to pay for it. Learn how to Acquire testnet LINK and Fund your contract.

samples/APIRequests/GenericBigWord.sol

To use this contract:

  1. Open the contract in Remix.

  2. Compile and deploy the contract using the Injected Provider environment. The contract includes all the configuration variables for the Sepolia testnet. Make sure your wallet is set to use Sepolia. The constructor sets the following parameters:

    • The Chainlink Token address for Sepolia by calling the setChainlinkToken function.
    • The Oracle contract address for Sepolia by calling the setChainlinkOracle function.
    • The jobId: A specific job for the oracle node to run. In this case, the data is a bytes data type, so you must call a job that calls an API and returns bytes. We will be using a generic GET>bytes job that can be found here.
  3. Fund your contract with 0.1 LINK. To learn how to send LINK to contracts, read the Fund Your Contracts page.

  4. Call the data and image_url functions to confirm that the data and image_url state variables are not set.

  5. Run the requestBytes function. This builds the Chainlink.Request using the correct parameters:

    • The req.add("get", "<url>") request parameter provides the oracle node with the url where to fetch the response.
    • The req.add('path', 'image') request parameter tells the oracle node how to parse the response.
  6. After few seconds, call the data and image_url functions. You should get non-empty responses.

What's next

Stay updated on the latest Chainlink news