Overview of Food and Nutrition Public API
Food and Nutrition 1.0 from CORMAC is an API designed to assist consumers in locating specific agricultural products that are available for sale in a given geographic area. When a user queries the API and specifies both their zip code and a health condition, e.g. Hypertension or Diabetes, it will return a list of associated herbs considered to be beneficial dietary additions for people with those conditions, as well as the nearest Farmers Markets which have that class of products available for purchase. The United States Department of Agriculture (USDA) periodically publishes a list of Farmers Markets across the country, including an overview of participating vendors’ regular inventory. This initiative supports local growers by bringing in business from within the neighborhood, and encourages community engagement by easily showing that the ingredients for a healthy lifestyle are available nearby. CORMAC’s custom API leverages this USDA product availability data and cross-references it with standard recommendations for those affected by chronic conditions, providing a convenient readout of what herbal products are recommended and where they might be purchased.
Getting Started - Accessing the API
To run queries on the API, go to: http://ec2-3-89-177-3.compute-1.amazonaws.com/playground This link leads to GraphQL Playground, an in-browser IDE for running GraphQL Queries. If the goal is building a frontend (like Apollo Client) or making calls to the API programmatically, the endpoint is: http://ec2-3-89-177-3.compute-1.amazonaws.com/graphql
Creating a user
In order for one’s application to make use of (fetch) data via an API, the receiving application (e.g., the application fetching the data) must have an authentication token. Authentication tokens are created via a user account with the sending system and can be generated by running the following query:
mutation {
addUser(data: {
name:"Username",
email:"name@example.com",
password:"password"
}) {
user {
_id
}
token
}
}
This will return the following result, including the token, which is needed to authenticate requests.
{
"data": {
"addUser": {
"user": {
"_id": "5245b4d5debdeedc67d3f833"
},
"token": "eyJhbGciOeJIUzI1NiIsInR5cCI6IkpXVCf9.eyJfdWQiOiIsZYmRlZTBjNjdkMzk4MzMiLCJpYXQiOjE1NjQ4NDkzNjs9.Li_s6ZL6Bs__buQiuUcDU4FBgTcpu7dpN1CaStBONhk"
}
}
}
Each time a request is made to the API, it is necessary to create an HTTP Header called Authorization (exactly as shown). For the value, create a string that starts with bearer Bearer (with an extra space) and add the authentication token after it. If working in the GraphQL Playground, click the button that says HTTP Headers and in it, put the following json:
{
"Authorization":"Bearer token goes here"
}
Note: The whitespace between Bearer and the authentication token is important. Once a user has been created and authenticated, it is possible to fetch data from the API.
Returning Users
If a user has been previously created but one does not have the key, it is possible to get the key using the email and password according to the following method:
mutation {
login(data: {
email:"name@example.com"
password:"password"
}) {
token
}
}
Fetching Data
There are two queries that can be used to fetch data from the API. Both of them require authentication.
View the API Documentation to see more methods.
getCondition (Must be Authenticated)
Make sure the authentication token is included in the HTTP Header tab as shown above. To use this method, run the following query below:
query {
getCondition (data: {
condition: "Blood Pressure",
zip:"21042"
}){
name
zip
address
herbs
products {
name
available
}
}
}
Logging Out
Once finished using the API, the transaction can be concluded through a simple manual logout process. Ensure that there is proper authentication for this by checking the HTTP Headers and identifying the token. Then, to complete the logout, run the following mutation:
mutation {
logout
}
If this successfully fires and logout is completed, it will show a 200 status code. Also, the authentication token being used will no longer function.
All Methods
To see the all the methods that can be run, visit the API Documentation.
Use Cases & Value
Developers can integrate the Food & Nutrition API as an added value element within the wellness apps and portals that are proliferating through the health technology marketplace.
Reduce Logistical Burden
If this API is leveraged within an app, it can provide users with a consolidated information source, listing recommended herbs as well as local Farmers Market outlets where these products are available.
Promote Healthy Lifestyle Habits
In addition to following the course of treatment as prescribed by a physician, positive changes to diet—including the addition of specific herbs—can improve overall well-being.
Increase Visibility of Local Resources
Consumers are more likely to support vendors from within their own community if it is simple and convenient. This API is built to anticipate users’ propensity to use products that are easiest to access by immediately matching with appropriate vendors in the provided location.
Support
Our team at CORMAC’s Innovation Lab values feedback from the larger community of developers. The goal is to use our experience to improve your experience. If you have any comments or questions regarding use of this tool, please contact us at [email address].