What is ColdFusion?
ColdFusion is a markup language used for creating websites. If you have never heard of this, you need go no further. The rest of what
follows will mean nothing to you. You must be familiar with ColdFusion for this tutorial to be meaningful.
TUTORIAL
Before you can begin, you will have to have a FMP API Key. Get this from FMP.
This is the CF code to use to obtain a short quote for a stock symbol (sym). Insert your API Key.
- The part of the code (line 5) after financialmodelingprep.com can be changed to any of the functions or endpoints offered by FMP, and there are many of them. This one is for a "quote-short". Use the URL from the FMP docs that is shown on the right side of their page. Substitue the stock symbol from their example with #sym#, which will be defined in your ColdFusion code.
- The line (8) that contains "DeserializeJSON" is the CF function that converts the JSON in the FMP response ("result.filecontent") to an array-structure that can be used in ColdFusion.
- Setting "MyData" equal to "result.filecontent" on line 8, makes "MyData" an array. It can have one or many elements. And it can have a child array name as well.
- Explore "MyData" using a "CFDump" to see if there is a child array name. If there is, then on the last line (9), "MyData[1]" is replaced with "MyData[1].arrayname[x]." Where "arrayname" is the child array name found and [x] is the child array element number. Many of the endpoints don't have a child array name. The [1] in "MyData[1]" is because this array only has one element.
- If the array, "MyData" has more than one element, the total number of elements, or the array length can be obtained with "N=arraylen(MyData)". Or if you are interested in a different array element, put whatever number in the subscript as desired... "MyData[7]", etc..
- Once you have "N", then you can loop through the data with cfloop index="x" from=1 to=#N#
- The structure key of interest can be specified. Instead of using "price" on line 9, use whatever you wish... "MyWhatever=MyData[1].structurekey" in last line. When you do a "CFDUMP" on "MyData" you will see the structure keys of the endpoint function that are available.
- If you wanted to see the quarterly dividend yields for a company for the last 130 quarters, then you use the FMP "dividendYield" endpoint, and loop thru the array elements, using "MyData[x].dividendYield" in the loop. cfloop index="x" from=1 to=130.
- One last tip. If the CFHTTP result is empty, i.e. no data found, then use an IF statement at the beginning of the code, to skip over the extraction process. i.e... like this... cfif result.filecontent neq "[ ]" ......code....... /cfif (of course use the cf tag brackets , omitted here).
This will avoid an error if the symbol can't be found or is wrong.
|