What is a health check endpoint in a web application?
A health check endpoint is a simple URL in a web app that returns a status showing if the app is running well. It helps monitoring tools know if the app is healthy.
Click to reveal answer
beginner
How do you create a basic health check endpoint in FastAPI?
You create a route with @app.get('/health') that returns a simple JSON like {'status': 'ok'}. This shows the app is running.
Click to reveal answer
intermediate
Why should health check endpoints be lightweight and fast?
Because they are called often by monitoring systems. If they take too long or use many resources, they can slow down the app or give wrong health info.
Click to reveal answer
beginner
What HTTP status code is commonly returned by a healthy health check endpoint?
HTTP 200 OK is returned to show the app is healthy and working as expected.
Click to reveal answer
intermediate
How can you extend a health check endpoint to check database connectivity in FastAPI?
Inside the endpoint function, try a simple database query or connection test. If it works, return status ok; if it fails, return an error status and message.
Click to reveal answer
What is the main purpose of a health check endpoint?
ATo serve the main webpage
BTo handle user login
CTo update the database
DTo show if the app is running properly
✗ Incorrect
Health check endpoints are designed to report the app's running status to monitoring tools.
Which HTTP method is usually used for health check endpoints in FastAPI?
APUT
BPOST
CGET
DDELETE
✗ Incorrect
GET is used because health checks only retrieve status information without changing data.
What should a simple health check endpoint return to indicate success?
AHTTP 404 not found
BHTTP 200 OK with status 'ok'
CHTTP 500 error
DHTTP 403 forbidden
✗ Incorrect
HTTP 200 OK with a status message like 'ok' signals the app is healthy.
Why avoid heavy operations in health check endpoints?
AThey slow down the app and can cause false health reports
BThey improve app security
CThey help with user authentication
DThey increase database size
✗ Incorrect
Heavy operations can delay responses and mislead monitoring systems about app health.
How can you test database connectivity in a FastAPI health check?
ARun a simple query and catch errors
BRestart the database server
CDelete all database records
DIgnore database status
✗ Incorrect
Running a simple query helps confirm the database is reachable and working.
Explain how to create a basic health check endpoint in FastAPI and why it is useful.
Think about a simple URL that tells if your app is alive.
You got /4 concepts.
Describe how you would extend a health check endpoint to verify database connectivity.
Imagine checking if your app can talk to its database before saying it is healthy.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a health check endpoint in a FastAPI application?
easy
A. To serve static files like images
B. To handle user authentication
C. To confirm the app is running and responsive
D. To perform database migrations
Solution
Step 1: Understand the role of health check endpoints
Health check endpoints are simple URLs that tell if the app is alive and working properly.
Step 2: Identify the correct purpose
They do not handle authentication, serve files, or migrate databases but confirm app responsiveness.
Final Answer:
To confirm the app is running and responsive -> Option C
Quick Check:
Health check = Confirm app alive [OK]
Hint: Health check endpoints confirm app is alive and responsive [OK]
Common Mistakes:
Confusing health check with authentication endpoints
Thinking health check serves files
Assuming health check runs database tasks
2. Which of the following is the correct way to define a simple health check endpoint in FastAPI?
easy
A. @app.get('/health')
def health():
return {'status': 'ok'}
B. app.route('/health', method='GET')(def health(): return 'ok')
C. app.get('/health')(def health(): return {'status': 'ok'})
D. def health():
return {'status': 'ok'}
app.get('/health', health)
Solution
Step 1: Recall FastAPI syntax for endpoints
FastAPI uses decorators like @app.get('/path') above the function to define GET endpoints.
Step 2: Check each option's syntax
@app.get('/health')
def health():
return {'status': 'ok'} correctly uses @app.get decorator and defines a function returning JSON. Others have syntax errors or wrong decorator usage.
Final Answer:
@app.get('/health')\ndef health():\n return {'status': 'ok'} -> Option A
Quick Check:
Use @app.get decorator for GET endpoints [OK]
Hint: Use @app.get decorator above function for GET endpoints [OK]
Common Mistakes:
Using app.get() as a function call instead of decorator
Wrong decorator syntax or missing @
Returning string instead of JSON/dict
3. Given this FastAPI health check endpoint code, what will be the JSON response when accessed?
The return line uses 'return status: 'ok'' which is invalid Python syntax; it should be a dictionary like {'status': 'ok'}.
Step 2: Check other parts for correctness
The decorator @app.get is correct, function name can be anything, and app instance is created properly.
Final Answer:
Syntax error in return statement -> Option B
Quick Check:
Return dict with braces, not 'key: value' alone [OK]
Hint: Return a dict with braces, not 'key: value' alone [OK]
Common Mistakes:
Writing return without braces for dict
Using wrong decorator for GET endpoint
Assuming function name is fixed
5. You want to create a health check endpoint in FastAPI that returns HTTP 200 with JSON {'status': 'ok'} if the app is healthy, but HTTP 503 with {'status': 'unhealthy'} if a database connection fails. Which approach correctly implements this?
hard
A. Use try-except inside the endpoint to check DB, return JSON with status code accordingly
B. Always return {'status': 'ok'} and rely on external monitoring for DB status
C. Create two endpoints: one for app status and one for DB status, no status codes
D. Raise an exception if DB fails, FastAPI returns 500 automatically
Solution
Step 1: Understand the requirement for conditional response
The endpoint must check DB health and respond with different JSON and HTTP status codes based on that.
Step 2: Evaluate options for handling DB check and response
Use try-except inside the endpoint to check DB, return JSON with status code accordingly uses try-except to check DB and returns JSON with correct status codes, matching the requirement. Others either ignore DB status or do not set proper codes.
Final Answer:
Use try-except inside the endpoint to check DB, return JSON with status code accordingly -> Option A
Quick Check:
Use try-except and return JSON with status codes [OK]
Hint: Use try-except and return JSON with proper HTTP status codes [OK]
Common Mistakes:
Ignoring database health in the endpoint
Not setting HTTP status codes explicitly
Relying on default 500 instead of 503 for service unavailable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.