# API & REST API Interview Questions with Answers
## A. Basic API & REST API Concepts
1. What is an API?
An API (Application Programming Interface) is a set of rules that allows different
software applications to communicate and interact with each other.
2. What is the difference between an API and a Web API?
An API can be any interface between software components. A Web API is specifically
accessible over the internet using HTTP.
3. What is a REST API?
A REST API is a type of Web API that follows REST (Representational State Transfer)
principles. It allows interaction with resources via HTTP methods like GET, POST,
PUT, DELETE.
4. What does REST stand for?
REST stands for Representational State Transfer.
5. What are the principles of REST architecture?
- Statelessness
- Client-Server architecture
- Uniform interface
- Cacheability
- Layered system
- Code on demand (optional)
6. What is the difference between REST and SOAP?
REST is lightweight and uses HTTP directly. SOAP is a protocol with strict
standards and uses XML.
7. What is the difference between REST and GraphQL?
REST has multiple endpoints with fixed structures. GraphQL has one endpoint and
lets clients request only the data they need.
8. What is the difference between REST API and normal API?
A normal API might not use web protocols. A REST API uses HTTP and adheres to REST
principles.
## B. HTTP Methods and Status Codes
9. What are HTTP methods used in REST?
GET, POST, PUT, PATCH, DELETE
10. Explain the purpose of GET, POST, PUT, PATCH, DELETE.
- GET: Retrieve data
- POST: Create new data
- PUT: Update existing data completely
- PATCH: Partially update data
- DELETE: Remove data
11. What is the difference between PUT and PATCH?
PUT replaces the entire resource. PATCH updates only specific fields.
12. What are common HTTP status codes and their meanings?
- 200 OK: Success
- 201 Created: Resource created
- 204 No Content: Successful but no data returned
- 400 Bad Request: Invalid request
- 401 Unauthorized: Authentication needed
- 403 Forbidden: Not allowed
- 404 Not Found: Resource missing
- 500 Internal Server Error: Server crashed
13. What status code would you return after a successful POST request?
201 Created
## C. Django REST Framework (DRF)
14. What is Django REST Framework?
A toolkit for building Web APIs using Django. It simplifies serialization,
viewsets, authentication, and permissions.
15. How do you create an API in DRF?
Define a model, create a serializer, write views (APIView or ViewSet), and connect
URLs.
16. What are serializers in DRF?
They convert complex data types like querysets into native Python data types (and
vice versa).
17. What’s the difference between Serializer and ModelSerializer?
- Serializer: Manual field declaration.
- ModelSerializer: Auto-generates fields from a model.
18. What is APIView vs ViewSet in DRF?
- APIView: More control, define each method (get, post).
- ViewSet: DRF auto-generates methods and URLs with less code.
19. What are Routers in DRF and why are they useful?
Routers auto-generate URL routes for ViewSets, reducing boilerplate code.
20. How do you create custom endpoints in DRF?
Use `@action` decorator in ViewSets or define custom methods in APIView.
## D. Request/Response Handling
21. What are request and response objects in DRF?
Request: Contains HTTP request data. Response: DRF’s response wrapper that supports
content negotiation.
22. How do you access request data in DRF?
- `request.data`: For POST, PUT, PATCH
- `request.query_params`: For GET query strings
23. How do you return a custom response in DRF?
Use `Response` class: `return Response({'message': 'Success'}, status=200)`
## E. Authentication & Permissions
24. How do you secure a REST API?
Use authentication (Token, JWT, OAuth), permissions, throttling, and HTTPS.
25. What are the different types of authentication in DRF?
- BasicAuthentication
- TokenAuthentication
- SessionAuthentication
- JWTAuthentication (third-party)
26. What is Token Authentication?
Each user gets a unique token. The client includes this token in requests for
secure access.
27. What are permissions in DRF?
Permissions control who can access or modify resources (e.g., IsAuthenticated,
IsAdminUser).
28. How do you restrict API access to certain users?
Use permission classes in views or globally in settings.
29. What is CORS and how do you handle it in APIs?
CORS (Cross-Origin Resource Sharing) is a browser security feature. Use `django-
cors-headers` to allow cross-domain requests.
## F. API Design & Best Practices
30. How should REST endpoints be named?
Use nouns and plural: `/users/`, `/products/`, `/orders/{id}`
31. What is versioning in REST APIs and why is it important?
Versioning ensures backward compatibility. Example: `/api/v1/users/`
32. How do you handle pagination in APIs?
Use DRF’s pagination classes (PageNumber, LimitOffset, Cursor).
33. How do you document APIs?
Use tools like Swagger (drf-yasg), Postman, or DRF’s built-in docs.
34. What are some best practices for designing REST APIs?
- Use proper HTTP status codes
- Keep endpoints consistent
- Validate inputs
- Use versioning
- Provide documentation
## G. Real-World Scenarios & Debugging
35. How do you test APIs?
Use Postman, Curl, or Django’s built-in test client.
36. How do you debug issues in API responses?
Check status codes, error messages, server logs, and test with known inputs.
37. What do you do if an API you’re calling returns 500?
Check API server logs, retry with fewer inputs, contact provider.
38. How do you handle exceptions in your API?
Use DRF’s exception handlers or `try/except` blocks with `Response`.
39. How do you handle validation errors in serializers?
DRF automatically returns errors if fields are invalid. You can customize messages
using `validate_<field>()` or `raise serializers.ValidationError()`.