All interview guides

FastAPI Interview Questions and Answers

24 questions that come up in FastAPI technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What is async database integration?

Advanced

Answer: All of the above

Async database integration uses async SQLAlchemy, databases library, or motor (MongoDB). Enables non-blocking I/O for better concurrency and performance.

2. What is dependency injection in FastAPI?

Intermediate

Answer: All of the above

Dependency injection in FastAPI uses Depends() to declare dependencies. It enables code reuse, shared logic, database connections, authentication, etc.

3. What is FastAPI?

Beginner

Answer: A modern, fast web framework for building APIs with Python

FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints.

4. How do you implement testing in FastAPI?

Advanced

Answer: All of the above

Test FastAPI with TestClient: creates client for testing without running server. Use pytest for test framework. Supports async tests with pytest-asyncio.

5. Which Python feature does FastAPI heavily rely on?

Beginner

Answer: Type hints

FastAPI heavily relies on Python type hints for data validation, serialization, and automatic API documentation generation.

6. How do you define a dependency?

Intermediate

Answer: Create a function and use Depends()

Define a dependency as a function, then use it with Depends(): `def get_db(): ...; @app.get("/") def read(db = Depends(get_db)):`. FastAPI calls it automatically.

7. How do you install FastAPI?

Beginner

Answer: pip install fastapi

Install FastAPI using pip: `pip install fastapi`. You also need an ASGI server like uvicorn: `pip install uvicorn[standard]`.

8. What is sub-dependency in FastAPI?

Intermediate

Answer: Both b and c

Sub-dependencies are dependencies that themselves have dependencies. FastAPI resolves the entire dependency tree automatically, creating chains of dependencies.

9. What is dependency override for testing?

Advanced

Answer: All of the above

Override dependencies in tests using app.dependency_overrides dict. Replace real database with test database, mock external APIs, etc.

10. What is the correct way to create a FastAPI instance?

Beginner

Answer: app = FastAPI()

Create a FastAPI instance with `app = FastAPI()`. This creates the main application object that you use to define routes and configurations.

11. How do you handle database connections with dependencies?

Intermediate

Answer: All of the above

Use generator functions with yield for database sessions. Code before yield runs before request, after yield runs after (cleanup). Example: get_db() yields session.

12. What is GraphQL integration with FastAPI?

Advanced

Answer: Both b and c

Integrate GraphQL using Strawberry or Graphene libraries. Add GraphQL endpoint to FastAPI app. Combine REST and GraphQL in same application.

13. What is custom exception handling?

Advanced

Answer: All of the above

Create custom exception handlers with @app.exception_handler(ExceptionClass). Return custom error responses, log errors, send notifications, etc.

14. Which decorator is used to define a GET endpoint?

Beginner

Answer: @app.get()

Use `@app.get("/path")` to define a GET endpoint. FastAPI provides decorators for all HTTP methods: get, post, put, delete, patch, options, head.

15. What is OAuth2PasswordBearer?

Intermediate

Answer: Both b and c

OAuth2PasswordBearer is a security scheme that extracts the bearer token from Authorization header. Used as dependency to protect endpoints.

16. How do you implement JWT authentication?

Intermediate

Answer: All of the above

Implement JWT using python-jose: create tokens with user info and expiry, sign them, return to client. Verify tokens in dependency functions to protect endpoints.

17. How do you run a FastAPI application with uvicorn?

Beginner

Answer: uvicorn main:app --reload

Run FastAPI with uvicorn: `uvicorn main:app --reload` where main is the filename and app is the FastAPI instance. --reload enables auto-reload during development.

18. What is rate limiting in FastAPI?

Advanced

Answer: All of the above

Implement rate limiting using slowapi (Flask-Limiter port) or custom middleware. Limit requests per IP/user per time window. Prevents abuse and DDoS.

19. How do you implement pagination?

Advanced

Answer: All of the above

Implement pagination with skip/limit query parameters. Return paginated data with total count. Create reusable pagination dependency for consistency.

20. What is APIRouter?

Intermediate

Answer: Both b and c

APIRouter creates modular, reusable route groups. Define routes in separate files/modules, then include them in main app with app.include_router().

21. What is automatic API documentation in FastAPI?

Beginner

Answer: Auto-generated interactive docs at /docs and /redoc

FastAPI automatically generates interactive API documentation. Access Swagger UI at /docs and ReDoc at /redoc. Based on OpenAPI standard.

22. What is response streaming?

Advanced

Answer: Both b and c

StreamingResponse sends content in chunks without loading entire response in memory. Useful for large files, video streaming, or generated content.

23. How do you use APIRouter?

Intermediate

Answer: All of the above

Create router with APIRouter(), define routes on it, then include: `app.include_router(router, prefix="/items", tags=["items"])`.

24. How do you define path parameters in FastAPI?

Beginner

Answer: Using curly braces in path

Define path parameters using curly braces: `@app.get("/items/{item_id}")`. The parameter is automatically passed to the function and validated.

Ready to test yourself?

The full FastAPI bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the FastAPI quiz