Fork me on GitHub
Skip to page content
 

ColdFusion GOV Data SDK Sample

Overview

This sample will provide the steps to create a simple ColdFusion application that can consume GOV Data. Following these steps in order will allow your application to operate correctly. Download this project.


Create a New Project

For this sample we are going to create a web site that will consume and list Agency data.
Create a project folder for this sample.
Create a new text document using any text editor and save it as Sample.cfm in the project folder.

Add the SDK To Your Project

Copy the SDK folder into the project folder. You can get this from our GitHub Repository.

Locate Dataset Path

Each Dataset has a Dataset Location path that gives the URL that will be needed to reach the Dataset on the API. For the standard GOV Agency dataset sample please use http://api.dol.gov/V1/DOLAgency.

Define the SDK Object

This SDK is used in an object-oriented manner. This provides for cleaner code that is easier to use and maintain. Your first task is to create the object.

<cfscript> 
objSDK = CreateObject("component", "gov.dol.govAPISDK.SDK");
</cfscript>

The next step will be to create the shell of the HTML page:

<html>
<head>
	<style>
	td {
	border: 1px solid black;
	}
	</style>
</head>

<body>

</body>
</html>

Implementing the GOV Data Request

The rest of the code you will be writing will be between the body tags. These next tags could be placed up with the object instantiation above, but if you're combining data from multiple federal agencies, you will likely find yourself using these later on in the document as we are now. Let's set our object's properties (by default, they're "").

	<!--- get your key at http://developer.dol.gov --->
	<cfset objSDK.API_KEY = "{ add your own API key here }">
	<cfset objSDK.API_HOST = "http://api.dol.gov">
	<cfset objSDK.API_URL = "/V1">
	<cfset API_METHOD = "DOLAgency/Agencies">

Our next step will be to call the object's method that will give us our data.

	<cfscript>myAPICall = objSDK.submitRequest(API_METHOD);</cfscript><br>

Our last step is now to loop through the result and print it to the screen. As you use other agencies' APIs, you may want to cfdump your results first as the structure of the results will vary from API to API.

	<table>
		<tr>
			<th>Agency</th>
			<th>Full Name</th>
			<th>Web Site</th>
			<th>Mission Page</th>
			<th>Organizational Page</th>
			<th>Key Personnel Page</th>
		</tr>
		<cfloop index="agencyItem" array="#myAPICall.d.results#">
			<cfoutput><tr>
				<td>#agencyItem.Agency#</td>
				<td>#agencyItem.AgencyFullName#</td>
				<td>#agencyItem.WebsiteURL#</td>
				<td>#agencyItem.MissionURL#</td>
				<td>#agencyItem.OrganizationalURL#</td>
				<td>#agencyItem.KeyPersonnelURL#</td>
			<tr></cfoutput>
		</cfloop>
	</table>

The finished code should look like this:

<cfscript> 
objSDK = CreateObject("component", "gov.dol.govAPISDK.SDK");
</cfscript>

<html>
<head>
	<style>
	td {
	border: 1px solid black;
	}
	</style>
</head>

<body>

	<!--- get your key at http://developer.dol.gov --->
	<cfset objSDK.API_KEY = "{ add your own API key here }">
	<cfset objSDK.API_HOST = "http://api.dol.gov">
	<cfset objSDK.API_URL = "/V1">
	<cfset API_METHOD = "DOLAgency/Agencies">

	<cfscript>myAPICall = objSDK.submitRequest(API_METHOD);</cfscript><br>

	<table>
		<tr>
			<th>Agency</th>
			<th>Full Name</th>
			<th>Web Site</th>
			<th>Mission Page</th>
			<th>Organizational Page</th>
			<th>Key Personnel Page</th>
		</tr>
		<cfloop index="agencyItem" array="#myAPICall.d.results#">
			<cfoutput><tr>
				<td>#agencyItem.Agency#</td>
				<td>#agencyItem.AgencyFullName#</td>
				<td>#agencyItem.WebsiteURL#</td>
				<td>#agencyItem.MissionURL#</td>
				<td>#agencyItem.OrganizationalURL#</td>
				<td>#agencyItem.KeyPersonnelURL#</td>
			<tr></cfoutput>
		</cfloop>
	</table>
</body>
</html>