PieQL is a lightweight FastAPI middleware that allows clients to filter JSON responses from your endpoints using JMESPath queries.
With PieQL, clients can request only the data they need without changing your endpoint logic.
- Works with any HTTP method (
GET,POST, etc.) - Supports
JSONResponseand Pydantic models (viaJSONResponse) - Automatically ignores non-JSON responses (HTML, files, streaming responses)
- Simple integration with FastAPI via one line of code
- Fully supports nested JSON structures and JMESPath functions like
sum,max,min, etc.
Install via pip:
pip install pieql jmespath fastapifrom fastapi import FastAPI
from pieql import PieQL
app = FastAPI()
PieQL(app) # default query parameter: "__schema"You can customize the query parameter name:
PieQL(app, param_name="filter")from fastapi import FastAPI
from pieql import PieQL
app = FastAPI()
PieQL(app)
@app.get("/items")
def items():
return {
"users": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"},
{"id": 3, "name": "Carol"}
]
}- Get all user names:
GET /items?__schema=users[*].name
Response:
["Alice", "Bob", "Carol"]- Sum of user IDs:
GET /items?__schema=sum(users[*].id)
Response:
6- Get user with
id=2:
GET /items?__schema=users[?id==`2`]
Response:
[{"id": 2, "name": "Bob"}]If the JMESPath query is invalid, PieQL returns HTTP 400:
{
"error": "Invalid JMESPath users[??].name for query: SyntaxError..."
}- Middleware only processes
JSONResponseobjects. All other response types are returned unchanged. - Works seamlessly with nested JSON structures and Pydantic models.
MIT