Skip to main content

Database-Backed Operations

Serve an operation straight from a database. Each operation runs one read-only SELECT, and the request's values are bound as parameters, so a caller can never change the SQL. You don't need an upstream service.

Each operation of an API is served from one of two places:

  • Upstream: the API's base URL, through the gateway. This is the default.
  • Database: one of your data sources, with a query saved for that operation.

You choose per operation, so one API can mix both. An API served entirely from databases needs no base URL.


Before You Start​

  • An administrator allows the API's space. In Admin › Agent Catalogs › the catalog › Data sources, edit the data source and tick the space under API Builder spaces. Only APIs in a ticked space can serve the data source. When such an API is called, no user is involved: the ticked space is the grant.
  • You can read the data source yourself. A builder needs their own read grant on the data source (under Who may use it), so nobody can build an API over data they couldn't read themselves.
  • The data source's own rules still apply. Allowed tables, hidden columns, the row cap, the timeout and read-only queries all work as described in What the Checks Refuse.

Serve an Operation from a Database​

  1. Open the API in the editor. The Database section lists every operation as Upstream or Database · data source.

  2. Click Serve from a database on an operation, and fill in:

    FieldMeaning
    Data sourceThe data sources this API's space may use and you can read.
    AnswerA list of rows, or One row (404 if none). See What Callers Get.
    Max rowsThe most rows one call returns, up to the data source's row cap.
    SQL (one SELECT)The query, with a :name placeholder for every value from the request.
  3. When you leave the SQL box, a row is added to the parameters table for each new :name. For each one, say where its value comes from:

    ColumnMeaning
    Frompath, query or body (a field of the JSON body, such as filter.country).
    Request nameThe name in the request, if it differs from the placeholder.
    Typestring, integer, number or boolean. The request's value is converted to it.
    RequiredA call without it gets 400 request_invalid.
    DefaultUsed when the request leaves the value out. With no default, an optional value is NULL.
    Min / maxFor numbers: a value outside the range gets 400 request_invalid.
    SampleA value for Preview only. It isn't saved.
  4. Preview runs the query with the sample values and shows up to 5 rows. Nothing is saved.

  5. Save. Edit query changes the query later, and Remove sends the operation to the upstream again.

Who may change a query follows the same rule as for message mappers:

  • an api_developer may change it while the API is a draft or rejected;
  • once the API is published or in review, only a lifecycle_manager, space_admin or admin may.

The save also warns about a parameter the operation's spec doesn't declare, since a caller can't know to send it.


Writing the Query​

sql
SELECT id, name, country
FROM customers
WHERE (:country IS NULL OR country = :country)
ORDER BY id
LIMIT :limit OFFSET :offset
  • Put a placeholder where each value goes. Never write a request's value into the SQL text. The database driver sends the values separately, so a value such as ' OR 1=1 -- stays a plain string.
  • Make a filter optional with (:country IS NULL OR country = :country). A value the request leaves out is NULL, unless the parameter has a default.
  • Page with integer parameters, such as limit (with a default and a max) and offset.
  • Cast with CAST(:d AS date), not :d::date. A :: cast right after a placeholder is refused.
  • Compare non-text columns carefully. A placeholder compared directly with a column, such as id = :id, takes the column's type, so it works for uuid, date and enum columns too. A placeholder the database can't type by itself is sent with its declared Type: one in :x IS NULL, a function argument, or the select list. On PostgreSQL, an optional filter on a uuid or date column therefore needs the cast in both places: (CAST(:id AS uuid) IS NULL OR id = CAST(:id AS uuid)).
  • Select plain columns, one row per record. The gateway builds the JSON itself: don't build it in SQL with json_build_object or json_agg.
  • Name tables as the data source lists them. With no default schema set, PostgreSQL tables can be written as customers or public.customers.

Every placeholder must be a declared parameter, and the SQL must pass the data source's checks. Otherwise the query isn't saved, and the editor says why.


What Callers Get​

AnswerResponse
A list of rows200 {"items": [ … ], "count": 3}, plus "truncated": true when there were more rows than Max rows.
One row200 with the first row itself, or 404 {"error": "not_found", "message": "No matching record."} when there is none.

Hidden columns are never returned. A query that names one is refused when it is saved, and SELECT * returns them as ***.

The response then goes through the same steps as an upstream's response: the response message mapper, response validation and masking. The GET cache and the API's calls at once limit apply too. Retries and the circuit breaker don't apply, because they are for the upstream.

Errors​

StatuserrorWhen
400request_invalidA required value is missing, or a value can't be converted to its type or is out of range.
502datasource_unavailableThe data source no longer exists, is disabled, or no longer allows the API's space.
502datasource_errorThe database refused or failed the query.
504datasource_timeoutThe query ran longer than the data source's timeout.
502no_backendThe operation has no query and the API has no base URL.

Callers get only a generic message. The detail (the SQL rule, the table, the database's error with credentials and addresses removed) goes to the call log and to the editor's test console. The analytics count the datasource_* codes as upstream errors and no_backend as a gateway error.


Checks Before Review and Publishing​

Submitting an API for review, and publishing it, check that:

  • every operation has a backend: an upstream base URL, or a query;
  • every query's data source exists, is enabled and allows the API's space;
  • every query still passes the data source's checks.

A problem stops the submit or publish, and the message names the operation.


Versions, Export and Logs​

  • Versions. The queries are part of each version, like mappers. An environment runs the queries saved with the version it serves, and a rollback restores them.
  • Export and import. An exported API carries its queries. Importing one warns that its operations are served from data sources: the target platform needs data sources with the same names that allow the target space.
  • Logs. The gateway's call log records every call. The data source's query log records the refusals and errors of API calls, but not their successful queries, so busy APIs don't fill it. Previews from the editor and the assistant are in the query log as usual.

With the AI Assistant​

Describe the API to the AI assistant, for example:

Build a REST API named customers in the Sales space, served from the shop database: list customers with an optional country filter and paging, and get one customer by id. Then test both operations.

The assistant:

  1. lists the data sources it can use there, and reads the tables' real columns;
  2. creates a draft with GET operations and response schemas from those columns;
  3. saves a query for each operation, with placeholders, and previews it;
  4. tests every operation, including a not-found case.

Data sources are read-only, so it refuses an operation that inserts, updates or deletes rows and says why. It works with your own access, so it sees only the data sources you can read.


API​

MethodPathPurpose
GET/api/v1/api-definitions/{id}/datasource-bindingsThe API's queries, one per operation.
PUT/api/v1/api-definitions/{id}/datasource-bindings/{operation_id}Save an operation's query: datasource, sql, params, result (list or object), max_rows, enabled. The response includes any warnings.
DELETE/api/v1/api-definitions/{id}/datasource-bindings/{operation_id}The operation goes to the upstream again.
POST/api/v1/api-definitions/{id}/datasource-bindings/previewRun a query with sample values: up to 5 rows, nothing saved.
GET/api/v1/api-definitions/{id}/datasourcesThe data sources this API may use.
GET/api/v1/api-definitions/{id}/datasources/{name}/tablesTheir tables.
GET/api/v1/api-definitions/{id}/datasources/{name}/describe?table=A table's columns, with hidden ones marked.

Each item of params is {"name", "in": "path" | "query" | "body", "field", "type", "required", "default", "min", "max"}.

Hrida.ai is proprietary software of Zlabs Innovation. See the license for terms. © 2026 Zlabs Innovation.