Data API Builder Supports ENV Files
One of the reasons developers like you love Data API Builder is because it’s packed with developer-friendly features. Each feature makes your development journey easier. In earlier versions, we integrated Swagger for easy REST endpoint testing and Banana Cake Pop for simple GraphQL endpoint testing. In this release, we’ve added ENV file support, enabling you to easily set process-scoped environment variables while developing your API. Node.js developers are accustomed to ENV files, as are Python developers. Data API Builder now accommodates your existing workflow. And, since DAB is language-agnostic, ENV file support works for GO, .NET developers, and everyone else.
Configuration Primer
Data API Builder uses a JSON configuration file to direct where your data resides and what objects to expose; you don’t code for DAB. Setting configuration values is as straightforward as embedding them in the configuration file. But who wants your connections or other pivotal settings hardcoded? Nobody. The answer is environment variables. Fortunately, Data API Builder supports these right out of the box with the @env('my-var-name')
syntax.
Upgrade your configuration from this:
{
"connection-string": "My connection string has secrets!"
}
To something modular like this:
{
"connection-string": "@env('my-connection-string')"
}
Environment variables shield secrets from plain text exposure and allow for value swapping in different settings. However, these variables must be set either in the user
or computer
scope, which can lead to cross-project variable “bleeding” if variable names are duplicated. The better alternative? ENV files.
Enter ENV Files
Your Git repository likely already has a .gitignore
file, used to identify files that should not be tracked. Similarly, ENV files have no name—only the .env
extension. Their internal structure is elegantly simple, defining each variable as name=value
.
Sample ENV file
# this is a comment
environment=development
my-connection-string="Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"
Note that
environment
doesn’t use quotes, whilemy-connection-string
does. Why? The latter’s value contains=
equal signs, which are reserved characters.
It really is this simple.
To leverage the above .env
file, simply update your configuration file as previously discussed, wrapping values in @env()
syntax. Then save your values in your .env
file. Easy.
{
"environment": "@env('environment')",
"connection-string": "@env('my-connection-string')"
}
Wherever you employ a scalar value in your Data API Builder configuration file, the @env()
syntax will replace it with the corresponding environment variable. If a .env
file is in the same folder as your configuration file, Data API Builder will automatically add those variables to the process
scope, thus eliminating your risk of “bleeding” values into other projects.
Word of Warning
One crucial detail to remember: always exclude .env
files from Git tracking. This means .env
should always be an entry in your .gitignore
file. This is important to get right before you push to GitHub.
Here’s the .gitignore
syntax:
.env
What’s the takeaway? ENV files are awesome and their syntax is simple. Data API Builder continues to add features aimed at elevating your development experience. Our current focus is on structured logging and increased OpenAPI version 3 support for REST endpoints. The backlog is deep and – since we are open-source – we welcome your contributions, submit a PR
! Today, however, we celebrate the availability of ENV file support, designed to make environment variable management more streamlined and safe.
Best of luck!
Hey thanks for your article. I’m facing an issue and I hope you could help me with it. I’m trying to use the .env file in a docker container where my dab-config.json ist referenced. It looks like this:
how can I use the .env file in that specific use case. Is it possible?
Hello there!
Of course! Here you have one example of how to use the .env file (environment variables) with Docker:
sudo docker run -it
-v ".:/App/samples" -p PORT:PORT \
-e CONN_STRING="Server=;Database=;User ID=;Password=;" mcr.microsoft.com/azure-databases/data-api-builder \
--ConfigFileName ./samples/dab-config.json
Note this example uses the
"-e"
(environment variable) option for thedocker run
command. This option allows you to pass any variable at the container during run time. However, you must ensure that your DAB config file has the same variable. In this example, I’m passing the “CONN_STRING” env variable that the DAB configuration file will read:{
"$schema": "https://github.com/Azure/data-api-builder/releases/download/v{dab-version}/dab.draft.schema.json",
"data-source": {
"database-type": "mssql",
"connection-string": "@env('CONN_STRING')"
},
Therefore, you also need to modify your DAB configuration file. For more information see Setting environment variables
.
Let me know if you have more questions or comments.
Carlos Robles