Skip to main content

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 database frameworks. It also supports async/await syntax and is compatible with Python 3.10 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 database frameworks, including SQLAlchemy, Tortoise ORM, and PyMongo.
  • Supports async/await syntax.
  • Compatible with Python 3.10 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 below:

app-example

Metadata

Release files for fastapi-pagination 0.16.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-pagination 0.16.2
File Size Uploaded Provenance Actions
fastapi_pagination-0.16.2.tar.gz 662.2 kB Without provenance Details for fastapi_pagination-0.16.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-pagination 0.16.2
File Interpreter ABI Platform Actions
fastapi_pagination-0.16.2-py3-none-any.whl Python 3 none any Details for fastapi_pagination-0.16.2-py3-none-any.whl

Total release size: 715.4 kB

Release files / fastapi_pagination-0.16.2.tar.gz

Download URL fastapi_pagination-0.16.2.tar.gz
Size 662.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f9b0bec39f2156e66b3ef73c35cc6eb7345faa86a7c3149d4f63b60b0e9e5e58
BLAKE2b-256 checksum
How to use checksums
9ca173165d7932f1f87bac9f5e12e65864cbce1bd86882ed37c7603888d31299
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / fastapi_pagination-0.16.2-py3-none-any.whl

Download URL fastapi_pagination-0.16.2-py3-none-any.whl
Size 53.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d154e37a1e9fefdd8c3730786863aa9502a90c08b6c59326ae3a5e07ae0a19d
BLAKE2b-256 checksum
How to use checksums
ec01609ad358f28bef0d1f42694e2e1894f1791c443fd8bacc426a518a10ef88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for fastapi-pagination 0.16.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.16.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page