Skip to content

Links in Pagination

If you want to add pagination links to your response, you can use pages from fastapi_pagination.links module. It contains Page class for page-number pagination, LimitOffsetPage for limit-offset pagination.

Response schema will contain additional links field with pagination links:

  • first - link to the first page.
  • last - link to the last page.
  • self - link to the current page.
  • next - link to the next page.
  • prev - link to the previous page.


For instance, let user page-number pagination from links module:

from typing import List

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

from fastapi_pagination import add_pagination, paginate
from fastapi_pagination.links import Page

app = FastAPI()

class UserOut(BaseModel):
    name: str
    email: EmailStr

users: List[UserOut] = [
    # ...

def get_users() -> Page[UserOut]:
    return paginate(users)

Now when we will call /users endpoint we will get paginated data like this:

GET /users?page=2&size=3
  "items": [
      "name": "John",
      "email": ""
      "name": "Jane",
      "email": ""
      "name": "Bob",
      "email": ""
  "page": 2,
  "size": 3,
  "pages": 34,
  "total": 100,
  "links": {
    "first": "http://localhost:8000/users?page=1&size=3",
    "last": "http://localhost:8000/users?page=34&size=3",
    "self": "http://localhost:8000/users?page=2&size=3",
    "next": "http://localhost:8000/users?page=3&size=3",
    "prev": "http://localhost:8000/users?page=1&size=3"


Code above will add pagination parameters to the endpoint and you will see them in the OpenAPI docs.

OpenAPI Result