Validating Environment Variables in a Python Web App using FastAPI

Posted by


In any web application, it is essential to properly validate the data that is being passed as environment variables. Environment variables are variables that are stored on the server and are accessible to the application during runtime. It is important to ensure that these variables are correctly formatted and contain the necessary information to prevent any potential security risks or errors in the application.

In this tutorial, we will be using FastAPI, a modern web framework for building APIs with Python, to demonstrate how to validate environment variables in a Python web app. We will be using the pydantic library, which is integrated with FastAPI and provides a simple way to define data types and enforce validation rules.

Step 1: Install FastAPI and pydantic

Before we begin, make sure that you have FastAPI and pydantic installed. You can install them using pip by running the following commands:

pip install fastapi
pip install pydantic

Step 2: Create a FastAPI app

Let’s create a simple FastAPI app to demonstrate how to validate environment variables. Create a new Python file (e.g., main.py) and import the necessary modules:

from fastapi import FastAPI
from pydantic import BaseSettings

Next, create a FastAPI app instance:

app = FastAPI()

Step 3: Define a Pydantic model for environment variables

In FastAPI, you can use Pydantic models to define the structure of incoming data and validate it. Create a new Pydantic model to define the structure of your environment variables:

class EnvironmentSettings(BaseSettings):
    environment: str
    port: int

In this example, we are defining two environment variables: "environment" as a string and "port" as an integer. You can add more variables as needed.

Step 4: Validate environment variables

Now that we have our Pydantic model defined, we can use it to validate our environment variables. Create an instance of the EnvironmentSettings class and pass the environment variables as arguments:

env_settings = EnvironmentSettings()

When you create an instance of the EnvironmentSettings class, Pydantic will automatically validate the environment variables based on the data types and validation rules defined in the model. If any validation errors occur, Pydantic will raise an exception with detailed error messages.

Step 5: Access validated environment variables

Once the environment variables have been validated successfully, you can access them through the instance of the EnvironmentSettings class:

print(env_settings.environment)
print(env_settings.port)

You can now use the validated environment variables in your FastAPI app to configure the app’s settings based on the values provided.

Step 6: Running the FastAPI app

To run your FastAPI app, add the following code at the end of your Python file:

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

You can start your FastAPI app by running the following command in your terminal:

uvicorn main:app --reload

Now you have a simple FastAPI app that validates environment variables using Pydantic. This ensures that your app is robust and secure, as it prevents any potential errors caused by invalid data being passed as environment variables. Remember to always validate your environment variables to ensure the stability and security of your web application.

0 0 votes
Article Rating

Leave a Reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@joelstevick1533
4 hours ago

Nice video!

@lucaspraciano4640
4 hours ago

You must learn about the Dynaconf library

2
0
Would love your thoughts, please comment.x
()
x