0
0
FastAPIframework~30 mins

Health check endpoints in FastAPI - Mini Project: Build & Apply

Choose your learning style9 modes available
Health check endpoints
📖 Scenario: You are building a simple web service using FastAPI. To make sure your service is running well, you want to add health check endpoints. These endpoints will let other systems ask if your service is alive and ready to handle requests.
🎯 Goal: Create a FastAPI app with two health check endpoints: /health/alive and /health/ready. The /health/alive endpoint will confirm the app is running. The /health/ready endpoint will confirm the app is ready to serve requests.
📋 What You'll Learn
Create a FastAPI app instance named app
Add a GET endpoint at /health/alive that returns JSON {"status": "alive"}
Add a GET endpoint at /health/ready that returns JSON {"status": "ready"}
Use proper FastAPI route decorators and response models
💡 Why This Matters
🌍 Real World
Health check endpoints are used by monitoring tools and load balancers to verify if a web service is running and ready to accept traffic.
💼 Career
Knowing how to create health check endpoints is essential for backend developers and DevOps engineers to ensure reliable and maintainable web services.
Progress0 / 4 steps
1
Create FastAPI app instance
Import FastAPI from fastapi and create an app instance called app.
FastAPI
Need a hint?

Use FastAPI() to create the app instance and assign it to app.

2
Add /health/alive endpoint
Add a GET endpoint at /health/alive using @app.get decorator. The endpoint function should be named health_alive and return {"status": "alive"}.
FastAPI
Need a hint?

Use @app.get("/health/alive") to create the route and return the JSON dictionary.

3
Add /health/ready endpoint
Add a GET endpoint at /health/ready using @app.get decorator. The endpoint function should be named health_ready and return {"status": "ready"}.
FastAPI
Need a hint?

Use @app.get("/health/ready") and return the JSON dictionary with status ready.

4
Complete FastAPI app with both endpoints
Ensure the FastAPI app has both /health/alive and /health/ready GET endpoints defined with correct function names and return values.
FastAPI
Need a hint?

Review your code to confirm both endpoints are present and correct.