> For clean Markdown of any page, append `.md` to the page URL. > For a complete documentation index, see https://docs.sarvam.ai/llms.txt. > For full documentation content in one file, see https://docs.sarvam.ai/llms-full.txt. > For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.sarvam.ai/_mcp/server. # Preview query POST https://apps.sarvam.ai/api/analytics/v1/{org_id}/{workspace_id}/query-preview Content-Type: application/json Executes read-only SQL against the workspace's analytics views and returns the rows. Use it to develop a query before creating a widget from it. Safe and side-effect free despite being a POST: the SQL and filter values are too large and too structured for a query string. Tenant scoping is enforced server-side and cannot be bypassed. Reference: https://docs.sarvam.ai/api-reference/boards/query/preview ## Request ### Path parameters - `org_id` (string, required) — Organization ID - `workspace_id` (string, required) — Workspace ID ### Headers - `X-API-Key` (string, required) — Your workspace API key. ### Body (application/json) This endpoint expects a PreviewQueryRequest. - `sql` (string, required) — Read-only SELECT; same filter-token syntax as widget SQL - `filter_values` (map from string to FilterValueInput, optional) — Values for the tokens in `sql`, keyed by token name - `row_limit` (integer, optional, default: 1000) — Maximum rows returned. Optional — omit it for the default of 1000. A `LIMIT` inside your SQL can narrow the result further but never widens it past this. The result reports `truncated` when this cut the result short. ## Response ### 200 Successful Response - `columns` (list of QueryColumn, required) — Column metadata in result order; position N describes position N of every row - `rows` (list of list of any, required) — Row values as positional arrays aligned 1:1 with `columns`, not keyed objects - `row_count` (integer, required) — Number of rows in `rows`, which is not the total matched when `truncated` - `truncated` (boolean, required) — True when the result was cut short by `row_limit` - `execution_ms` (integer, required) — Query execution time in milliseconds, excluding network transfer ## Errors ### 400 Bad Request Error Invalid SQL, unresolved filter token, or bad filter value - `error` (ErrorBody, required) — Details of the error that occurred. ### 401 Unauthorized Error Missing or invalid credentials - `error` (ErrorBody, required) — Details of the error that occurred. ### 403 Forbidden Error Credentials are not scoped to this org and workspace - `error` (ErrorBody, required) — Details of the error that occurred. ### 408 Request Timeout Error The analytics query exceeded its time budget - `error` (ErrorBody, required) — Details of the error that occurred. ### 422 Unprocessable Entity Error The analytics database rejected the query - `error` (ErrorBody, required) — Details of the error that occurred. ### 429 Too Many Requests Error Rate limit exceeded. Honour the `Retry-After` response header before retrying. - `error` (ErrorBody, required) — Details of the error that occurred. ### 500 Internal Server Error Unexpected server error - `error` (ErrorBody, required) — Details of the error that occurred. ## Types ### FilterValueInput A filter value supplied at run time. Send the filter's declared type alongside the raw string value; Sarvam converts it before binding (a `date`, for example, is parsed and converted to UTC). - `filter_type` (enum, required) - Allowed values: `number`, `string`, `date`, `dropdown` - `value` (string, required) ### QueryColumn - `name` (string, required) — Column name as returned by the query - `type` (string, required) — ClickHouse type name, e.g. `UInt64`, `String`, `DateTime` ### ErrorBody Details of the error that occurred. - `code` (string, required) — A service-specific error code. - `message` (string, required) — A human-readable description of the error. ## Examples **Request** ```json { "sql": "SELECT language_name, count() AS calls\nFROM EngagementFactsBoardsView\nWHERE effective_datetime >= {{start_date}}\nGROUP BY language_name\nORDER BY calls DESC", "filter_values": { "start_date": { "filter_type": "date", "value": "2026-09-01" } }, "row_limit": 100 } ``` **Response** ```json { "columns": [ { "name": "language_name", "type": "LowCardinality(String)" }, { "name": "calls", "type": "UInt64" } ], "rows": [ [ "Hindi", 1842 ], [ "English", 1109 ], [ "Tamil", 402 ] ], "row_count": 3, "truncated": false, "execution_ms": 96 } ``` **SDK Code** ```python Calls by language import requests url = "https://apps.sarvam.ai/api/analytics/v1/org_id/workspace_id/query-preview" payload = { "sql": "SELECT language_name, count() AS calls FROM EngagementFactsBoardsView WHERE effective_datetime >= {{start_date}} GROUP BY language_name ORDER BY calls DESC", "filter_values": { "start_date": { "filter_type": "date", "value": "2026-09-01" } }, "row_limit": 100 } headers = { "X-API-Key": "", "Content-Type": "application/json" } response = requests.post(url, json=payload, headers=headers) print(response.json()) ``` ```javascript Calls by language const url = 'https://apps.sarvam.ai/api/analytics/v1/org_id/workspace_id/query-preview'; const options = { method: 'POST', headers: {'X-API-Key': '', 'Content-Type': 'application/json'}, body: '{"sql":"SELECT language_name, count() AS calls\nFROM EngagementFactsBoardsView\nWHERE effective_datetime >= {{start_date}}\nGROUP BY language_name\nORDER BY calls DESC","filter_values":{"start_date":{"filter_type":"date","value":"2026-09-01"}},"row_limit":100}' }; try { const response = await fetch(url, options); const data = await response.json(); console.log(data); } catch (error) { console.error(error); } ``` ```go Calls by language package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://apps.sarvam.ai/api/analytics/v1/org_id/workspace_id/query-preview" payload := strings.NewReader("{\n \"sql\": \"SELECT language_name, count() AS calls\\nFROM EngagementFactsBoardsView\\nWHERE effective_datetime >= {{start_date}}\\nGROUP BY language_name\\nORDER BY calls DESC\",\n \"filter_values\": {\n \"start_date\": {\n \"filter_type\": \"date\",\n \"value\": \"2026-09-01\"\n }\n },\n \"row_limit\": 100\n}") req, _ := http.NewRequest("POST", url, payload) req.Header.Add("X-API-Key", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` ```ruby Calls by language require 'uri' require 'net/http' url = URI("https://apps.sarvam.ai/api/analytics/v1/org_id/workspace_id/query-preview") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["X-API-Key"] = '' request["Content-Type"] = 'application/json' request.body = "{\n \"sql\": \"SELECT language_name, count() AS calls\\nFROM EngagementFactsBoardsView\\nWHERE effective_datetime >= {{start_date}}\\nGROUP BY language_name\\nORDER BY calls DESC\",\n \"filter_values\": {\n \"start_date\": {\n \"filter_type\": \"date\",\n \"value\": \"2026-09-01\"\n }\n },\n \"row_limit\": 100\n}" response = http.request(request) puts response.read_body ``` ```java Calls by language import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; HttpResponse response = Unirest.post("https://apps.sarvam.ai/api/analytics/v1/org_id/workspace_id/query-preview") .header("X-API-Key", "") .header("Content-Type", "application/json") .body("{\n \"sql\": \"SELECT language_name, count() AS calls\\nFROM EngagementFactsBoardsView\\nWHERE effective_datetime >= {{start_date}}\\nGROUP BY language_name\\nORDER BY calls DESC\",\n \"filter_values\": {\n \"start_date\": {\n \"filter_type\": \"date\",\n \"value\": \"2026-09-01\"\n }\n },\n \"row_limit\": 100\n}") .asString(); ``` ```php Calls by language request('POST', 'https://apps.sarvam.ai/api/analytics/v1/org_id/workspace_id/query-preview', [ 'body' => '{ "sql": "SELECT language_name, count() AS calls\\nFROM EngagementFactsBoardsView\\nWHERE effective_datetime >= {{start_date}}\\nGROUP BY language_name\\nORDER BY calls DESC", "filter_values": { "start_date": { "filter_type": "date", "value": "2026-09-01" } }, "row_limit": 100 }', 'headers' => [ 'Content-Type' => 'application/json', 'X-API-Key' => '', ], ]); echo $response->getBody(); ``` ```csharp Calls by language using RestSharp; var client = new RestClient("https://apps.sarvam.ai/api/analytics/v1/org_id/workspace_id/query-preview"); var request = new RestRequest(Method.POST); request.AddHeader("X-API-Key", ""); request.AddHeader("Content-Type", "application/json"); request.AddParameter("application/json", "{\n \"sql\": \"SELECT language_name, count() AS calls\\nFROM EngagementFactsBoardsView\\nWHERE effective_datetime >= {{start_date}}\\nGROUP BY language_name\\nORDER BY calls DESC\",\n \"filter_values\": {\n \"start_date\": {\n \"filter_type\": \"date\",\n \"value\": \"2026-09-01\"\n }\n },\n \"row_limit\": 100\n}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); ``` ```swift Calls by language import Foundation let headers = [ "X-API-Key": "", "Content-Type": "application/json" ] let parameters = [ "sql": "SELECT language_name, count() AS calls FROM EngagementFactsBoardsView WHERE effective_datetime >= {{start_date}} GROUP BY language_name ORDER BY calls DESC", "filter_values": ["start_date": [ "filter_type": "date", "value": "2026-09-01" ]], "row_limit": 100 ] as [String : Any] let postData = JSONSerialization.data(withJSONObject: parameters, options: []) let request = NSMutableURLRequest(url: NSURL(string: "https://apps.sarvam.ai/api/analytics/v1/org_id/workspace_id/query-preview")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData as Data let session = URLSession.shared let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if (error != nil) { print(error as Any) } else { let httpResponse = response as? HTTPURLResponse print(httpResponse) } }) dataTask.resume() ```