Pre-requisites
Before proceeding, ensure you have:- An API key with appropriate permissions
- An Admin Console Role
Minimum permissionsTo query an API endpoint, the API key needs
Member organization role with Query Endpoints service access. The database role is configured when you create the endpoint.Create a saved query
If you have a saved query, you can skip this step.Open a new query tab. For demonstration purposes, we’ll use the youtube dataset, which contains approximately 4.5 billion records. Follow the steps in section “Create table” to create the table on your Cloud service and insert data to it.As an example query, we’ll return the top 10 uploaders by average views per video in a user-inputtedyear parameter.year) which is highlighted in the snippet above.
You can specify query parameters using { } together with the type of the parameter.
The SQL console query editor automatically detects ClickHouse query parameter expressions and provides an input for each parameter.Let’s quickly run this query to make sure that it works by specifying the year 2010 in the query variables input box on the right side of the SQL editor:Next, save the query:More documentation around saved queries can be found in section “Saving a query”.Configuring the query API endpoint
Query API endpoints can be configured directly from query view by clicking the Share button and selectingAPI Endpoint.
You’ll be prompted to specify which API keys should be able to access the endpoint:After selecting an API key, you will be asked to:- Select the Database role that will be used to run the query (
Full access,Read onlyorCreate a custom role) - Specify cross-origin resource sharing (CORS) allowed domains
curl command will be displayed so you can send a test request:The curl command displayed in the interface is given below for convenience:Query API parameters
Query parameters in a query can be specified with the syntax{parameter_name: type}. These parameters will be automatically detected and the example request payload will contain a queryVariables object through which you can pass these parameters.Testing and monitoring
Once a Query API endpoint is created, you can test that it works by usingcurl or any other HTTP client:After you’ve sent your first request, a new button should appear immediately to the right of the Share button. Clicking it will open a flyout containing monitoring data about the query:Implementation details
This endpoint executes queries on your saved Query API endpoints. It supports multiple versions, flexible response formats, parameterized queries, and optional streaming responses (version 2 only). Endpoint:HTTP methods
| Method | Use Case | Parameters |
|---|---|---|
| GET | Simple queries with parameters | Pass query variables via URL parameters (?param_name=value) |
| POST | Complex queries or when using request body | Pass query variables in request body (queryVariables object) |
- Simple queries without complex nested data
- Parameters can be easily URL-encoded
- Caching benefits from HTTP GET semantics
- Complex query variables (arrays, objects, large strings)
- When request body is preferred for security/privacy
- Streaming file uploads or large data
Authentication
Required: YesMethod: Basic Auth using OpenAPI Key/Secret
Permissions: Appropriate permissions for the query endpoint
Request configuration
URL parameters
| Parameter | Required | Description |
|---|---|---|
queryEndpointId | Yes | The unique identifier of the query endpoint to run |
Query parameters
| Parameter | Required | Description | Example |
|---|---|---|---|
format | No | Response format (supports all ClickHouse formats) | ?format=JSONEachRow |
param_:name | No | Query variables when request body is a stream. Replace :name with your variable name | ?param_year=2024 |
request_timeout | No | Query timeout in milliseconds (default: 30000) | ?request_timeout=60000 |
:clickhouse_setting | No | Any supported ClickHouse setting | ?max_threads=8 |
Headers
| Header | Required | Description | Values |
|---|---|---|---|
x-clickhouse-endpoint-version | No | Specifies the endpoint version | 1 or 2 (defaults to last saved version) |
x-clickhouse-endpoint-upgrade | No | Triggers endpoint version upgrade (use with version header) | 1 to upgrade |
Request body
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
queryVariables | object | No | Variables to be used in the query |
format | string | No | Response format |
Supported formats
| Version | Supported Formats |
|---|---|
| Version 2 | All ClickHouse-supported formats |
| Version 1 (limited) | TabSeparated TabSeparatedWithNames TabSeparatedWithNamesAndTypes JSON JSONEachRow CSV CSVWithNames CSVWithNamesAndTypes |
Responses
Success
Status:200 OKThe query was successfully executed.
Error codes
| Status Code | Description |
|---|---|
400 Bad Request | The request was malformed |
401 Unauthorized | Missing authentication or insufficient permissions |
404 Not Found | The specified query endpoint wasn’t found |
Error handling best practices
- Ensure valid authentication credentials are included in the request
- Validate the
queryEndpointIdandqueryVariablesbefore sending - Implement graceful error handling with appropriate error messages
Upgrading endpoint versions
To upgrade from version 1 to version 2:- Include the
x-clickhouse-endpoint-upgradeheader set to1 - Include the
x-clickhouse-endpoint-versionheader set to2
- Support for all ClickHouse formats
- Response streaming capabilities
- Enhanced performance and functionality
Examples
Basic request
Query API Endpoint SQL:Version 1
- cURL
- JavaScript
Version 2
- GET (cURL)
- POST (cURL)
- JavaScript
Response
Request with query variables and version 2 on JSONCompactEachRow format
Query API Endpoint SQL:- GET (cURL)
- POST (cURL)
- JavaScript
Response
Request with array in the query variables that inserts data into a table
Table SQL:- cURL
- JavaScript
Request with ClickHouse settings max_threads set to 8
Query API Endpoint SQL:
- GET (cURL)
- POST (cURL)
- JavaScript
Request and parse the response as a stream
Query API Endpoint SQL:- TypeScript
Output
Insert a stream from a file into a table
Create a file./samples/my_first_table_2024-07-11.csv with the following content: