FastAPI Questions & Answers

 

Basic FastAPI Questions

1. What is FastAPI?

FastAPI is a modern Python framework for building APIs with:

  • High performance
  • Automatic validation
  • Async support

2. Why is FastAPI fast?

Because it uses:

  • ASGI (Asynchronous Server Gateway Interface)
  • Async/await
  • Starlette & Pydantic

3. What is ASGI?

ASGI is a standard for async Python web apps, allowing:

  • Concurrent requests
  • WebSockets
  • High performance

4. FastAPI vs Flask?

FastAPI:

  • Async support
  • Auto docs
  • Better performance

Flask:

  • Simpler
  • Sync-based

5. FastAPI vs Django REST Framework?

FastAPI → Performance & microservices
DRF → Full-featured applications

Intermediate Questions

6. What are Path Parameters?

Dynamic values in URL:

@app.get("/items/{id}")

7. What are Query Parameters?

Optional parameters:

/items?limit=10

8. What is Pydantic?

Library for:

  • Data validation
  • Type enforcement

9. What is Dependency Injection?

Reusing logic using Depends()

10. What is Depends()?

A function that injects dependencies into routes

11. What is Response Model?

Defines output structure of API

12. What is UploadFile?

Efficient file upload handling

13. What is BackgroundTasks?

Runs tasks after response

14. What is Middleware?

Runs before & after request

15. What is CORS?

Allows cross-origin requests

Authentication & Security

16. What is JWT?

JSON Web Token used for authentication

17. How does JWT work?

Login → Token → Verify → Access

18. Why hash passwords?

For security

19. What is OAuth2?

Authentication framework

20. What is Bearer Token?

Token sent in header:

Authorization: Bearer <token>

Database Questions

21. How to connect DB in FastAPI?

Using SQLAlchemy

22. What is ORM?

Object Relational Mapping

23. What is orm_mode?

Converts ORM objects → JSON

24. How to manage DB sessions?

Using dependency injection

25. What is migration?

Managing DB changes (Alembic)


Advanced Questions

26. What is async in FastAPI?

Non-blocking execution

27. Difference between sync & async?

Sync → blocking
Async → non-blocking

28. When to use async?

I/O operations

29. What is Uvicorn?

ASGI server for FastAPI

30. What is Gunicorn?

Production server with workers

31. What is Docker?

Containerization tool

32. How to deploy FastAPI?

Docker, AWS, Nginx

33. What is caching?

Store responses for faster access

34. What is Redis?

In-memory cache

35. What is rate limiting?

Limiting API requests

Testing & Debugging

36. How to test FastAPI?

Using pytest & TestClient

37. What is TestClient?

Tool to test APIs

38. What is mocking?

Simulating dependencies

39. How to structure FastAPI project?

Use layered architecture

40. Why use environment variables?

Secure secrets

41. How to handle errors?

Use HTTPException

42. How to improve performance?

Async + caching + workers


43. Create a simple FastAPI API

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
return {"message": "Hello World"}

44. Create POST API with validation

from pydantic import BaseModel

class Item(BaseModel):
name: str

@app.post("/items/")
def create(item: Item):
return item

45. Protect a route

@app.get("/protected")
def protected(user=Depends(get_current_user)):
return user


Comments

Popular posts from this blog

Database Integration in FastAPI (SQLAlchemy CRUD)

Middleware & CORS in FastAPI

Python Data Handling