Reading Configuration Variables For Python APIs
By: Mike Wolfe
Tags: Json, Programming, API, Software Development, Python

Photo by Ferenc Almasi on Unsplash

Lately, I have been working on a logging API that logs different levels of events for my personal applications. Thinking about future plans for this API, the goal is to create CI/CD pipelines within Jenkins.

Within those pipelines, it would be really nice to change the configuration variables of any application based on the environment they are being deployed to. These variables will have the job of storing connection strings, URLs, file paths, etc.

However, before all that can happen, I need to figure out how I want these variables to be stored.

Option 1: The Old Fashioned Way

One possible way of storing configuration variables is as an environment variable on your computer/server. If you are a Linux user, you can create a variable by using this command:

export CONNECTIONSTRING="mysql://<USER>:<PASSWORD>@<HOST>/Logging"

To see what value is assigned to the variable, run this command in your terminal:

echo $CONNECTIONSTRING

You should get results similar to this:

Now that the environment variable is created, we can go over to our Python code and import the “os” module. This module is installed by default, so there will be no need to use pip.

import os

To use the value of that variable in a Python script, utilize the “getenv” function from the “os” module and assign it to a variable.

testVariable = os.getenv(["CONNECTIONSTRING"])

If you print out the contents of the Python variable and run your script, the output should like something similar to this:

Option 2: JSON File

Another interesting way to handle configuration variables is by using a JSON file. This method will require a little more code but will remove the reliance of needing to setup up the variable on your computer/server. Here is an example of a very simple JSON configuration file:

So how do we get this to fit in with Python? The easiest place to start is by creating a separate file that loads in the settings file.

Essentially, this bit of code opens the settings file and then parses out the contents to a variable using the “load” method from the “json” module. To actually use this within your API, import this Python file with:

from load_settings import data

Once it’s imported, we are free to read our settings. The “data” variable that we imported, is actually in the form of a dictionary. Therefore, if we create a variable, and assign the configuration variable to it, it will look something like this:

testVariable = data["CONNECTIONSTRING"]

If we print out this new variable and run your script, the output would be similar to this image:

Conclusion

To sum things up, we discussed two very good options to store configuration variables for your APIs. The first option is pretty easy to set up, however, the major downside is that variables live on your computer/server. In option two, the variables are stored in a file.

For me, I will probably go with option two for my APIs. This will allow me to make changes to the variables on the fly and not have to worry about them being set up on the production server when deployed.

Also, since I plan on containerizing my APIs, the configuration variables will get wrapped up in the container. Feel free to leave a comment about how you store configuration variables for APIs. Until next time, cheers!