Design REST API filtering: allow conditions and validate queries
API filtering lets a client request only collection items that match conditions. A safe filter API defines which fields, operators, and value formats clients may use, then validates and applies them in the data store. It does not accept arbitrary SQL fragments or let client input set access permissions.
API filtering lets a client request only collection items that match conditions. A safe filter API defines which fields, operators, and value formats clients may use, then validates and applies them in the data store. It does not accept arbitrary SQL fragments or let client input set access permissions.
Define the client filter contract
OData defines a $filter query option with comparison operators such as eq, ne, gt, ge, lt, and le, and boolean composition with and, or, not, and parentheses. For example, this request asks for orders whose status is paid and total is at least 100:
GET /api/orders?$filter=status%20eq%20'paid'%20and%20total%20ge%20100This is example syntax. Not every REST API supports OData, and an OData-based API may still allow different operators on different routes or fields. Document filterable fields, types, operators, null meaning, date format, maximum expression size, and error responses. If the goal is free-text search, define $search or another search contract distinct from structured comparisons.
Validate the expression and execute it safely
The server should parse the filter into a syntax tree and check that each field and operator is on an allowlist. Then validate and convert each value to the field's type. Do not concatenate values into SQL strings; pass values as parameters. Database identifiers such as column names generally cannot be value parameters, so map allowed field names to query expressions selected by the server.
For example, the server could translate the request into a predicate like this, with status and total selected from allowed fields and values bound separately:
WHERE status = :p0 AND total >= :p1Microsoft Data API builder documents compiling its OData-inspired $filter values to parameterized SQL. That behavior is specific to that product. Other implementations should also use their own parser or a trusted query builder with bound values; they should not insert the raw filter string into SQL.
Control authorization and cost separately
A client filter narrows results; it does not grant access. For example, sending tenantId eq 7 does not authorize a user to read that tenant's data. The server must independently apply the authenticated user's access scope, then combine the client filter. If fields have different read permissions, explicitly define whether sensitive fields may be used in filters and enforce that on the server.
Even valid conditions can be expensive. Offer only approved fields and operators, cap nesting depth, condition count, string length, and result size, and filter in the data store. Microsoft's REST guidance recommends documenting bounds on filter-expression and request complexity and applying filtering in the data store rather than after loading all rows into memory. ASP.NET Core OData allows services to enable query options selectively rather than enabling all by default.
Specify nulls, encoding, and empty results
In OData-style filtering, only resources whose expression evaluates to true are included. Microsoft OData documentation says expressions evaluating to false or null are omitted. This is OData behavior; another API may define different semantics. Specify whether eq null and ne null are supported and whether a missing field differs from explicit null.
Because a filter is carried in a URL query string, clients must URL-encode reserved characters such as spaces, quotes, and parentheses correctly. The server should decode, then validate syntax and types. An unknown field, unsupported operator, or malformed date should produce a documented error rather than being silently ignored. A valid request with no matches should return an empty result; keep that distinct from an invalid request.
Implementation checklist
Document filterable fields, operators, types, null behavior, and date rules in the API contract.
Parse the expression and validate fields and operators against allowlists.
Type-check values and bind them as parameters; select SQL identifiers through server-controlled mappings.
Apply the authenticated user's access scope independently of client filters.
Set bounds for expression depth, condition count, string length, page size, and query cost.
Review storage query plans and representative conditions; verify errors for invalid syntax, nulls, encoding, and empty results.
Sources
[OData: Query options overview](https://learn.microsoft.com/en-us/odata/concepts/queryoptions-overview)
[ASP.NET Core OData 8: Fundamentals overview](https://learn.microsoft.com/en-us/odata/webapi-8/fundamentals/overview)
[Microsoft Data API builder: Filtering data in REST (`$filter`)](https://learn.microsoft.com/en-us/azure/data-api-builder/keywords/filter-rest)
[Microsoft Azure Architecture Center: RESTful Web API implementation best practices](https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-implementation)