Using Environment Variables
It is good practice to make a FLAT app configurable so that it can run in multiple environments. For example, the upstream APIs have different locations in a local dev setup and the production system.
FLAT runs as a Docker Container and can make use of the environment variables defined in Dockerfile
, docker-compose.yml
or with docker run -e
.
Defining Env Vars
flat
cli
flat
cliIn a development setup we may use flat
cli to run our API. By convention, all shell environment variables starting with FLAT_
are passed into the container.
Let's start FLAT with an env variable:
(We assume, that you have a swagger.yaml
and some API path that you can call. If you're not so far, yet, checkout the tutorial).
The env vars can be inspected with the env
debug topic:
There's our FLAT_MY_VAR
! (The FLAT_DEBUG
var is set by flat
cli to allow this kind of debugging).
Note that we have discarded the actual HTTP response with > /dev/null
. The default debug sink in flat
is stderr
which is printed on the starting shell. If you have no idea where this terminal window is, you can also include the debug output in the HTTP response:
The inline
sink interweaves the debug output with your API response. It will disturb your response, but it is sometimes handy for debugging…
Docker
If you control the Docker setup yourself (e.g. docker-compose
, Kubernetes, …) you can use its built-in support for environment variables. In this case, you are not limited to variables with names starting with FLAT_
.
Your docker-compose.yaml
could look like this:
We have defined MY_VAR
in the environment
section. Start the container with
and run the curl
command again:
This time the debug output will appear in the docker-compose
output. If your container is running in the background, you may start another log tail with
Between a bunch of access logs, you will see the current environment dump:
Another way of defining env vars would be the -e
flag of a simple docker run
:
Great! We now know how to define environment variables. Let's use them!
Accessing Env Vars in Flows
All environment variables are accessible from flows by the system variable $env
. It is an OXN object with the defined environment variables as properties:
We could create a simple template to send our env var back to the client:
Use Cases
Upstream URLs
A typical use-case for an env var is the base URL of an upstream API.
Let's define the origin of our auth service:
To request the auth service, we have to build a URL:
If the app is deployed into production, the AUTH_SERVICE
variable will be set to another value, probably one with https
and running on a different port.
JWT
When working with JWT a signing key (or two, in case of RS*
algos) need to be configured in FLAT. It is good idea to pass these as environment variables.
See Working with JWT for a larger example.
Credentials
Often, access between services is restricted by an HTTP Authentication.
You could read Basic Auth credentials from an env var in your request:
Or if you need to include a token in your requests:
See also
Test Action
set-env
(reference)Working with JWT (cookbook)
Last updated