MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Documenting API Endpoints0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
9.1Swagger UI Integration9.2Redoc Documentation9.3Documenting API Endpoints
HomeTutorialsREST API Development with DjangoDocumenting API Endpoints
Build REST API for E-commerce App

Documenting API Endpoints

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Documenting Request Bodies, Responses, and Query Parameters with @extend_schema

While drf-spectacular inspects serializers automatically, custom actions, query parameters, multipart file uploads, and dynamic responses require explicit annotations. DRF provides the powerful @extend_schema decorator to customize every detail of OpenAPI generation.


1. The @extend_schema Decorator

The @extend_schema decorator can be applied to @api_view functions, APIView methods (get, post), or ViewSet action methods (@action).

1from rest_framework import serializers, status
2from rest_framework.views import APIView
3from rest_framework.response import Response
4from drf_spectacular.utils import extend_schema, OpenApiParameter, OpenApiExample
5from drf_spectacular.types import OpenApiTypes
6 
7class CartCheckoutSerializer(serializers.Serializer):
8 shipping_address = serializers.CharField(max_length=255)
9 payment_method = serializers.ChoiceField(choices=['stripe', 'paypal', 'apple_pay'])
10 coupon_code = serializers.CharField(required=False, allow_blank=True)
11 
12class OrderSuccessSerializer(serializers.Serializer):
13 order_id = serializers.UUIDField()
14 status = serializers.CharField()
15 total_amount = serializers.DecimalField(max_digits=10, decimal_places=2)
16 created_at = serializers.DateTimeField()

2. Annotating Custom View Logic

1class CheckoutAPIView(APIView):
2 @extend_schema(
3 summary="Process cart checkout",
4 description="Creates an order from the active user's cart, verifies inventory, and initiates payment.",
5 request=CartCheckoutSerializer,
6 responses={
7 201: OrderSuccessSerializer,
8 400: OpenApiTypes.OBJECT,
9 409: OpenApiTypes.OBJECT,
10 },
11 examples=[
12 OpenApiExample(
13 'Successful Checkout Request',
14 value={
15 'shipping_address': '742 Evergreen Terrace, Springfield, OR',
16 'payment_method': 'stripe',
17 'coupon_code': 'SPRING20'
18 },
19 request_only=True
20 ),
21 OpenApiExample(
22 'Successful Order Response',
23 value={
24 'order_id': '3fa85f64-5717-4562-b3fc-2c963f66afa6',
25 'status': 'PENDING_PAYMENT',
26 'total_amount': '159.99',
27 'created_at': '2026-09-15T12:00:00Z'
28 },
29 response_only=True
30 )
31 ],
32 tags=['Orders & Billing']
33 )
34 def post(self, request):
35 serializer = CartCheckoutSerializer(data=request.data)
36 serializer.is_valid(raise_exception=True)
37 # Business logic...
38 return Response({'status': 'PENDING_PAYMENT'}, status=status.HTTP_201_CREATED)

3. Documenting Query Parameters with OpenApiParameter

When an endpoint filters on parameters not present in the request body (e.g. GET requests), use OpenApiParameter:

1from rest_framework.viewsets import ReadOnlyModelViewSet
2from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiParameter
3 
4@extend_schema_view(
5 list=extend_schema(
6 summary="List all courses with filtering",
7 parameters=[
8 OpenApiParameter(
9 name='category',
10 type=str,
11 location=OpenApiParameter.QUERY,
12 description='Filter courses by category slug (e.g., data-analysis, web-dev)',
13 required=False
14 ),
15 OpenApiParameter(
16 name='is_featured',
17 type=bool,
18 location=OpenApiParameter.QUERY,
19 description='Pass true to return only featured showcase courses',
20 required=False
21 )
22 ]
23 )
24)
25class CourseViewSet(ReadOnlyModelViewSet):
26 # ViewSet implementation...
27 pass

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which decorator from drf_spectacular.utils is used to customize the OpenAPI schema of a view or method?
Question 2
What decorator is used at the class level to override schemas for multiple ViewSet actions (like list, retrieve, create) simultaneously?
Question 3
How do you document a URL query parameter like ?search=term using drf-spectacular?
Question 4
What does the examples argument in @extend_schema accomplish?
Question 5
What is the purpose of the tags argument in @extend_schema?
Next Lesson

Build REST API for E-commerce App

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Redoc DocumentationBuild REST API for E-commerce App

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master REST API Development with Django with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext