Ensure you have your client ID from https://builder.dfda.earth and access tokens set properly in your environment variables.
- Create a .env File: In the root of your Node.js project, create a file named .env. Inside this file, you can define your FDAI client credentials like so:
Make sure to never expose your client secret in your client side code. This is strictly backend stuff.
- Load Environment Variables: To load the variables from your .env file, you’ll need a package like dotenv. If you haven’t already installed dotenv, you can do so by running:
- Access Environment Variables: Now, you can access DFDA_CLIENT_ID and DFDA_CLIENT_SECRET anywhere in your application using process.env.DFDA_CLIENT_ID and process.env.DFDA_CLIENT_SECRET, respectively. Here’s an updated snippet of your function getDfdaAccessToken incorporating the loading of environment variables using dotenv:
Usage
You can then use the access token to make authenticated requests to the FDAI API like this:Record Measurements
See an example of how to create a measurement using the access token.