api-filtering-sorting

Implements query parameter parsing, validation, and sorting for REST API endpoints in Node.js.

Updated Jun 22, 2026
One-click install
npx skills add https://github.com/aicodepro/ai-agent-nexi --skill api-filtering-sorting-aicodepro
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: api-filtering-sorting
Source: https://github.com/aicodepro/ai-agent-nexi/tree/main/agent/skills/api-filtering-sorting
Command: npx skills add https://github.com/aicodepro/ai-agent-nexi --skill api-filtering-sorting-aicodepro

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building API endpoints that accept arbitrary filter and sort parameters often leads to inconsistent query syntax, unvalidated input, and injection vulnerabilities. This Skill provides a structured pattern for parsing, whitelisting, and applying filters and sorts safely. ## Core Features & Use Cases - Query Parameter Parsing: Parse bracket-style operators like price[gte]=100 and comma-separated sort fields like sort=-price,name. - Whitelist Validation: Restrict filterable and sortable fields to explicit allowlists to prevent injection attacks. - Operator Support: Covers eq, ne, gt/gte, lt/lte, in, and like operators with automatic type coercion. - Use Case: When building a product catalog endpoint in Express with MongoDB, use this pattern to let clients filter by category and price range and sort by multiple fields without exposing the database to unvalidated queries. ## Quick Start Ask the assistant to add filtering and sorting with whitelisted fields and bracket operators to your Express products endpoint.

Frequently Asked Questions about api-filtering-sorting

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add filtering and sorting to a REST API endpoint?

Parse query parameters with bracket syntax like price[gte]=100 for operators and comma-separated values like sort=-price,name for sorting. Whitelist allowed fields, then build a query object passed to your database layer such as MongoDB's find and sort.

How to parse query string operators like price[gte] in Express?

Iterate over req.query entries and match keys against a regex like /^(\w+)\[(\w+)\]$/ to extract the field and operator. Map the operator to a database operator such as $gte, and skip any field not in your allowed filters list.

How do I prevent NoSQL injection in API filter parameters?

Whitelist allowed filter and sort fields before building the query, validate input types per field, and coerce values to expected types like numbers or booleans. Never pass raw req.query objects directly to the database driver.

What filter operators should a search API support?

A practical set includes eq, ne, gt, gte, lt, lte, in, and like. These cover equality checks, numeric ranges, array membership, and substring matching, which handle most data grid and search endpoint requirements.

Why is my API filter query slow on large collections?

Filtering on unindexed columns forces full collection scans. Index frequently filtered and sorted columns, limit query complexity per request, and monitor query performance to catch slow patterns early.