Skip to content
DocumentationLakehouse
Lakehouse
Explore and query
Query from an application

Query from an application

Run SQL over HTTPS with an Altertable SDK or HTTP client. You need lakehouse credentials for the environment that owns your catalogs.

For terminal queries, use the CLI. For all request fields, response formats, and errors, use the HTTP API reference.

Set up with your coding agent

Agent prompt
Add an Altertable query to this application. Read https://altertable.ai/docs/lakehouse/query/http-api.md and the linked SDK or HTTP reference first. Inspect the application runtime and existing data-access code. Ask what result the application needs and confirm the target environment. Choose an SDK supported by this runtime, or use the HTTP API. Keep lakehouse credentials on the server in the project's secret configuration. Verify authentication with SELECT 1 AS connected, then inspect the real catalog and table schemas. Use a bounded query and bind parameters for user-supplied values. Integrate result handling and errors using the application's existing patterns. Handle the API's streamed response correctly. Run the relevant checks and verify a real result without logging credentials or sensitive rows. Summarize the code changes and required configuration.

1. Verify authentication

Set your credentials locally, then send a query that needs no existing table:

Terminal
export ALTERTABLE_USERNAME='YOUR_LAKEHOUSE_USERNAME'
export ALTERTABLE_PASSWORD='YOUR_LAKEHOUSE_PASSWORD'
curl https://api.altertable.ai/query \
--user "$ALTERTABLE_USERNAME:$ALTERTABLE_PASSWORD" \
-H 'Content-Type: application/json' \
-d '{"statement":"SELECT 1 AS connected"}'

Confirm that the response contains a connected column and a row with 1. If authentication fails, check the credentials and target environment before querying application data.

2. Query your data

See lakehouse SDK installation and availability before choosing a library. Rust and PHP are not yet published; use HTTP from those runtimes.

The cURL example below reuses ALTERTABLE_USERNAME and ALTERTABLE_PASSWORD from step 1. Replace the example catalog and table with names from your environment. The SDK examples show client initialization and result handling:

cURL

Terminal
curl -X POST https://api.altertable.ai/query \
--user "$ALTERTABLE_USERNAME:$ALTERTABLE_PASSWORD" \
-H 'Content-Type: application/json' \
-d '{"statement": "SELECT * FROM my_catalog.main.events ORDER BY timestamp DESC LIMIT 100"}'

SDKs

import { AltertableLakehouseClient } from '@altertable/lakehouse';
​
const client = new AltertableLakehouseClient({
username: 'YOUR_LAKEHOUSE_USERNAME',
password: 'YOUR_LAKEHOUSE_PASSWORD',
});
​
const result = await client.queryAll({
statement: `
SELECT
date_trunc('day', timestamp) AS day,
event,
COUNT(*) AS events,
ROUND(AVG(value), 2) AS avg_value
FROM my_catalog.main.events
WHERE timestamp >= NOW() - INTERVAL '30 days'
GROUP BY 1, 2
ORDER BY day DESC, events DESC
`,
});
​
for (const row of result.rows) {
console.log(row);
}

Use fully qualified table names and bound exploratory queries with LIMIT. In application code, store credentials in your runtime's secret configuration.

Use bind parameters for values supplied by users. Named parameters use $name; positional parameters use $1, $2, and so on. Send an object or array in params rather than interpolating values into SQL:

JSON
{
"statement": "SELECT * FROM my_catalog.main.users WHERE id = $id",
"params": { "id": 123 }
}

3. Consume results

The default HTTP response is a JSONL stream containing query metadata, a column-name array, and result rows. Process it line by line rather than parsing the whole response as one JSON document. An SDK can handle the stream for you.

If your consumer expects a standard format, request CSV, row-oriented JSONL, or Parquet. The response format reference defines the exact envelopes and options.

Tune execution when needed

Troubleshooting

  • Unauthorized request: use lakehouse credentials from the intended environment.
  • Missing table: qualify the table with its catalog and schema, then check catalog permissions.
  • Unexpected response shape: check whether you requested the default envelope or a standard output format.
  • No available Worker: inspect compute availability and retry with backoff. See compute sizing.