Database-Backed Operations
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
-
Open the API in the editor. The Database section lists every operation as Upstream or Database · data source.
-
Click Serve from a database on an operation, and fill in:
Field Meaning Data source The data sources this API's space may use and you can read. Answer A list of rows, or One row (404 if none). See What Callers Get. Max rows The most rows one call returns, up to the data source's row cap. SQL (one SELECT) The query, with a :nameplaceholder for every value from the request. -
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:Column Meaning From path,queryorbody(a field of the JSON body, such asfilter.country).Request name The name in the request, if it differs from the placeholder. Type string,integer,numberorboolean. The request's value is converted to it.Required A call without it gets 400 request_invalid.Default Used when the request leaves the value out. With no default, an optional value is NULL.Min / max For numbers: a value outside the range gets 400 request_invalid.Sample A value for Preview only. It isn't saved. -
Preview runs the query with the sample values and shows up to 5 rows. Nothing is saved.
-
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_developermay change it while the API is a draft or rejected; - once the API is published or in review, only a
lifecycle_manager,space_adminoradminmay.
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
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 isNULL, unless the parameter has a default. - Page with integer parameters, such as
limit(with a default and a max) andoffset. - 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 foruuid,dateand 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 auuidordatecolumn 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_objectorjson_agg. - Name tables as the data source lists them. With no default schema set, PostgreSQL tables can be written as
customersorpublic.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
| Answer | Response |
|---|---|
| A list of rows | 200 {"items": [ … ], "count": 3}, plus "truncated": true when there were more rows than Max rows. |
| One row | 200 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
| Status | error | When |
|---|---|---|
400 | request_invalid | A required value is missing, or a value can't be converted to its type or is out of range. |
502 | datasource_unavailable | The data source no longer exists, is disabled, or no longer allows the API's space. |
502 | datasource_error | The database refused or failed the query. |
504 | datasource_timeout | The query ran longer than the data source's timeout. |
502 | no_backend | The 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:
- lists the data sources it can use there, and reads the tables' real columns;
- creates a draft with
GEToperations and response schemas from those columns; - saves a query for each operation, with placeholders, and previews it;
- 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
| Method | Path | Purpose |
|---|---|---|
GET | /api/v1/api-definitions/{id}/datasource-bindings | The 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/preview | Run a query with sample values: up to 5 rows, nothing saved. |
GET | /api/v1/api-definitions/{id}/datasources | The data sources this API may use. |
GET | /api/v1/api-definitions/{id}/datasources/{name}/tables | Their 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"}.