Execute a SQL query
Runs a SQL query against a connection in your project and returns the result rows as JSON.
The connection identified by connectionId must belong to the same
project as the access token. Any security policies assigned to the
token (row-level, column-level, and schema-level security)
are applied automatically before the result is returned.
Python post-processing
If you include pythonCode, execution follows this order:
| Stage | What happens |
|---|---|
| 1 | Semaphor runs the SQL query against the connection |
| 2 | Security policies and row limits are applied to the result |
| 3 | The filtered rows are passed into a server-side Python environment as data |
| 4 | If your code sets a variable called result, that value is returned; otherwise the original rows are returned |
Inside the Python environment:
datais a list of dictionaries (one per row)- Anything printed to stdout is captured and returned in the
outputfield - Assign your transformed rows to
resultto replace the default response
Example:
import pandas as pd
df = pd.DataFrame(data)
result = df.to_dict("records")Authorization
BearerAuth A project-scoped access token. Generate one via the Token API.
In: header
Request Body
application/json
TypeScript Definitions
Use the request body type in TypeScript.
Response Body
application/json
application/json
application/json
application/json
application/json
curl -X post "https://semaphor.cloud/api/v1/sql" \ -H "Content-Type: application/json" \ -d '{ "connectionId": "conn_sales", "sql": "select\n order_id,\n amount,\n created_at\nfrom orders\nwhere status = \'paid\'\n" }'{
"records": [
{
"order_id": 1,
"amount": 12.3
},
{
"order_id": 2,
"amount": 44.1
}
],
"rowLimitExceeded": false
}{
"error": "string",
"requestId": "string",
"details": "string",
"warnings": [
"string"
]
}{
"error": "string",
"requestId": "string",
"details": "string",
"warnings": [
"string"
]
}{
"error": "string",
"requestId": "string",
"details": "string",
"warnings": [
"string"
]
}{
"error": "string",
"requestId": "string",
"details": "string",
"warnings": [
"string"
]
}