Skip to main content

Overview

CoroNet uses environment variables to manage sensitive configuration data and customize application behavior. All environment variables should be stored in a .env file in the project root directory.
Never commit your .env file to version control. It contains sensitive information like API keys. The .gitignore file is already configured to exclude .env files.

Required Environment Variables

OPENAI_API_KEY

The OpenAI API key is required for the primary OCR functionality using GPT-4o-mini vision model.
.env
OPENAI_API_KEY=sk-proj-...
The application will fail to start if this variable is not set. See the OpenAI Setup guide for instructions on obtaining an API key.
Used in: app.py:15
app.py
from dotenv import load_dotenv
import os
from openai import OpenAI

load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

Optional Environment Variables

Flask Configuration

While not currently exposed as environment variables in the code, you can add these common Flask settings:
.env
# Flask application settings
FLASK_ENV=development
FLASK_DEBUG=True
FLASK_PORT=5000
FLASK_HOST=127.0.0.1

# Flask secret key (recommended for production)
SECRET_KEY=your-secure-random-secret-key-here
The current implementation uses a hardcoded secret key (app.py:12). For production deployments, change this to use an environment variable:
app.secret_key = os.getenv("SECRET_KEY", "superclave")

File Storage Paths

The application automatically creates these directories if they don’t exist:
  • DATA_PATH: data/registros.csv - CSV file storing license plate records
  • UPLOADS_DIR: uploads/ - Directory for uploaded vehicle images
Configuration in code: app.py:17-22
app.py
BASE_DIR = os.path.dirname(__file__)
DATA_PATH = os.path.join(BASE_DIR, "data", "registros.csv")
UPLOADS_DIR = os.path.join(BASE_DIR, "uploads")

os.makedirs(os.path.dirname(DATA_PATH), exist_ok=True)
os.makedirs(UPLOADS_DIR, exist_ok=True)

Creating the .env File

1

Create the file

In your project root directory (where app.py is located), create a new file named .env:
touch .env
2

Add your API key

Open the .env file in a text editor and add your OpenAI API key:
.env
OPENAI_API_KEY=sk-proj-your-actual-key-here
3

Verify .gitignore

Confirm that .env is listed in your .gitignore file to prevent accidental commits:
cat .gitignore | grep .env
You should see *.env in the output.
4

Load environment variables

The application automatically loads environment variables using python-dotenv:
from dotenv import load_dotenv
load_dotenv()  # Called at app.py:14

Environment Variable Loading

CoroNet uses the python-dotenv package to load environment variables from the .env file:
from dotenv import load_dotenv
import os

load_dotenv()  # Loads variables from .env file
api_key = os.getenv("OPENAI_API_KEY")  # Access the variable
The load_dotenv() function:
  • Searches for a .env file in the current directory and parent directories
  • Loads all variables into the environment
  • Does not override existing environment variables
  • Silently fails if no .env file is found

Example .env File

Here’s a complete example .env file for CoroNet:
.env
# OpenAI Configuration (Required)
OPENAI_API_KEY=sk-proj-AbCdEfGhIjKlMnOpQrStUvWxYz1234567890

# Flask Configuration (Optional)
FLASK_ENV=development
FLASK_DEBUG=True
FLASK_PORT=5000
SECRET_KEY=change-this-to-a-random-secure-key-in-production

# Application Settings (Optional)
MAX_UPLOAD_SIZE=16777216  # 16MB in bytes
ALLOWED_EXTENSIONS=jpg,jpeg,png,gif

Verifying Configuration

To verify your environment variables are loaded correctly:
import os
from dotenv import load_dotenv

load_dotenv()

if os.getenv("OPENAI_API_KEY"):
    print("✓ OPENAI_API_KEY is set")
    print(f"  Key starts with: {os.getenv('OPENAI_API_KEY')[:10]}...")
else:
    print("✗ OPENAI_API_KEY is not set")

Security Best Practices

Follow these security guidelines to protect your API keys and sensitive data:
  1. Never commit .env files - Always keep them in .gitignore
  2. Use strong secret keys - Generate random keys for SECRET_KEY
  3. Rotate API keys regularly - Update your OpenAI API key periodically
  4. Limit API key permissions - Use keys with minimal required permissions
  5. Use different keys per environment - Separate keys for development, staging, and production
  6. Monitor API usage - Check your OpenAI dashboard for unexpected usage

Troubleshooting

Application won’t start

If you see an error related to the OpenAI client, ensure your .env file:
  • Exists in the project root directory (same location as app.py)
  • Contains a valid OPENAI_API_KEY variable
  • Has no extra spaces around the = sign
  • Has no quotes around the API key value

Environment variables not loading

If variables aren’t being read:
  1. Verify python-dotenv is installed: pip list | grep dotenv
  2. Check the .env file location (should be in project root)
  3. Ensure load_dotenv() is called before accessing variables
  4. Try specifying the path explicitly: load_dotenv('.env')

Next Steps

OpenAI Setup

Get your OpenAI API key and configure the model

Tesseract Setup

Install Tesseract OCR for fallback text extraction

Build docs developers (and LLMs) love