Got a cool example to share? Get listed here! Submit it now.

useChainlinkFunctions()

Collection of community submitted examples for Chainlink Functions

Get inspired from quick examples

Chainlink Functions, a new self-service platform that allows anyone to write serverless code to fetch any data from any API and run custom compute on Chainlink's network. Learn from examples created by the community or contribute your own!
Learn more about Chainlink Functions

Weight-Height results from Poke API

Submitted by:
Naman Gautam
This Function returns the Base Experience, Weight, Height of Pokemon. It uses the Poke API. Parameters includes name of pokemon.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 // This function fetches Base Experience, Weight, Height of Pokemon results from Poke API // Args include name of pokemon const pokiURL = "https://pokeapi.co/api/v2/pokemon" const pokemonCharacter = args[0] console.log(`Sending HTTP request to ${pokiURL}/${pokemonCharacter}/`) const pokiRequest = Functions.makeHttpRequest({ url: `${pokiURL}/${pokemonCharacter}`, method: "GET", }) // Execute the API request (Promise) const pokiResponse = await pokiRequest if (pokiResponse.error) { console.error(pokiResponse.error) throw Error("Request failed, try checking the params provided") } console.log(pokiResponse) // gets the Base Experience, Weight, Height of Pokemon const reqData = pokiResponse.data // Gives the whole response from the request console.log(reqData) // result is in JSON object, containing Base Experience, Weight, Height of Pokemon const myData = { base_experience: reqData.base_experience, weight: reqData.weight/10, // The weight of this Pokemon in hectograms which is converted into kilograms by dividing by 10 height: reqData.height/10, // The height of this Pokemon in decimetres which is converted into metres by dividing by 10 } // Use JSON.stringify() to convert from JSON object to JSON string // Finally, use the helper Functions.encodeString() to encode from string to bytes return Functions.encodeString(JSON.stringify(myData))