Skip to main content
skill CX Codex

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.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add secondsky/claude-skills --skill api-filtering-sorting -a claude-code
Open on GitHub

What 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-code

2Manually: put the files in place

~/.claude/skills/api-filtering-sorting/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/api-filtering-sorting/

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

Similar skills

Everything for Backend and APIs →

From the same set89

websocket-implementation A skill that has your agent write the WebSocket server and client: rooms, reconnection, Redis scaling. CX Codex +1 425installs root-cause-tracing Walks an agent backward through the call stack until it finds the original trigger. Useful when an error surfaces deep in execution, not at the entry point. CX Codex +1 426installs recommendation-engine A skill for building recommendation systems: collaborative filtering, matrix factorization, hybrid approaches. Useful for product recommendations and personalization. CX Codex +1 423installs api-response-optimization A coding-agent skill that walks through payload trimming, HTTP caching and compression for API endpoints. Useful when responses feel slow. CX Codex +1 422installs bun-drizzle-integration A skill that walks your agent through wiring Drizzle ORM to the bun:sqlite driver: schema, migrations, CRUD and transactions. CX Codex +1 420installs drizzle-orm-d1 A skill that gives your agent working knowledge of Drizzle ORM on Cloudflare D1: schemas, typed queries, Drizzle Kit migrations and the usual D1 errors. CX Codex +1 416installs push-notification-setup A skill that walks your agent through push notification setup with Firebase Cloud Messaging and native services. Useful when you add notifications to an app or debug background messages. CX Codex +1 418installs bun-runtime A skill that teaches your agent the Bun runtime: running files, watch and hot modes, env vars, CLI flags and module resolution. CX Codex +1 414installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor