Edit

Share via


Send T-SQL queries via the REST API

Applies to: ✅ Microsoft FabricAzure Data Explorer

This article describes how to use a subset of the T-SQL language to send T-SQL queries via the REST API.

Request structure

To send T-SQL queries to the API, create a POST request with the following components.

To copy your URI, in the Azure portal, go to your cluster's overview page, and then select the URI. https://<your_cluster>.kusto.windows.net/MyDatabase? web=0&query=KustoLogs+%7c+where+Timestamp+>+ago({Period})+%7c+count&Period=1h Replace <your_cluster> with your Azure Data Explorer cluster name.

To copy your URI, see Copy a KQL database URI.

  • Request URL: The URL should be formatted as https://<cluster_uri>/v1/rest/query, where <cluster_uri> is the URI of your cluster or database containing the table you want to query.

  • Headers: Set the following headers. Replace <cluster_uri> and <bearer_token> with your specific cluster or database URI and bearer token values.

    makefile
    Accept:application/json
    Content-Type:application/json; charset=utf-8
    Host:<cluster_uri>
    Authorization: Bearer <bearer_token>
    
  • Body: Set the csl property to the text of your T-SQL query, and the client request property query_language to sql.

    JSON
    {
        "csl": "<t_sql_query>",
        "properties": {
            "Options": {
                "query_language": "sql"
            }
        }
    }
    

Example

The following example shows a request body with a T-SQL query in the csl field and the query_language client request property set to sql.

JSON
{
    "db": "MyDatabase",
    "csl": "SELECT top(10) * FROM MyTable",
    "properties": {
        "Options": {
            "query_language": "sql"
        }
    }
}

The response is in a format similar to the following.

JSON
{
    "Tables": [
        {
            "TableName": "Table_0",
            "Columns": [
                {
                    "ColumnName": "rf_id",
                    "DataType": "String",
                    "ColumnType": "string"
                },
                ...
            ],
            "Rows": [
                [
                    "b9b84d3451b4d3183d0640df455399a9",
                    ...
                ],
                ...
            ]
        }
    ]
}