fastapi-pagination 0.14.3


pip install fastapi-pagination

  Latest version

Released: Oct 08, 2025

Project Links

Meta
Author: Yurii Karabas
Requires Python: <4.0,>=3.9

Classifiers

Operating System
  • OS Independent

Programming Language
  • Python
  • Python :: 3
  • Python :: 3 :: Only
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13

logo

license test codecov downloads pypi

Introduction

fastapi-pagination is a Python library designed to simplify pagination in FastAPI applications. It provides a set of utility functions and data models to help you paginate your database queries and return paginated responses to your clients.

With fastapi-pagination, you can easily define pagination parameters in your FastAPI endpoint functions, and use them to generate paginated responses that include the requested subset of your data. The library supports a variety of pagination strategies, including cursor-based pagination and page-based pagination.

fastapi-pagination is built on top of the popular fastapi library, and it works with a wide range of SQL and NoSQL databases frameworks. It also supports async/await syntax and is compatible with Python 3.9 and higher.

Features:

  • Simplifies pagination in FastAPI applications.
  • Supports a variety of pagination strategies, including cursor-based pagination and page-based pagination
  • Works with a wide range of SQL and NoSQL databases frameworks, including SQLAlchemy, Tortoise ORM, and PyMongo.
  • Supports async/await syntax.
  • Compatible with Python 3.9 and higher.

For more information on how to use fastapi-pagination, please refer to the official documentation.


Installation

pip install fastapi-pagination

Quickstart

All you need to do is to use Page class as a return type for your endpoint and call paginate function on data you want to paginate.

from fastapi import FastAPI
from pydantic import BaseModel, Field

# import all you need from fastapi-pagination
from fastapi_pagination import Page, add_pagination, paginate

app = FastAPI()  # create FastAPI app
add_pagination(app)  # important! add pagination to your app


class UserOut(BaseModel):  # define your model
    name: str = Field(..., example="Steve")
    surname: str = Field(..., example="Rogers")


users = [  # create some data
    UserOut(name="Steve", surname="Rogers"),
    # ...
]


# req: GET /users
@app.get("/users")
async def get_users() -> Page[UserOut]:
    # use Page[UserOut] as return type annotation
    return paginate(users)  # use paginate function to paginate your data

Please, be careful when you work with databases, because default paginate will require to load all data in memory.

For instance, if you use SQLAlchemy you can use paginate from fastapi_pagination.ext.sqlalchemy module.

from sqlalchemy import select
from fastapi_pagination.ext.sqlalchemy import paginate


@app.get("/users")
def get_users(db: Session = Depends(get_db)) -> Page[UserOut]:
    return paginate(db, select(User).order_by(User.created_at))

Code from Quickstart will generate OpenAPI schema as bellow:

app-example
0.14.3 Oct 08, 2025
0.14.2 Sep 29, 2025
0.14.1 Sep 03, 2025
0.14.0 Aug 20, 2025
0.14.0b1 Jul 26, 2025
0.13.3 Jun 25, 2025
0.13.2 Jun 07, 2025
0.13.1 Apr 24, 2025
0.13.0 Apr 21, 2025
0.13.0a3 Mar 30, 2025
0.13.0a2 Mar 22, 2025
0.13.0a1 Feb 02, 2025
0.12.34 Dec 21, 2024
0.12.33 Dec 18, 2024
0.12.32 Nov 16, 2024
0.12.31 Oct 08, 2024
0.12.30 Oct 07, 2024
0.12.29 Oct 01, 2024
0.12.28 Sep 29, 2024
0.12.27 Sep 10, 2024
0.12.26 Jul 02, 2024
0.12.25 Jun 08, 2024
0.12.24 Apr 26, 2024
0.12.23 Apr 17, 2024
0.12.22 Apr 06, 2024
0.12.21 Mar 19, 2024
0.12.20 Mar 19, 2024
0.12.19 Mar 05, 2024
0.12.18 Mar 04, 2024
0.12.17 Feb 22, 2024
0.12.16 Feb 19, 2024
0.12.15 Feb 11, 2024
0.12.14 Dec 17, 2023
0.12.13 Nov 27, 2023
0.12.12 Oct 30, 2023
0.12.11 Oct 20, 2023
0.12.10 Sep 26, 2023
0.12.9 Aug 24, 2023
0.12.8 Aug 14, 2023
0.12.7 Aug 07, 2023
0.12.6 Jul 17, 2023
0.12.5 Jun 27, 2023
0.12.4 May 18, 2023
0.12.3 May 08, 2023
0.12.2 Apr 18, 2023
0.12.1 Apr 09, 2023
0.12.0 Apr 09, 2023
0.11.4 Feb 11, 2023
0.11.3 Feb 02, 2023
0.11.2 Jan 10, 2023
0.11.1 Dec 11, 2022
0.11.0 Nov 27, 2022
0.10.0 Sep 04, 2022
0.9.3 Apr 16, 2022
0.9.2 Apr 09, 2022
0.9.1 Nov 10, 2021
0.9.0 Sep 26, 2021
0.8.3 Aug 06, 2021
0.8.2 Jul 21, 2021
0.8.1 Jul 04, 2021
0.8.0 Jun 29, 2021
0.7.4 Jun 01, 2021
0.7.3 May 13, 2021
0.7.2 May 12, 2021
0.7.1 Apr 23, 2021
0.7.0 Mar 15, 2021
0.6.1 Mar 10, 2021
0.6.0 Mar 06, 2021
0.5.3 Feb 26, 2021
0.5.2 Jan 20, 2021
0.5.1 Dec 31, 2020
0.5.0 Dec 26, 2020
0.4.1 Dec 04, 2020
0.4.0 Nov 28, 2020
0.3.2 Nov 11, 2020
0.3.1 Nov 11, 2020
0.3.0 Nov 09, 2020
0.2.0 Nov 09, 2020
0.1.0 Nov 06, 2020
Extras:
Dependencies:
fastapi (>=0.93.0)
pydantic (>=1.9.1)
typing-extensions (<5,>=4.8.0)