NEW

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

Multi-Variable 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 multiple responses. This is known as multi-variable or multi-word responses.

Example

This example shows how to:

  • Fetch several responses in one single call.

Cryptocompare GET /data/price/ API returns the current price of any cryptocurrency in any other currency that you need. To check the response, you can directly paste the following URL in your browser https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC or run this command in your terminal:

curl -X 'GET' \
  'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC' \
  -H 'accept: application/json'

The response should be similar to the following:

{
  "BTC": 0.07297
}

The request above shows how to get the price of ETH against BTC. Now let say we want the price of ETH against several currencies: BTC, USD, and EUR. Our contract will have to support receiving multiple responses. To consume an API with multiple responses, your contract should inherit from ChainlinkClient. This contract exposes a struct called Chainlink.Request, which your contract should use to build the API request. The request should 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.

Assume that a user wants to obtain the ETH price quoted against three different currencies: BTC , USD and EUR. If they use only a single-word job, it would require three different requests. For a comparison, see the Single Word Response example. To make these requests more efficient, use multi-word responses to do it all in a single request as shown in the following example:

samples/APIRequests/MultiWordConsumer.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, you must call a job that is specifically configured to return ETH price against BTC, USD and EUR. You can find the job spec for the Chainlink node 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 btc, usd , and eur functions to confirm that the respective btc, usd , and eur state variables are equal to zero.

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

    • The req.add("urlBTC", "<cryptocompareETHBTCURL>") request parameter provides the oracle node with the url where to fetch the ETH-BTC price. Same logic for req.add("urlEUR", "<cryptocompareETHEURURL>") and req.add("urlUSD", "<cryptocompareETHUSDURL>").
    • THe req.add('pathBTC', 'BTC') request parameter tells the oracle node where to fetch the ETH-BTC price in the json response. Same logic for req.add('pathUSD', 'EUR') and req.add('pathEUR', 'USD'). Because you provide the URLs and paths, the MultiWordConsumer in the example can call any public API as long as the URLs and paths are correct.
  6. After few seconds, call the btc, usd , and eur functions. You should get a non-zero responses. The job spec for the Chainlink node in this example can be found here.

What's next

Stay updated on the latest Chainlink news