When developing FastAPI applications, being able to flexibly control different parts of your app using environment variables enables you to modify your application behavior without changing your code. You can store anything from feature toggles and service URLs to database credentials as environment variables.
In this example, we'll demonstrate how to leverage environment variables to control feature toggles.
We've created an application that has a simple messaging feature under development. The availability of this new feature is controlled by an environment variable NEW_MESSAGING_ENABLED
.
# main.py
import os
from fastapi import FastAPI, HTTPException
app = FastAPI()
NEW_MESSAGING_ENABLED = os.getenv("NEW_MESSAGING_ENABLED", "false").lower() == "true"
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/new_message")
def new_message():
if not NEW_MESSAGING_ENABLED:
raise HTTPException(status_code=404, detail="Feature not found")
return {"status": "New messaging feature is running!"}
# feature.env
NEW_MESSAGING_ENABLED=false
Let's deploy this application with the new messaging feature disabled.
fastapi-serve deploy jcloud main:app --env feature.env
โญโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ App ID โ fastapi-c18fb24e03 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Phase โ Serving โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Endpoint โ https://fastapi-c18fb24e03.wolf.jina.ai โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ App logs โ https://cloud.jina.ai/ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Base credits (per hour) โ 10.104 (Read about pricing here) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Swagger UI โ https://fastapi-c18fb24e03.wolf.jina.ai/docs โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ OpenAPI JSON โ https://fastapi-c18fb24e03.wolf.jina.ai/openapi.json โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Let's test the application by sending a request to the /new_message
endpoint.
curl -sX GET "https://fastapi-c18fb24e03.jina.ai/new_message" | jq
{
"detail": "Feature not found"
}
Let's enable the new messaging feature by updating the environment variable and the app.
# feature.env
NEW_MESSAGING_ENABLED=true
fastapi-serve update jcloud main:app --env feature.env --app-id fastapi-c18fb24e03
Once the update is complete, let's test the application again.
curl -X GET "https://fastapi-c18fb24e03.jina.ai/new_message"
{
"status": "New messaging feature is running!"
}
Note: It might take some time for the update to be reflected in the application.
The example demonstrates how effectively fastapi-serve
supports using environment variables to manage FastAPI applications. With a simple environment file, we altered the app's behavior, revealing the potential to control almost any feature of your application. This highlights how fastapi-serve
is a powerful and convenient tool for deploying and managing FastAPI apps.