Skip to content

Limit Offset Pagination

You can use limit-offset pagination type to paginate data by limit and offset. In this tutorial, you will learn how to use limit-offset pagination type.

General information

limit parameter is used to specify the number of items to fetch, and offset parameter is used to specify the number of items that we need to skip.

By default limit is 50 and offset is 0, but you can change them.

Response schema will contain:

  • items - list of items paginated items.
  • limit - number of items per page.
  • offset - number of skipped items.
  • total - total number of items.

Example

To use limit-offset you need to import LimitOffsetPage from fastapi_pagination and use it as a response model.

from typing import List

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

from fastapi_pagination import LimitOffsetPage, add_pagination, paginate

app = FastAPI()
add_pagination(app)


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


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


@app.get("/users")
def get_users() -> LimitOffsetPage[UserOut]:
    return paginate(users)

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

GET /users?limit=3&offset=2
{
  "items": [
    {
      "name": "John",
      "email": "john@example.com"
    },
    {
      "name": "Jane",
      "email": "jane@example.com"
    },
    {
      "name": "Bob",
      "email": "bob@example.com"
    }
  ],
  "limit": 3,
  "offset": 2,
  "total": 100
}

OpenAPI

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

OpenAPI Result