FRED© API Without Secrets
Download this Project @ GitHub
Posted on August 4, 2020
The Federal Reserve Economic Data [FRED®] API is a web service that gives access to over 500,000 financial and economic data series from more than 85 public and proprietary sources , most of which are US government agencies. It is a treasure trove of critical data for economic analysis.
A fundamental key to the API is understanding its date nomenclature. “Observation” dates describe the time period for the data. “Realtime” dates are “as-of” dates, the conceived truth of the data as of a reported date. The default for “observation” dates is the earliest information available, up to the most recent release. The default for “realtime” dates is today, leaving that null always brings back the latest truth.
Data series are continually added. The website updates for new series and is searchable at:
Consuming an API
Application Progamming Intrerface (API) is a program that sets on a server and takes defined orders to perform any number of actions. It accepts parameters and returns data in most cases. A user account is required to complete a request from the FRED® API. Make a simple request, no authorization or bearer tokens, to an endpoint of the API passing in a key generated from:
The code formats the variables into url parameters that are passed to the API and used to filter the data in their backend. The "realtime" dates are intentionally missing in the URL to default to the latest reported date. The endpoint responds with the data formatted in JSON
System.Text.Json is an amazing library that can convert JSON to objects or vice versa. This set of JSON APIs is optimized for performance by using Span<T> and it can process UTF-8. The method serializes the returned JSON data and formats it to a C# object. Copy JSON data as a string and paste into a C# class genrator:
This defines and mataches the data structure of the returned JSON to be serialized. Here is a link with more information on other methods and techniques:
Link to the Wonderful World of MSDN Docs
Securing Secrets
API Keys are tied to user accounts and should be kept as securely as possible. Working in larger groups or committing code publicly to GitHub can be dangerous. Initialize secrets in a terminal from the root folder of the project:
dotnet user-secrets init
Add the NuGet package Microsoft.Extensions.Configuration.UserSecrets to your project. A JSON file is generated to store the secrets. Set this file for use with the project. The JSON file is generated here for Mac by default:
Windows stores the file here:
%APPDATA%\microsoft\UserSecrets\\secrets.json
Save the API key for FRED by running the following command in terminal:
dotnet user-secrets set "APIKeys:FRED" "your api key"
Or edit the file manually:
Create a class to match up with the structure of the secrets mapping in the secrets.json file. This will be used to build the secrets into an object for use in the project. Initialize a new instance of the host with the secrets through the Configuration Builder. The environment variable can be stored on the server itself. This would allow developers to work with the keys for the Development server while the Production keys are only stored on the server itself.
IIS
Internet Information Services is a Mircosoft web server software package used for hosting web applications. A GUI is used to manage websites and application pools. Open the IIS Manager tool and open the Configuration Editor:
Select “system.webServer/aspNetCore” on the first dropdown and select ApplicationHost.config. This will store the secret on the server itself. Otherwise the web.config resets each time there is a publish and the variables will need to be reset.
Then select “environmentVariables”, click on the “…” and use the add/remove buttons to configure as you want.
Click apply to finish and you are done.
Happy coding and stay safe out there!