Prefer using APIs?
You can manage your ClickHouse connections using Connection API.
Query your logs & metrics outside of the Better Stack dashboard with our read-only HTTP API.
You can connect from Grafana, the ClickHouse client via HTTP or any other HTTP client.
Create a username and password by navigating to AI SRE → MCP and API in the side menu.
Click Connect ClickHouse HTTP client.
Follow the instructions in form and click Create connection.
Copy the password shown in the flash message and store it securely. You won't be able to access the password again.
You can manage your ClickHouse connections using Connection API.
Fetch your data using a simple curl command:
Replace $USERNAME:$PASSWORD with your connection credentials and t123456_your_source depending on your available sources, which you can find in Connections:
The Query API provides access to three types of telemetry data for each source: logs, spans, and metrics. This data is split between hot storage for recent data and cold storage for historical data.
remote(t123456_your_source_logs) for fast access to recent logs.
For historical data, use s3Cluster(primary, t123456_your_source_s3). Filter with WHERE _row_type = 1.
Use UNION ALL to combine recent and historical data for complete results.
remote(t123456_your_source_spans) for fast access to recent spans.
For historical data, use s3Cluster(primary, t123456_your_source_s3). Filter with WHERE _row_type = 3.
remote(t123456_your_source_metrics) provides aggregated metrics data, similar to Dashboards.
Replace t123456_your_source depending on your available sources, which you can find in Connections.
Filter logs containing specific text or fields:
Access nested JSON fields in your logs:
For large result sets, paginate using time-based ordering:
To help run extremely fast S3 queries over very wide time ranges, you can apply sampling, which uses a percentage of the stored S3 files as opposed to all of them; this is great if you want results quickly, and are comfortable with an estimated result.
If you encounter MEMORY_LIMIT_EXCEEDED errors:
dt, source, or other fields early in your query.LIMIT or max_result_rows settings.To avoid hitting the concurrent query limit:
The Query API supports various ClickHouse output formats, most notably:
JSON - A single JSON data structure.JSONEachRow - One JSON object per line, best for programmatic access.Pretty - Human-readable table format.CSV - Comma-separated values.TSV - Tab-separated values.Specify the format at the end of your SQL query: FORMAT JSONEachRow
Missing the brackets around your data when using JSONEachRow?
You can use SETTINGS output_format_json_array_of_rows = 1 in front of FORMAT JSONEachRow to make sure your data is wrapped by [ and ], if you need valid JSON output.
Learn more about using JSON in ClickHouse.
ORDER BY dt DESC or ORDER BY dt ASC for consistent pagination.WHERE conditions to reduce data processing.With Standard querying, you can run up to 4 concurrent queries for logs and 20 concurrent queries for metrics.
You can choose a higher Query speed, which is applied to the data region for both your Telemetry and Warehouse sources.
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.