0Pricing
Elixir & Phoenix: Scalable Backend Development · Lesson

Pagination, Filtering & API Versioning

Make Phoenix APIs scalable and maintainable with query pagination, flexible filtering, and clean versioning strategies.

Pagination, Filtering & API Versioning is a free Elixir & Phoenix: Scalable Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elixir & Phoenix: Scalable Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Pagination Matters

Returning every record in one response is slow and memory-heavy. Pagination sends data in manageable pages.

Offset-Based Pagination

The classic approach uses limit and offset driven by query params like ?page=2&per_page=20.

from(p in Post, limit: ^per_page, offset: ^((page - 1) * per_page))

Returning Page Metadata

Clients need to know totals. Include metadata such as current page, page size, and total count alongside the data.

%{
  data: posts,
  meta: %{page: 2, per_page: 20, total: 137}
}

The Downside of Offset

Large offsets get slow because the database still scans skipped rows, and rows shifting between requests cause duplicates.

Cursor-Based Pagination

Cursor pagination uses the last seen value (often an id or timestamp) to fetch the next page, staying fast and stable.

from(p in Post,
  where: p.id > ^cursor,
  order_by: [asc: p.id],
  limit: ^limit)

Filtering Results

Let clients narrow results with query params. Build the query conditionally based on which params are present.

def filter(query, %{"status" => status}) do
  from(q in query, where: q.status == ^status)
end
def filter(query, _), do: query

Composing Multiple Filters

Reduce over a params map to apply each supported filter, keeping the query composable and safe from injection via parameter binding.

Enum.reduce(params, base_query, fn
  {"author", a}, q -> from(p in q, where: p.author == ^a)
  _, q -> q
end)

Sorting

Accept a sort param and map it to a safe whitelist of allowed columns to avoid arbitrary ordering.

order = case params["sort"] do
  "newest" -> [desc: :inserted_at]
  _ -> [asc: :id]
end

Why Version Your API?

Versioning lets you evolve the API without breaking existing clients when you change response shapes or behavior.

URL Path Versioning

The most common strategy puts the version in the path, scoped in the router.

scope "/api/v1", MyAppWeb.V1 do
  pipe_through :api
  resources "/posts", PostController
end

Header-Based Versioning

Alternatively, select the version from an Accept header so URLs stay stable. Choose ONE strategy and stay consistent.

# Accept: application/vnd.myapp.v2+json

Quick Check

Test your API scaling knowledge.

Recap

You made APIs scalable and durable:

  • Paginate with offset (simple) or cursor (fast, stable)
  • Return page metadata with the data
  • Apply composable, parameter-bound filters and whitelisted sorting
  • Version via URL path or Accept header, consistently

Frequently asked questions

Is the “Pagination, Filtering & API Versioning” lesson free?

Yes — the full text of “Pagination, Filtering & API Versioning” is free to read here on the web, and the Elixir & Phoenix: Scalable Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elixir & Phoenix: Scalable Backend Development course, upgrade to CoddyKit PRO.

What will I learn in “Pagination, Filtering & API Versioning”?

Make Phoenix APIs scalable and maintainable with query pagination, flexible filtering, and clean versioning strategies. You practise Elixir & Phoenix: Scalable Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Elixir & Phoenix: Scalable Backend Development?

No prior experience is required. Elixir & Phoenix: Scalable Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Pagination, Filtering & API Versioning” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Elixir & Phoenix: Scalable Backend Development lesson?

Yes. Every Elixir & Phoenix: Scalable Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. API Design Principles and Best Practices
  2. Implementing API Endpoints and Serialization
  3. Authentication and Authorization Strategies
  4. Pagination, Filtering & API Versioning
← Back to Elixir & Phoenix: Scalable Backend Development