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]=10sortSort 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).
GET /api/v1/posts?page=2{
"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.
GET /api/v1/posts?limit=10Combining Parameters
You can combine page andlimit to get specific pages with custom page sizes.
GET /api/v1/posts?page=1&limit=50Use 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
| Operator | Meaning | Example |
|---|---|---|
eq | Equals (the default when no operator is given) | ?status=published |
ne | Not equal | ?status[ne]=draft |
gt / gte | Greater than / greater than or equal | ?views[gte]=100 |
lt / lte | Less than / less than or equal | ?price[lt]=50 |
in | Any of a comma-separated list | ?category[in]=news,tips |
contains | Text contains (case-insensitive) | ?title[contains]=api |
Examples
GET /api/v1/posts?status=publishedGET /api/v1/products?price[gte]=10&price[lte]=50GET /api/v1/posts?title[contains]=reactGET /api/v1/posts?createdAt[gte]=2024-01-01GET /api/v1/tasks?done=false&assignee=USER_ID- Number fields compare as numbers (
10is greater than9), date fields as dates. containsis case-insensitive and works on text fields.- Boolean fields take
trueorfalsewitheq/ne. idand relation fields supporteq,neandin. JSON fields cannot be filtered.- You can also filter on
createdAtandupdatedAt. - 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=-createdAtIf 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.
GET /api/v1/products?sort=priceGET /api/v1/products?sort=-priceGET /api/v1/posts?sort=-featured,titleSelecting 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.
GET /api/v1/posts?fields=title,createdAt{
"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.
{
"id": "123",
"title": "My Post",
"author": "user-id-456" // Just the ID
}{
"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.
GET /api/v1/posts?populate=author,categoryThe 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.
GET /api/v1/posts?page=2&limit=10Example 2: Populate Relations
Get posts with author and category information populated.
GET /api/v1/posts?populate=author,categoryExample 3: Combined Parameters
Get the 25 most viewed published posts with the author populated, returning only the title and views.
GET /api/v1/posts?status=published&sort=-views&limit=25&fields=title,views&populate=authorcURL 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.