Guide

Filtering & sorting

Paginate, filter, sort and shape the results of list requests with query parameters, and load related records in the same request.

Overview

Query parameters allow you to customize how data is returned from your API endpoints. They are added to the URL after a ?and can be combined using &.

Note: Your API is accessible via subdomain routing. Use https://YOUR_PROJECT_SLUG.codefreeapi.com/api/v1/...instead of path-based URLs.

Supported Query Parameters

pageSpecify which page of results to return (for pagination)
limitSet the number of records per page (default: 20, maximum: 100)
field[op]Filter by any field, e.g. status=active or price[gte]=10
sortSort by one or more fields; prefix with - for descending (default: -createdAt)
fieldsReturn only the listed fields (comma-separated)
populatePopulate related entities (comma-separated field names)

Pagination

Pagination allows you to retrieve data in chunks, making it easier to handle large datasets and improve performance. By default, the API returns 20 records per page.

page Parameter

The page parameter specifies which page of results you want to retrieve. Pages are 1-indexed (first page is 1).

Example Request
GET /api/v1/posts?page=2
Response Structure
{
  "data": [...],
  "pagination": {
    "page": 2,
    "limit": 20,
    "total": 45,
    "totalPages": 3,
    "nextCursor": "MjAyNi0xMC0yOCAxNTo1NDo1MC45NDc..."
  }
}

Large lists: next page by cursor

For long lists, or to read everything in batches, pass the nextCursor of a response as after. It stays fast however far you go, and records added meanwhile don't shift your pages. Cursor pages skip the total count, and nextCursor is null on the last page. Works with filters and search, in the default order or sort=createdAt.

GET /api/v1/posts?limit=100&after=MjAyNi0xMC0yOCAxNTo1NDo1MC45NDc...

limit Parameter

The limit parameter controls how many records are returned per page. The default is 20 and the maximum is 100; larger values are capped at 100.

Example Request
GET /api/v1/posts?limit=10

Combining Parameters

You can combine page andlimit to get specific pages with custom page sizes.

Example Request
GET /api/v1/posts?page=1&limit=50
💡 Tip

Use smaller limits (10-50) for better performance. Larger limits may slow down your requests.

Filtering

Add any field of your entity as a query parameter to return only matching records. Use field=value for an exact match, or field[operator]=value for other comparisons. Multiple filters are combined, so a record must match all of them.

Operators

OperatorMeaningExample
eqEquals (the default when no operator is given)?status=published
neNot equal?status[ne]=draft
gt / gteGreater than / greater than or equal?views[gte]=100
lt / lteLess than / less than or equal?price[lt]=50
inAny of a comma-separated list?category[in]=news,tips
containsText contains (case-insensitive)?title[contains]=api

Examples

Only published posts
GET /api/v1/posts?status=published
Products priced from 10 to 50
GET /api/v1/products?price[gte]=10&price[lte]=50
Posts with "react" anywhere in the title
GET /api/v1/posts?title[contains]=react
Posts created since 1 January 2024
GET /api/v1/posts?createdAt[gte]=2024-01-01
Open tasks of one user (relation field)
GET /api/v1/tasks?done=false&assignee=USER_ID
How values are compared
  • Number fields compare as numbers (10 is greater than 9), date fields as dates.
  • contains is case-insensitive and works on text fields.
  • Boolean fields take true or false with eq / ne.
  • id and relation fields support eq, ne and in. JSON fields cannot be filtered.
  • You can also filter on createdAt and updatedAt.
  • Parameters starting with _ (like cache busters) are ignored; any other unknown field returns a 400 error.

Search

?q= finds records where any text field (and the id) contains the words, case-insensitive. It combines with filters, sorting and paging, for example a search box over a filtered list:

GET /api/v1/posts?q=react&status=published&sort=-createdAt

If your entity has its own field named q, ?q= filters that field instead.

Sorting

Use sort with a comma-separated list of up to 5 fields. Prefix a field with - to sort descending. Without sort, the newest records come first (-createdAt). Records without a value for the sort field are listed last.

Cheapest first
GET /api/v1/products?sort=price
Most expensive first
GET /api/v1/products?sort=-price
Featured posts first, then alphabetical
GET /api/v1/posts?sort=-featured,title

Selecting Fields

Use fields to return only the fields you need, which keeps responses small. id is always included, and so are relations you ask for with populate.

Only the title and creation date of each post
GET /api/v1/posts?fields=title,createdAt
Response
{
  "data": [
    { "id": "3f2c...", "title": "Hello world", "createdAt": "2024-05-01T09:30:00.000Z" }
  ],
  "pagination": { "page": 1, "limit": 20, "total": 1, "totalPages": 1 }
}

Populate Relations

When you have relation fields (fields that reference other entities), you can use thepopulate parameter to automatically fetch and include related data in the response.

How It Works

If you have a relation field (e.g., a "Posts" entity with an "author" field that relates to a "Users" entity), you can populate that field to get the full user data instead of just the user ID.

Without populate:
{
  "id": "123",
  "title": "My Post",
  "author": "user-id-456"  // Just the ID
}
With populate:
{
  "id": "123",
  "title": "My Post",
  "author": {              // Full user object
    "id": "user-id-456",
    "name": "John Doe",
    "email": "[email protected]"
  }
}

populate Parameter

Pass a comma-separated list of relation field names that you want to populate.

Example Request
GET /api/v1/posts?populate=author,category
⚠️ Note

The populate parameter only works with relation field types. If you specify a field that isn't a relation, it will be ignored.

Errors

If a filter, sort or field name doesn't exist, or a value has the wrong type, the API responds with 400 Bad Request and explains what to fix:

GET /api/v1/posts?colour=red

{
  "error": "Invalid query",
  "code": "INVALID_QUERY",
  "message": "Unknown field \"colour\". Available fields: id, createdAt, updatedAt, title, status, views",
  "requestId": "9b1c…"
}

Every error code the API can return is listed in the errors guide.

Complete Examples

Here are some real-world examples combining multiple query parameters.

Example 1: Paginated List

Get the second page of posts with 10 items per page.

Request
GET /api/v1/posts?page=2&limit=10

Example 2: Populate Relations

Get posts with author and category information populated.

Request
GET /api/v1/posts?populate=author,category

Example 3: Combined Parameters

Get the 25 most viewed published posts with the author populated, returning only the title and views.

Request
GET /api/v1/posts?status=published&sort=-views&limit=25&fields=title,views&populate=author

cURL Examples

Here are complete cURL examples you can use in your terminal or scripts.

Pagination Example

curl "https://YOUR_PROJECT_SLUG.codefreeapi.com/api/v1/posts?page=2&limit=10" \
  -H "X-API-Key: YOUR_API_KEY"

Populate Example

curl "https://YOUR_PROJECT_SLUG.codefreeapi.com/api/v1/posts?populate=author,category" \
  -H "X-API-Key: YOUR_API_KEY"

Filter and Sort Example

curl -g "https://YOUR_PROJECT_SLUG.codefreeapi.com/api/v1/posts?status=published&views[gte]=100&sort=-createdAt" \
  -H "X-API-Key: YOUR_API_KEY"

-g stops curl from treating the square brackets as a URL pattern.