NEW

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

Array Response

This guide explains how to make an HTTP GET request to an external API, that returns a json array, from a smart contract, using Chainlink’s Request & Receive Data cycle and then receive the needed data from the array.

Example

This example shows how to:

  • Call an API that returns a JSON array.
  • Fetch a specific information from the response.

Coingecko GET /coins/markets/ API returns a list of coins and their market data such as price, market cap, and volume. To check the response, you can directly paste the following URL in your browser https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false or run this command in your terminal:

curl -X 'GET' \
  'https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false' \
  -H 'accept: application/json'

The response should be similar to the following:

[
  {
    "id": "bitcoin",
    "symbol": "btc",
    "name": "Bitcoin",
    "image": "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
    "current_price": 42097,
    "market_cap": 802478449872,
    ...
  },
  {
    ...
  }
]

Fetch the id of the first element. 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/FetchFromArray.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 id is a string data type, so you must call a job that calls an API and returns a string. We will be using a generic GET>string 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 id function to confirm that the id state variable is not set.

  5. Run the requestFirstId function. This builds the Chainlink.Request using the correct parameters. The req.add("path", "0,id") request parameter tells the oracle node to fetch the id at index 0 of the array returned by the GET request. It uses JSONPath expression with comma , delimited string for nested objects, for example: '0,id'.

  6. After few seconds, call the id function. You should get a non-empty response: bitcoin

What's next

Stay updated on the latest Chainlink news