api-filtering-sorting — Claude Code skill: API filters and sorting
A skill that walks your coding agent through building API filtering and sorting: query parsing, validation, and injection safety.
npx skills add secondsky/claude-skills --skill api-filtering-sorting -a claude-codeWhat api-filtering-sorting does
The skill lays out a query-parameter scheme for filtering: plain fields like ?category=electronics plus bracketed operators such as ?price[gte]=100&price[lte]=500. Sorting rides on a single sort parameter, and a leading minus flips the direction: sort=-price,name.
Inside is a Node.js example built on Express and Mongoose. Two arrays, allowedFilters and allowedSorts, act as allowlists — anything else in the request gets dropped. Operators are matched with a regex: eq, ne, gt, gte, lt, lte, in, like. A small parseValue helper converts strings to numbers or booleans.
The allowlist is the part that matters. Skip it and a filter on an arbitrary field goes straight into the database query, so this is worth checking on your own endpoint before someone else pokes at it. The security section also suggests validating types per field, indexing the columns you filter on most, and capping query complexity.
What it helps with
- build a search endpoint with filters across several fields
- add comparison operators for numeric ranges
- restrict sorting to an allowlist of columns
- keep arbitrary field names out of the database query
- coerce query strings into numbers and booleans
Example prompts for the agent
- add filtering and sorting to the /products endpoint using this scheme
- set up an allowlist of filter and sort fields for this route
- parse query params with gte and lte operators for price
How to install api-filtering-sorting
1Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill api-filtering-sorting -a claude-code2Manually: put the files in place
~/.claude/skills/api-filtering-sorting/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/api-filtering-sorting/
1Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill api-filtering-sorting -a codex2Manually: put the files in place
.agents/skills/api-filtering-sorting/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill api-filtering-sorting -a cursor2Manually: put the files in place
.cursor/skills/api-filtering-sorting/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install secondsky/claude-skills api-filtering-sorting2Manually: put the files in place
.github/skills/api-filtering-sorting/SKILL.md1Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill api-filtering-sorting -a gemini-cli2Platform CLI
gemini skills install https://github.com/secondsky/claude-skills --path plugins/api-filtering-sorting/skills/api-filtering-sortingManually: put the files in place
.devin/skills/api-filtering-sorting/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill api-filtering-sorting -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about api-filtering-sorting
How do I install api-filtering-sorting in Claude?
Run npx skills add secondsky/claude-skills --skill api-filtering-sorting -a claude-code. The skill file then lands at ~/.claude/skills/api-filtering-sorting/SKILL.md.
What does api-filtering-sorting do?
It guides the agent through building API filtering and sorting: query-parameter parsing, comparison operators, a field allowlist, and injection protection.
Which operators does the scheme support?
eq, ne, gt, gte, lt, lte, in, and like. Examples include ?status[ne]=deleted, ?price[gte]=100, and ?status[in]=active,pending.
Source text
SKILL.md · MIT
---
name: api-filtering-sorting
description: Builds flexible API filtering and sorting systems with query parameter parsing, validation, and security. Use when implementing search endpoints, building data grids, or creating dynamic query APIs.
license: MIT
---
# API Filtering & Sorting
Build flexible filtering and sorting systems that handle complex queries efficiently.
## Query Parameter Syntax
```
GET /products?category=electronics&price[gte]=100&price[lte]=500&sort=-price,name
```
## Implementation (Node.js)
```javascript
const allowedFilters = ['category', 'status', 'price', 'createdAt'];
const allowedSorts = ['name', 'price', 'createdAt'];
app.get('/products', async (req, res) => {
const filter = {};
const sort = {};
// Parse filters
for (const [key, value] of Object.entries(req.query)) {
if (key === 'sort') continue;
const match = key.match(/^(\w+)\[(\w+)\]$/);
if (match) {
const [, field, operator] = match;
if (!allowedFilters.includes(field)) continue;
filter[field] = { [`$${operator}`]: parseValue(value) };
} else if (allowedFilters.includes(key)) {
filter[key] = value;
}
}
// Parse sort
if (req.query.sort) {
for (const field of req.query.sort.split(',')) {
const direction = field.startsWith('-') ? -1 : 1;
const name = field.replace(/^-/, '');
if (allowedSorts.includes(name)) sort[name] = direction;
}
}
const products = await Product.find(filter).sort(sort);
res.json({ data: products });
});
function parseValue(value) {
if (value === 'true') return true;
if (value === 'false') return false;
if (!isNaN(value)) return Number(value);
return value;
}
```
## Filter Operators
| Operator | Meaning | Example |
|----------|---------|---------|
| eq | Equals | `?status=active` |
| ne | Not equals | `?status[ne]=deleted` |
| gt/gte | Greater than | `?price[gte]=100` |
| lt/lte | Less than | `?price[lte]=500` |
| in | In array | `?status[in]=active,pending` |
| like | Contains | `?name[like]=phone` |
## Security
- Whitelist allowed filter fields
- Validate input types per field
- Index frequently-filtered columns
- Limit query complexity
- Prevent SQL/NoSQL injection
## Best Practices
- Support common operators
- Cache filter option lists
- Monitor query performance
- Provide sensible defaults
© secondsky · MIT · secondsky/claude-skills