USDA National Farmers Market Directory API
Use of this API is subject to the Terms of Service as outlined by USDA's Agricultural Marketing Service.
This service provides search and data retrieval functions for the USDA AMS farmers market directory. The data is returned in JSON format.
This service can be consumed by AJAX Javascript. Scroll down to see an example.
This service can be consumed inside .NET code by creating a client and using it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:
svcutil.exe http://search.ams.usda.gov/FarmersMarkets/v1/data.svc?wsdl
This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:
C#
class Test { static void Main() { dataClient client = new dataClient(); // Use the 'client' variable to call operations on the service. // Always close the client. client.Close(); } }
Visual Basic
Class Test Shared Sub Main() Dim client As dataClient = New dataClient() ' Use the 'client' variable to call operations on the service. ' Always close the client. client.Close() End Sub End Class
These are restful services. They can also be called in Javascript without creating a client class. The functions will return JSONP objects.
zipSearch returns data in the JSONP format, in this example searchResultsHandler is the name of the callback method;
searchResultsHandler(
{
"results": [
{"id":"1002192", "marketname":"1.7 Ballston FRESHFARM Market"},
{"id":"22008839", "marketname":"1.9 Ballston Farmers Market"},
{"id":"1002291", "marketname":"2.1 Four Mile Run Farmers & Artisans Market"},
...
{"id":"1002181", "marketname":"4.4 Dupont Circle FRESHFARM Market"}
]
}
);
the id can be used to retrieve detailed information about the market. The number before the market name is the distance in miles the market is from the center of the zip code that was passed in.
Here is an example of using the service from JavaScript with JQuery;
JavaScript
function getResults(zip) { // or // function getResults(lat, lng) { $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", // submit a get request to the restful service zipSearch or locSearch. url: "http://search.ams.usda.gov/farmersmarkets/v1/data.svc/zipSearch?zip=" + zip, // or // url: "http://search.ams.usda.gov/farmersmarkets/v1/data.svc/locSearch?lat=" + lat + "&lng=" + lng, dataType: 'jsonp', jsonpCallback: 'searchResultsHandler' }); } //iterate through the JSON result object. function searchResultsHandler(searchResults) { for (var key in searchresults) { alert(key); var results = searchresults[key]; for (var i = 0; i < results.length; i++) { var result = results[i]; for (var key in result) { //only do an alert on the first search result if (i == 0) { alert(result[key]); } } } } }
mktDetail returns data in the JSONP format, in this example detailResultHandler is the name of the callback method;
detailResultHandler(
{
"marketdetails": {
"GoogleLink":"http://maps.google.com/?q=38.881112%2C%20-77.112179%20(%22Ballston+FRESHFARM+Market%22)",
"Address":"901 N Taylor St, Ballston, Virginia, 22203",
"Schedule":"June - October Thursday 3:00 PM to 7:00 PM",
"Products":"Baked goods; Cheese and/or dairy products; Eggs; Fresh fruit and vegetables; Fresh and/or dried herbs; Honey; Meat; Poultry"
}
}
);
Here is an example of using the service from JavaScript with JQuery;
JavaScript
function getDetails(id) { $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", // submit a get request to the restful service mktDetail. url: "http://search.ams.usda.gov/farmersmarkets/v1/data.svc/mktDetail?id=" + id, dataType: 'jsonp', jsonpCallback: 'detailResultHandler' }); } //iterate through the JSON result object. function detailResultHandler(detailresults) { for (var key in detailresults) { alert(key); var results = detailresults[key]; alert(results['GoogleLink']); } }