Query Capabilities

Query Capabilities

api.vision provides powerful query capabilities that let you filter, sort, paginate, and select fields from your API resources. This guide provides an overview of these features.

Overview

api.vision automatically creates a RESTful API for your resources with full CRUD operations. Beyond the basic operations, the API supports a rich query language that lets you:

  • Filter resources based on field values
  • Sort results in ascending or descending order
  • Paginate large result sets
  • Select specific fields to include in responses
  • Expand relationships to include related resources

These capabilities are accessed through query parameters in your API requests.

Basic Endpoints

For each resource (e.g., "users"), api.vision automatically creates the following endpoints:

MethodPathDescription
GET/usersList all users
GET/users/:idGet a single user by ID
POST/usersCreate a new user
PUT/users/:idUpdate a user (replace all fields)
PATCH/users/:idPartially update a user
DELETE/users/:idDelete a user

Query Features

Filtering

Filter resources based on field values using query parameters.

GET /users?role=admin&status=active

Learn more about filtering →

Sorting

Sort results by one or more fields in ascending or descending order.

GET /products?sort=-price,name

Learn more about sorting →

Pagination

Limit the number of results returned and navigate through pages.

GET /posts?page=2&limit=10

Learn more about pagination →

Field Selection

Specify which fields to include in the response.

GET /users?fields=id,username,email

Learn more about field selection →

Relationship Expansion

Include related resources in the response.

GET /posts?expand=author,comments

Learn more about relationship expansion →

Combining Query Features

You can combine multiple query features in a single request to create powerful, targeted API calls:

GET /products?
  category=electronics&
  price_gte=100&
  sort=-rating,price&
  fields=id,name,price,rating&
  page=1&
  limit=20

This request:

  1. Filters for electronics products with a price of 100 or more
  2. Sorts by rating (descending) and then by price (ascending)
  3. Returns only the id, name, price, and rating fields
  4. Returns the first page of results with 20 items per page

Response Format

By default, api.vision returns JSON responses. For list endpoints, the response is an array of resource objects:

[
  {
    "id": 1,
    "name": "Smartphone X",
    "price": 699.99,
    "category": "electronics",
    "inStock": true
  },
  {
    "id": 2,
    "name": "Laptop Pro",
    "price": 1299.99,
    "category": "electronics",
    "inStock": true
  }
]

For single resource endpoints, the response is a single object:

{
  "id": 1,
  "name": "Smartphone X",
  "price": 699.99,
  "category": "electronics",
  "inStock": true
}

Pagination Headers

When using pagination, api.vision includes helpful headers in the response:

HeaderDescription
X-Total-CountTotal number of resources matching the query
X-Total-PagesTotal number of pages available
X-PageCurrent page number
X-LimitNumber of items per page
LinkNavigation links for next, prev, first, last pages

Error Handling

When an error occurs, api.vision returns an appropriate HTTP status code and a JSON object with error details:

{
  "status": 404,
  "error": "Not Found",
  "message": "Resource with id '123' not found",
  "path": "/users/123"
}

Advanced Example

Here's a comprehensive example showing different query capabilities combined:

Request:

GET /posts?author.role=admin&status=published&tags_contains=featured&sort=-createdAt&expand=author&fields=id,title,createdAt,author.username&page=1&limit=5

Response:

[
  {
    "id": 42,
    "title": "Advanced API Design Patterns",
    "createdAt": "2023-04-15T14:32:10Z",
    "author": {
      "username": "techguru"
    }
  },
  {
    "id": 39,
    "title": "The Future of Web Development",
    "createdAt": "2023-04-12T09:45:22Z",
    "author": {
      "username": "webwizard"
    }
  },
  {
    "id": 31,
    "title": "Mastering Microservices",
    "createdAt": "2023-04-05T16:20:45Z",
    "author": {
      "username": "techguru"
    }
  }
]

Next Steps

To learn more about each query capability in detail, check out the specific guides:

Tip: Testing in the Dashboard

api.vision's web dashboard includes a built-in API explorer where you can test queries without writing code. It provides a visual interface for building and testing queries, making it easy to see the results instantly.