Can I use a Chainlink Function to dowload a file?


I found this example Chainlink Function ``` // This example shows how to make a decentralized price feed using multiple APIs // Arguments can be provided when a request is initated on-chain and used in the request source code as shown below const coinMarketCapCoinId = args[0] const coinGeckoCoinId = args[1] const coinPaprikaCoinId = args[2] const badApiCoinId = args[3] if ( secrets.apiKey == "" || secrets.apiKey === "Your coinmarketcap API key (get a free one:" ) { throw Error( "COINMARKETCAP_API_KEY environment variable not set for CoinMarketCap API. Get a free key from" ) } // To make an HTTP request, use the Functions.makeHttpRequest function // Functions.makeHttpRequest function parameters: // - url // - method (optional, defaults to 'GET') // - headers: headers supplied as an object (optional) // - params: URL query parameters supplied as an object (optional) // - data: request body supplied as an object (optional) // - timeout: maximum request duration in ms (optional, defaults to 10000ms) // - responseType: expected response type (optional, defaults to 'json') // Use multiple APIs & aggregate the results to enhance decentralization const coinMarketCapRequest = Functions.makeHttpRequest({ url: `${coinMarketCapCoinId}`, // Get a free API key from headers: { "X-CMC_PRO_API_KEY": secrets.apiKey }, }) const coinGeckoRequest = Functions.makeHttpRequest({ url: `${coinGeckoCoinId}&vs_currencies=usd`, }) const coinPaprikaRequest = Functions.makeHttpRequest({ url: `${coinPaprikaCoinId}`, }) // This dummy request simulates a failed API request const badApiRequest = Functions.makeHttpRequest({ url: `${badApiCoinId}`, }) // First, execute all the API requests are executed concurrently, then wait for the responses const [coinMarketCapResponse, coinGeckoResponse, coinPaprikaResponse, badApiResponse] = await Promise.all([ coinMarketCapRequest, coinGeckoRequest, coinPaprikaRequest, badApiRequest, ]) const prices = [] if (!coinMarketCapResponse.error) { prices.push([coinMarketCapCoinId].quote.USD.price) } else { console.log("CoinMarketCap Error") } if (!coinGeckoResponse.error) { prices.push([coinGeckoCoinId].usd) } else { console.log("CoinGecko Error") } if (!coinPaprikaResponse.error) { prices.push( } else { console.log("CoinPaprika Error") } // A single failed API request does not cause the whole request to fail if (!badApiResponse.error) { prices.push(httpResponses[3].data.price.usd) } else { console.log( "Bad API request failed. (This message is expected to demonstrate using console.log for debugging locally with the simulator)" ) } // At least 3 out of 4 prices are needed to aggregate the median price if (prices.length < 3) { // If an error is thrown, it will be returned back to the smart contract throw Error("More than 1 API failed") } const medianPrice = prices.sort((a, b) => a - b)[Math.round(prices.length / 2)] console.log(`Median Bitcoin price: $${medianPrice.toFixed(2)}`) // The source code MUST return a Buffer or the request will return an error message // Use one of the following functions to convert to a Buffer representing the response bytes that are returned to the client smart contract: // - Functions.encodeUint256 // - Functions.encodeInt256 // - Functions.encodeString // Or return a custom Buffer for a custom byte encoding return Functions.encodeUint256(Math.round(medianPrice * 100)) ``` This function just fetches the price of bitcoin. Could I use a Chainlink function to download a file from IPFS? If so, what would it look like?

Answers 1

IPFS provides HTTP endpoint allowing to retrieve content with [GET request]( ``` GET /ipfs/{cid}[/{path}][?{params}] ``` `Functions.makeHttpRequest` should work for getting content from IPFS. ``` const fileCid = 'QmcfhgdUN7knC5JSkjpGtGSdQz4xdGyxNgfp8hUqKbyysc'; const fileUrl = `${fileCid}`; const ipfsRequest = Functions.makeHttpRequest({ url: fileUrl, }); const fileContent = await ipfsRequest; ```