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 domain to use. Allows consuming semantic definitions from different domains.
If not specified, Lynk will take the default domain
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
aliasGive 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.
For more information about time aggregations, visit Time Aggregations.
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)
Measures are reusable aggregate definitions that can be applied on entity rollup.
Just like regular SQL, when using measures (aggregate functions), make sure to put the rest of the entity features in the GROUP BY clause.
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
Note that you can apply either fields or sql filters, but not both at the same where clause.
(The effect will be the same whichever way you choose to apply as filter)
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
Note that you can apply either fields or sql filters, but not both at the same having clause.
(The effect will be the same whichever way you choose to apply as filter)
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
orderByArray 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
limitThe number of rows to return
offset
offsetThe number of rows to skip
Last updated