api-filtering-sorting

Implement advanced filtering and sorting capabilities for APIs with query parsing, field validation, and optimization. Use when building search features, complex queries, or flexible data retrieval endpoints.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "api-filtering-sorting" with this command: npx skills add aj-geddes/useful-ai-prompts/aj-geddes-useful-ai-prompts-api-filtering-sorting

API Filtering & Sorting

Table of Contents

Overview

Build flexible filtering and sorting systems that handle complex queries efficiently with proper validation, security, and performance optimization.

When to Use

  • Building search and filter interfaces
  • Implementing advanced query capabilities
  • Creating flexible data retrieval endpoints
  • Optimizing query performance
  • Validating user input for queries
  • Supporting complex filtering logic

Quick Start

Minimal working example:

// Node.js filtering implementation
app.get("/api/products", async (req, res) => {
  const filters = {};
  const sortOptions = {};

  // Parse filtering parameters
  const allowedFilters = [
    "category",
    "minPrice",
    "maxPrice",
    "inStock",
    "rating",
  ];
  for (const key of allowedFilters) {
    if (req.query[key]) {
      filters[key] = req.query[key];
    }
  }

  // Build MongoDB query
  const mongoQuery = {};

  if (filters.category) {
    mongoQuery.category = filters.category;
  }
// ... (see reference guides for full implementation)

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Query Parameter FilteringQuery Parameter Filtering
Advanced Filter ParserAdvanced Filter Parser
Filter Builder PatternFilter Builder Pattern
Python Filtering (SQLAlchemy)Python Filtering (SQLAlchemy)
Elasticsearch FilteringElasticsearch Filtering
Query ValidationQuery Validation

Best Practices

✅ DO

  • Whitelist allowed filter fields
  • Validate all input parameters
  • Index fields used for filtering
  • Support common operators
  • Provide faceted navigation
  • Cache filter options
  • Limit filter complexity
  • Document filter syntax
  • Use database-native operators
  • Optimize queries with indexes

❌ DON'T

  • Allow arbitrary field filtering
  • Support unlimited operators
  • Ignore SQL injection risks
  • Create complex filter logic
  • Expose internal field names
  • Filter on unindexed fields
  • Allow deeply nested filters
  • Skip input validation
  • Combine all filters with OR
  • Ignore performance impact

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

nodejs-express-server

No summary provided by upstream source.

Repository SourceNeeds Review
General

markdown-documentation

No summary provided by upstream source.

Repository SourceNeeds Review
General

rest-api-design

No summary provided by upstream source.

Repository SourceNeeds Review