Getting an Access Token
This is the first step to interact with the dFDA API. Learn how to create a user and get an access token.
You can check the API documentation to generate code to do this in any language.
However, here is a simple example of how you can create a user if you’re using Node.js and Prisma.
Ensure you have your client ID from https://builder.dfda.earth and access tokens set properly in your environment variables.
To securely manage your FDAI client credentials (DFDA_CLIENT_ID and DFDA_CLIENT_SECRET), it’s recommended to use environment variables. This approach helps in keeping sensitive information out of your source code. Here’s how you can set up and use a .env file for this purpose:
- 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:
Replace yourClientIdHere and yourClientSecretHere with your actual FDAI client ID and secret.
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:
Then, at the very top of your main application file (or at least before you use any environment variables), require and configure dotenv:
- 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.