SQL REST API
Last updated
Last updated
Querying the SQL REST API is as simple as sending a json format with the parameters you would use in a regular SQL query.
For AI agents, it is recommended to use the SQL REST API. SQL REST API is easier for agents to understand and generate.
Connecting to Lynk SQL REST API is done via a regular HTTP POST request to the following endpoint:
/api/sql/query?branch=&context=
For example:
branch
[optional]Indicate which git branch to use. Allows exploring semantic definitions while in development.
If not specified, Lynk will take the definitions of the branch which is defined as the main branch.
context
[optional] Indicate which context to use. Allows consuming semantic definitions from different .
If not specified, Lynk will take the default
context
In the example below you can find a simple Request Body for SQL REST API.
In this example, we request for top 100 the customers (customer_id) and their total_sales, for customers from BRAZIL, ordered by total_sales.
The above simple SQL REST API request simple example is equivalent to the following SQL API request:
entity
Define the main entity of the query
joins
Entities to be joined to the main entity
The properties for joins
are:
entity
The entity to join
join name [optional]
alias
Give an alias to the joined entity
timeAggregation
The time aggregation definition
The properties for timeAggregation
are:
timeGrain
windowSize
[optional]direction
[optional]The direction to apply (backward / forward), in case of time window aggregation. For example: "backward", for 7 days backward.
select
The fields to select
There are three types of fields you can select in the select
clause:
field
measure
lynk_function
The properties for select
are:
field
[select
]Use this option for fetching entity features
measure
[select
]Make sure to select measures of the main entity (not from joined entities). Lynk joins all the joined entities to the main entity, and aggregates to the main entity level (or main entity + time_agg definition, if specified)
lynk_function
[select
]where
Apply filters to the query
There are two types of filters you can apply in the where
clause:
fields
sql
where
- fields
In case of applying where
filters as fields
, you will need to add the following parameters:
entity
- the entity which the field to filter on belongs to
field
- the entity feature to filter on
values
- an array of field values to filter by
The above example translates to the following SQL WHERE
clause:
where
- sql
Apply a simple sql expression to use for filtering
groupBy
Array of fields to use in the group by
clause
having
Apply filters to the query using aggregated fields
There are two types of filters you can apply in the having
clause:
fields
sql
having
- fields
In case of applying having
filters as fields
, you will need to add the following parameters:
entity
- the entity which the field to filter on belongs to
measure
- the entity measure to filter on
values
- an array of field values to filter by
The above example translates to the following SQL HAVING
clause:
having
- sql
Apply a simple sql expression to use for filtering
orderBy
Array of items to sort by (order by):
In case of applying orderBy
option, you will need to add the following parameters:
entity
- the entity to take the field to sort by from
field
- the entity field to sort by
direction
- “desc” or “asc”. (default “asc”)
limit
The number of rows to return
offset
The number of rows to skip
To authenticate with the SQL REST API, you need to create and use an .
The API token should be sent in the x-api-key
header of the request.
Specify the . If not specified, Lynk will use the default join between the main entity and the entity in this joins clause
The to aggregate by (day / week / month etc..) For example: "day", for 7 days backward
The to apply (numeric value), in case of time window aggregation. For example: "7 ", for 7 days backward
For more information about time aggregations, visit .
Use this option for fetching from the main entity.
Use this option to apply a Lynk Function and fetching it's result. For more information about this, visit .
operator
- the filter operator (see all options for )
operator
- the filter operator (see all options for )