Pagination in APIs
API pagination controls how many results are returned per request. DRF supports page number pagination, limit/offset pagination, and cursor pagination. Pagination prevents large responses and reduces server load.
15 min•By Priygop Team•Updated 2026
Pagination Types
- PageNumberPagination — ?page=2 (most common)
- LimitOffsetPagination — ?limit=10&offset=20
- CursorPagination — Opaque cursor (best for real-time data)
- Set globally or per-view
- Custom page sizes per view
- Response includes: count, next, previous, results
Pagination Example
Pagination Example
# settings.py — Global pagination
# REST_FRAMEWORK = {
# 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
# 'PAGE_SIZE': 10,
# }
# Custom pagination class
# from rest_framework.pagination import PageNumberPagination
# class LargeResultsPagination(PageNumberPagination):
# page_size = 50
# page_size_query_param = 'page_size'
# max_page_size = 100
# class SmallResultsPagination(PageNumberPagination):
# page_size = 5
# Per-view pagination
# class PostViewSet(viewsets.ModelViewSet):
# pagination_class = LargeResultsPagination
# API Response format:
# {
# "count": 150,
# "next": "http://api.example.com/posts/?page=3",
# "previous": "http://api.example.com/posts/?page=1",
# "results": [
# {"id": 1, "title": "Post 1"},
# {"id": 2, "title": "Post 2"},
# ...
# ]
# }
# Client can request custom page size:
# /api/posts/?page=2&page_size=25Tip
Tip
Always paginate API responses. Use PageNumberPagination for simple APIs, CursorPagination for real-time data.
Diagram
Loading diagram…
QuerySets are LAZY — no DB hit until evaluated.
Common Mistake
Warning
Returning all records without pagination. Large datasets cause timeout errors and poor performance.
Practice Task
Note
(1) Add PageNumberPagination to settings. (2) Test pagination with large datasets. (3) Try CursorPagination.
Quick Quiz
Key Takeaways
- API pagination controls how many results are returned per request.
- PageNumberPagination — ?page=2 (most common)
- LimitOffsetPagination — ?limit=10&offset=20
- CursorPagination — Opaque cursor (best for real-time data)