FLAT
CouperSevenval TechnologiesDocker ImageGithub
develop
develop
  • Changelog
  • FLAT
  • Administration
    • Configuration
    • Docker
    • Logging
  • Cookbook
    • Using the Built-in Mocking
    • Performing Additional Checks on JWT Access Tokens
    • Logging Custom Fields
    • Using Environment Variables
    • Handling Errors with an Error Flow
    • File Serving
    • Forwarding a Request to an Upstream API
    • Extracting Common Initialization Flow Tasks
    • Encoding and Decoding JWT
    • Passing Header Fields to the Client
    • How can I pass an arbitrary header field to an upstream system?
    • Performing Additional Checks on JWT Access Tokens
    • Proxying requests to Upstream APIs
    • Increasing the Request Timeout
    • How can I see what the client requested?
    • Using Swagger UI for API Documentation
    • Testing API Requests
    • Testing with Backend Requests
    • Testing Templates
    • Sending POST Requests
    • Processing Upstream Responses
    • Protecting Access using JWT Tokens
  • Reference
    • Configuration
    • Debugging
    • flat CLI
    • Flow
    • Variables
    • OpenAPI / Swagger Integration
    • OpenAPI
      • CORS - Cross-Origin Resource Sharing
    • OpenAPI
      • Differences from Swagger
    • OpenAPI
      • Mocking
    • OpenAPI
      • Routing
    • OpenAPI
      • Security
    • OpenAPI
      • Upstream APIs
    • OpenAPI
      • Validation
    • Flow Actions
      • assert Action
      • auth Action
      • backend-flow Action
      • copy Action
      • debug Action
      • dump Action
      • echo Action
      • error Action
      • eval Action
      • log Action
      • nameshave Action
      • pass-body Action
      • proxy-request Action
      • regex Action
      • request Action
      • requests Action
      • serve Action
      • set-config Action
      • set-env Action
      • set-response-headers Action
      • set-status Action
      • sub-flow Action
      • template Action
      • test-request Action
      • xslt Action
    • Functions
      • apply-codecs()
      • array-reverse()
      • array()
      • base64-decode()
      • base64-encode()
      • body()
      • calc-signature()
      • capitalize-first()
      • content()
      • decrypt-xml()
      • decrypt()
      • encrypt()
      • ends-with()
      • file-exists()
      • fit-document()
      • fit-log()
      • fit-serialize()
      • get-log()
      • has-class()
      • html-parse()
      • join()
      • json-doc()
      • json-parse()
      • json-stringify()
      • json-to-csv()
      • json-to-xml()
      • jwt-decode()
      • jwt-encode()
      • ldap-lookup()
      • ldap-query()
      • lookup()
      • matches()
      • md5()
      • replace()
      • sort()
      • split()
      • tolower()
      • toupper()
      • trim()
      • unixtime()
      • urldecode(), url-decode()
      • urlencode(), url-encode()
      • uuid3() and uuid4()
      • verify-signature()
      • verify-xmldsig()
      • xml-parse()
      • xml-to-json()
    • Templating
      • {{,}}
      • Comment {{// …}}
      • Dot {{.}}
      • Conditional `{{if <condition>}} … {{elseif <condition> }} … {{else}} … {{end}}
      • loop
      • ?? Operator
      • Object XML Notation (OXN)
      • Pair Producer {{: …}}
      • Placeholder
      • Template Variables
      • with
    • Testing
  • Tutorial
Powered by GitBook
On this page
  • Defining Env Vars
  • flat cli
  • Docker
  • Accessing Env Vars in Flows
  • Use Cases
  • Upstream URLs
  • JWT
  • Credentials
  • See also

Was this helpful?

  1. Cookbook

Using Environment Variables

PreviousLogging Custom FieldsNextHandling Errors with an Error Flow

Last updated 5 years ago

Was this helpful?

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

In a development setup we may use 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:

$ export FLAT_MY_VAR="my value"
$ flat start

(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 ).

The env vars can be inspected with the env :

$ curl -s -H debug:env localhost:8080/ > /dev/null
debug   [env]                  Env vars: {
    "HOSTNAME": "cee6f96cd5ac",
    "FLAT_DEBUG": ":error:log",
    "PATH": "…",
    "PWD": "…",
    "FLAT_MY_VAR": "my value",
    "SHLVL": 1,
    "HOME": "/home/flat"
}

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:

$ curl -s -H debug:env::inline localhost:8080

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:

version: '3'
services:
  flat:
    image: sevenvaltechnologies/flatrunner
    volumes:
      - .:/app
    ports:
      - 8080:8080
    environment:
      - MY_VAR=my value

We have defined MY_VAR in the environment section. Start the container with

$ docker-compose up

and run the curl command again:

$ curl -s -H debug:env::inline localhost:8080

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

$ docker-compose logs -f

Between a bunch of access logs, you will see the current environment dump:

flat_1  | … debug   [env]                  Env vars: {
flat_1  |     "HOSTNAME": "1a315a77a2ed",
flat_1  |     "MY_VAR": "my value",
flat_1  |     "PATH": "…",
flat_1  |     "PWD": "…",
flat_1  |     "SHLVL": 1,
flat_1  |     "HOME": "/home/flat"
flat_1  | }

Another way of defining env vars would be the -e flag of a simple docker run:

$ docker run --rm -it -e MY_VAR="my value" -v "$(pwd):/app" sevenvaltechnologies/flatrunner

Great! We now know how to define environment variables. Let's use them!

Accessing Env Vars in Flows

{
  "MY_VAR": "my value",
  …
}
<flow>
  <template>
  {
    "my var": {{ $env/MY_VAR }}
  }
  </template>
</flow>

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:

AUTH_SERVICE=http://localhost:3000
<flow>
  <request>
  {
    "url": {{ concat($env/AUTH_SERVICE, "/v1/logout" )}},
    "headers": {
      {{: $request/headers/authorization }}
    }
  }
  </request>
</flow>

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

Credentials

<flow>
  <request>
  {
    "url": "…",
    "options": {
      "basic-auth-credentials": {{ $env/AUTH_SERVICE_CREDS }}
    }
  }
  </request>
</flow>

Or if you need to include a token in your requests:

<flow>
  <request>
  {
    "url": "…",
    "headers": {
      "authentication": {{ concat("Bearer ", $env/AUTH_SERVICE_TOKEN) }}
    }
  }
  </request>
</flow>

See also

All environment variables are accessible from by the system . It is an object with the defined environment variables as properties:

We could create a simple to send our env var back to the client:

To the auth service, we have to build a URL:

When working with 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 for a larger example.

Often, access between services is restricted by an .

You could read Basic Auth credentials from an env var in your :

Test Action (reference)

(cookbook)

flat cli
tutorial
debug topic
flows
variable $env
OXN
template
request
JWT
Working with JWT
HTTP Authentication
set-env
Working with JWT
request