SQL REST API
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
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:
// SQL REST API example
/api/sql/query?branch=dev&context=marketingbranch [optional]
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]
context [optional] Indicate which context to use. Allows consuming semantic definitions from different contexts.
If not specified, Lynk will take the default context
Authentication
To authenticate with the SQL REST API, you need to create and use an API token.
The API token should be sent in the x-api-key header of the request.
Request body - simple example
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:
Request body parameters
entity
entityDefine the main entity of the query
joins
joinsEntities to be joined to the main entity
The properties for joins are:
entity
entityThe entity to join
join name [optional]
join name [optional]Specify the join name. If not specified, Lynk will use the default join between the main entity and the entity in this joins clause
alias
alias Give an alias to the joined entity
timeAggregation
timeAggregationThe time aggregation definition
The properties for timeAggregation are:
timeGrain
timeGrainThe time grain to aggregate by (day / week / month etc..) For example: "day", for 7 days backward
windowSize [optional]
windowSize [optional]The window size to apply (numeric value), in case of time window aggregation. For example: "7 ", for 7 days backward
direction [optional]
direction [optional]The direction to apply (backward / forward), in case of time window aggregation. For example: "backward", for 7 days backward.
select
selectThe fields to select
There are three types of fields you can select in the select clause:
fieldmeasurelynk_function
The properties for select are:
field [select]
field [select]Use this option for fetching entity features
measure [select]
measure [select]Use this option for fetching measures from the main entity.
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]
lynk_function [select]Use this option to apply a Lynk Function and fetching it's result. For more information about this, visit Lynk Functions.
where
whereApply filters to the query
There are two types of filters you can apply in the where clause:
fieldssql
where - fields
where - fieldsIn 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 tofield- the entity feature to filter onoperator- the filter operator (see all options for filter operators)values- an array of field values to filter by
The above example translates to the following SQL WHERE clause:
where - sql
where - sqlApply a simple sql expression to use for filtering
groupBy
groupByArray of fields to use in the group by clause
having
havingApply filters to the query using aggregated fields
There are two types of filters you can apply in the having clause:
fieldssql
having - fields
having - fieldsIn 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 tomeasure- the entity measure to filter onoperator- the filter operator (see all options for filter operators)values- an array of field values to filter by
The above example translates to the following SQL HAVING clause:
having - sql
having - sqlApply a simple sql expression to use for filtering
orderBy
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 fromfield- the entity field to sort bydirection- “desc” or “asc”. (default “asc”)
limit
limit The number of rows to return
offset
offset The number of rows to skip
Last updated