Chainlink External Adapter Tutorial AWS + Python chainlink cryptocomparePut ANY API onto a smart contract!

rethereum on Reddit: How do you know the pools a wallet has ,I have the token transfers using the standard ETL, we can track the pools from the owner's wallet address and the tokens we support! That being said a pool address will appear on from or to the address, but I can't guarantee that that address is not the LP token or uni swap v3 or confirm is uniswap v2! How do you guys handle this? How To Buy XRP Ripple – Forbes Advisor XRP in the Digital Economy Ripple claims its payments network will usher in a “new global finance era.” Its products are used for payments and asset exchanges, similar to SWIFT, which provides chainlink cryptocompare Put ANY API onto a smart contract! Chainlink External Adapter Tutorial AWS + Python
chainlink cryptocompare Cardano Price predictions , 2025, and 2030.... Put ANY API onto a smart contract! Chainlink External Adapter Tutorial AWS + Python
whats up everyone michael here today ive got an awesome tutorial for you were going to be learning how to put any data from any api on the internet onto the blockchain chain link external adapters combined with a running chain link node allows you to call any api through the chain link interface from your smart contract if youre familiar with chain link you might know that there is something called a core adapter so why would you use an external adapter over a core adapter basically an external adapter allows you to do a few more things the main advantage is that you can do computations off-chain so if you want anything other than the raw api results for example if you want the maximum out of a series of values thats returned by the api you can do that computation off chain if youre using an external adapter this saves on gas for the ethereum contract it makes it cheaper to use the other reason is if youre using an api that requires a private key using an external adapter allows you to set environment variables and so this lets you use a private key to access the api during the tutorial were going to be using this github that chain link has provided its a template for an external adapter in python ill be sure to link this in the description below to get started make sure that you have git installed on your computer open the terminal and go ahead and clone the repository from chain link now we can see the into our repository and open it in our favorite text editor im going to use sublime now that we have the code open ill give you a quick run through so the most important file is this adapter file this is what were going to change in order to add our custom api you can see that right now they have this cryptocompare api in there as a placeholder the app file is used if you want to run the external adapter locally the bridge file contains helper code that is used to connect to the chain link bridge jumping back over to our adapter file which is where were going to be working for most of this tutorial we can take a look at the base url in our case were actually going to be using the noaa tides and currents api ill drop a link in the description lets go over to their api documentation so here we have the noaa api as you can see um they have documentation on the side for some of the different things lets go over to sample url and uh and see what we have here um so as you can see most of the api query data as to what were actually querying from the api is structured inside the url itself so this uh api specifically doesnt require parameters um on the post request you can just send the api um at the right url and youll get the data that you want one tool that were going to use to test our api is called boomerang so boomerang is a simple web tool that allows you to do simple api requests from your browser and its really useful for seeing the response and being able to add you can add headers so you can add a private key um you can add parameters and so this is a good way to test your api um that you set up once the external adapter setup but also to test and just kind of get familiar with apis from third parties so as you can see ive already put in the noaa titan currents api url into here and ive actually queried it once since this was a few days ago lets go ahead and change the date so in this date field begin date um we can see this was march 14th so lets change that to the 15th and end date also the 15th um and they have that actual times too and so our station id so basically what this is going to do is its going to pull data from a a weather station and this station specifically is going to be on virginia key which is in biscayne bay outside of miami um so lets go ahead and send this api request and see if we get a good response okay so we can see we got a response its from march 15th so theres metadata associated with the response and then actual data so now this api is working we can see the response data that were expecting lets go ahead and jump back over to our code and prepare our external adapter to handle this api because our api is url based and all the parameters are built into the url and handled on the apis back end we actually dont need these to and from param so we can go ahead and delete those it might be useful to actually run through the code really lets go ahead and run through and get an overall idea for what the code is doing at each step so this base url is going to be our url um so this init function so in python an init function is the first thing thats called when a class is initiated this id is a required attribute in chain link external adapters every call needs a job id request data is actually the request that were getting from the chain link node or from the smart contract so this is the data thats being input into our external adapter and so this is the data we can use to tailor our api requests to our external api validate request data is a function that you can see here on the screen and this is whats used to make sure that the request data is in the format that were expecting in our case were expecting a weather station id so what we could do if we wanted to was create a library of valid weather station ids and check to make sure that its part of that library if the validation fails which means the request coming from the smart contract is invalid for some reason this will trigger this self dot result error where we can send an error message back if we want to we can customize this to give more information about why its actually failing underneath the validate request data function were instantiating a bridge class object and this is just going to help us interact with the chain link bridge the next class is the set params class which you can see down here this is where we take information from our request data and use it to structure the parameters that were actually going to use in our api request to the third party api finally we have the create request function this is a function that takes all the information that we already have with the parameters that weve set uh with the information coming from the smart contract and then it tailors that into a request that is sent to our third party api then this is where we can parse the data from that request and decide what will be sent back to the chain link node and therefore the smart contract in the name of keeping the tutorial simple were only going to do a very basic external adapter and so some of these functions we wont be modifying or will be able to delete for validate request data we can go ahead and keep that the same and not do any fancy validation requests four set params so this is basically going through the params from the request data and setting them into our local grant function so we can actually go ahead and delete all of this the only parameter from the request data that were really worried about is our station id for our weather station so we can go ahead and get that by doing self.station id so this is saving um the results to the class a class variable which is station id that well be able to access later and then we can pull in request data thats coming in from the smart contract request thats going to be under self.request data we can call the got the dot get function to get a specific parameter and were going to get station id next were going to uh create the request so because were not using any of these parameters we can delete the whats inside the brackets we do still need to pass the parameters as an input to our bridge but we can leave that empty for now now we can go ahead and build our url to call the api so we actually dont need to put it here um it might not be the cleanest version but i like to go ahead and pull it down here because this is where were going to be working with it so go ahead and paste that in to our create request function and im going to go ahead and copy the the noaa api url that were going to be using okay ive copied that in and ill make sure to link that in the description but as you can see its a really long url but theres one part of the url that we want to change depending on the request data thats coming in and that is our station id so here you can see our station id we can change that using the python uh string editing so if you do some curly brackets here right where that thats portion of the string was and then you type dot format here we can add our station id in this case the station id is self dot station id okay so this response is actually sending the request to the api so this data is a response that were getting from our third party api im going to go ahead and add a comment just so we can keep our head straight okay the next thing we want to do is parse the response from the api its important that we do as much computation and parsing as we can inside of our external adapter because anything that you do inside of an ethereum contract or anything that you need to do is using gas cost so in our case we want to be able to provide the temperature data directly back to the smart contract so lets jump over to boomerang and see where our temperature data lies okay here we are in boomerang and we can see that in order to traverse the the response well have to go first into data and then theres actually a list of different um dictionaries and so we can just take the first element of that dictionary and well go ahead and take this this t uh this v is a value for the temperature okay so lets go ahead and access that so quick comment okay and were going to be putting our data into the self dot result attribute of this class and thats whats going to be used as you can see right here to put that into our data that we end up sending back to the smart contract so the actual data that we want to get is going to data and as you remember in the boomerang structure we have to go through data then take an element of the list and then we want v which is the temperature we can delete this this line which is the line that we just replaced and there we go were ready to go ahead and test out our external adapter go ahead and save this and then open up your terminal okay here i am in the terminal and im in this uh clea python template directory so in order to actually test our external adapter were going to have to install the dependencies so to do that if you list theres already a pip file and so we can just do pip-m install okay that all looks good now remember that its the app file right here thats actually able to run our external adapter locally so we can go ahead and run this using pipm run python3 laptop high which will run it through our pip bam virtual environment with all the dependencies okay it looks like were up and running and our api is running at uh this local port so go ahead and open up a new terminal window we can do it here we are in our new terminal with this command you can see that the data object that were passing in it has the id so remember that for chain link its required to have the job id and then it has this data parameter which is where we include our specific data that were going to parse through in our case thats the station id ill make sure that i put this command in the description so lets go ahead and run it and see if it works okay it looks like theres an error and luckily we have an error message adapter object has no attribute base url so lets go ahead back over to our code okay so lets go ahead and see if we can fix this no attribute base url so it looks like were using base url here and self.baseurl in the actual request so lets just delete that itself save that and see if we can go ahead and run our code again awesome it works the next step is to get this onto aws so that we can access it from anywhere not just locally so the last thing that we can do locally after testing our api is actually um put our external adapter into a zip file which is whats going to be used by our aws lambda function if you look at the github for the external adapter template they have good instructions on how to actually create the zip um so we can go ahead and copy those so this uh this lock function is basically taking the all of the packages that are being used in our pip environment and putting that into this requirements.txt file then were actually installing these from the requirements file into a package file so its all available in code and then were packaging everything including the package the main function adapter and bridge uh python files all into um a zip package which is what aws is actually going to read from in order to create the lambda okay so to make sure were in the right place and we can go ahead and just copy those commands right over so first well lock the current requirements next well install those into a package and then finally well put that all into a zip file using this final command and so now we should be able to see our zip file we have the so thats what were going to be using over in our aws lambda okay so here i am in our aws lets go ahead and go over to our lambda function so just uh you can search lambda and now what were going to do is were going to create a new function so go ahead and hit create function so were going to author it from scratch um choose any name you want for this im going to do uh youtube cl ea for external adapter uh demo and then for our runtime environment since we built everything in python were going to be using python 3.7 which is what what the docs on the template suggest that you use so um going to the execution role were just going to keep this selected go ahead and create a new basic role with uh lambda permissions and now we can go ahead and hit create function dont worry about the advanced settings for now okay so here we are in our function um successfully created so this code source is whats actually uh is the core of the functions the actual code thats being run for us we have everything in the zip file that we created so we can go ahead and upload from a zip file and then go ahead and choose the zip file on your computer to upload it into aws okay i have my zip file here this and so im going to save that okay so as you can see um we have our file here our zip file and we have if you just take a glance you can see like flask is here requests url libs so these are all um packages that we had installed in our zip file and then here are our main files um the adapter bridge and and main file so in a lambda function everything is run through a specific function in our in our code and so we need to select that function by default its this lambda handler function um and the function.lambdahandler but so as you can see um the handler is here um and so this is where all uh requests run through this is kind of like the main function for a uh lambda function um so this is kind of what directs like whats going to happen in the code um luckily chain link and their template they have a wrapper um to make this easy so we can go ahead and add this lambda function um and the handler were just gonna change that um so in the chain link template everything runs through the main dot lambda handler so set that domain lambda handler and then go ahead and save okay awesome so that part is all set the next step is we need to find a way to allow external sources like the chain link nodes to actually connect and make requests from our external adapter and to do that we need to add a trigger and were going to create an api gateway which is basically like creating a public endpoint for this lambda function so that people can call it from the public endpoint and it will operate just like an api um url so go ahead and select add trigger and then were going to choose api gateway so under api were going to choose create api and thats actually going to be a rest api for security in this case im going to set this to open if you have a relationship with a node provider or youre running your own private chain link node you might be able to create an api key for them to use um but for now for testing lets just do open okay we dont have to worry about the additional settings so lets go ahead and add that okay so now lets go into our api gateway trigger so now we have to set it up to actually work well with our lambda function so go to integration request and uncheck this lambda proxy integration go ahead and hit okay and okay so now this is allowing the api gateway to invoke our lambda function okay so now we have to basically reset our lambda function so lets go ahead and go back i have it open here so lets go to lambda functions um function overview again and so we have this api gateway we can go ahead and um delete that and then were just going to re-add it in order to reset that security okay so now we can hit add trigger and then for the trigger we can select api gateway and now we have our existing one in this case it was the youtube clea demo and for the deployment stage we can just do default okay thats all set last thing that we can do is actually test this api endpoint that weve set up on aws so lets go ahead look at details um we have the api endpoint here so go ahead and copy that and then go back uh to your terminal so we can test it locally on our computer okay so im back here um where we had our last command um to test it locally the only difference is were going to be changing the actual endpoint so here we have localhost 8080. so go ahead delete that and then paste in the link for your specific api endpoint and then go ahead you can keep the same station everything is going to be in the exact same format as when we tested locally and running it should give us the same response and then go ahead uh you can keep the same station everything is going to be in the exact same format as when we tested locally and running it should give us the same response awesome it worked as you can see we have all of our response here as expected you have officially created a chain link external adapter with a custom api great work so now that you have your chain link external adapter up and running with your aws endpoint um you need to get it onto a chain link node there are two ways to do that first you could just ask a node operator um you could post if you go to market dot link um you can post your external api here and you can also request uh that the operator run your external adapter looking at the chain link market if you go to adapters you can see the current adapters that the community has added so you can go ahead and add your adapter here if you want to actually ask someone to run it um you its a good idea to post it here but you can also go ahead on the chain link uh discord and ask a node um someone running a node to go ahead and run your external adapter the community is super helpful and they definitely get you up and running if you want to run your external adapter in your own node but you dont have a node yet go ahead and watch my last video on how to get a node chain link node up and running on aws if you already have a note up and running im going to explain how you can connect your chain link external adapter to that chain link node in my next video so go ahead and subscribe in order to see that if you like this video or if it was helpful please drop a like and a comment below it really helps out the channel and it really gives me the motivation um to know that im helping you guys and to make more videos like this and last but not least i really encourage you to check out the chain link discord and the chain link channel um they have a lot of really good resources and theyre actually starting a hackathon soon which would be a fun way to put some of these uh coding skills that youre learning to work if theres any other content youd like to see like videos on tokenomics or chain link research or even other tutorials for other projects drop a comment let me know and ill probably make a video on it thanks again for watching and ill see you in the next video Template on Github: NOAA API docs: NOAA API URL: 15:00&end_date=20210315 15:06&station=8723214&product=water_temperature&units=english&time_zone=gmt&application=ports_screen&format=json Boomerang: