ClickHouse JS
The official JS client for connecting to ClickHouse. The client is written in TypeScript and provides typings for the client public API.
It has zero dependencies, is optimized for maximum performance, and is tested with various ClickHouse versions and configurations (on-premise single node, on-premise cluster, and ClickHouse Cloud).
There are two different versions of the client available for different environments:
@clickhouse/client
- Node.js only@clickhouse/client-web
- browsers (Chrome/Firefox), Cloudflare workers
When using TypeScript, make sure it is at least version 4.5, which enables inline import and export syntax.
The client source code is available in the ClickHouse-JS GitHub repository.
Environment requirements (node.js)
Node.js must be available in the environment to run the client. The client is compatible with all the maintained Node.js releases.
As soon as a Node.js version approaches End-Of-Life, the client drops support for it as it is considered outdated and insecure.
Current Node.js versions support:
Node.js version | Supported? |
---|---|
22.x | ✔ |
20.x | ✔ |
18.x | ✔ |
16.x | Best effort |
Environment requirements (web)
The web version of the client is officially tested with the latest Chrome/Firefox browsers and can be used as a dependency in, for example, React/Vue/Angular applications, or Cloudflare workers.
Installation
To install the latest stable Node.js client version, run:
Web version installation:
Compatibility with ClickHouse
Client version | ClickHouse |
---|---|
1.12.0 | 24.8+ |
Likely, the client will work with the older versions, too; however, this is best-effort support and is not guaranteed. If you have a ClickHouse version older than 23.3, please refer to ClickHouse security policy and consider upgrading.
Examples
We aim to cover various scenarios of client usage with the examples in the client repository.
The overview is available in the examples README.
If something is unclear or missing from the examples or from the following documentation, feel free to contact us.
Client API
Most of the examples should be compatible with both Node.js and web versions of the client, unless explicitly stated otherwise.
Creating a client instance
You can create as many client instances as necessary with the createClient
factory:
If your environment doesn't support ESM modules, you can use CJS syntax instead:
A client instance can be pre-configured during instantiation.
Configuration
When creating a client instance, the following connection settings can be adjusted:
Setting | Description | Default Value | See Also |
---|---|---|---|
url?: string | A ClickHouse instance URL. | http://localhost:8123 | URL configuration docs |
pathname?: string | An optional pathname to add to the ClickHouse URL after it is parsed by the client. | '' | Proxy with a pathname docs |
request_timeout?: number | The request timeout in milliseconds. | 30_000 | - |
compression?: { **response**?: boolean; **request**?: boolean } | Enable compression. | - | Compression docs |
username?: string | The name of the user on whose behalf requests are made. | default | - |
password?: string | The user password. | '' | - |
application?: string | The name of the application using the Node.js client. | clickhouse-js | - |
database?: string | The database name to use. | default | - |
clickhouse_settings?: ClickHouseSettings | ClickHouse settings to apply to all requests. | {} | - |
log?: { **LoggerClass**?: Logger, **level**?: ClickHouseLogLevel } | Internal client logs configuration. | - | Logging docs |
session_id?: string | Optional ClickHouse Session ID to send with every request. | - | - |
keep_alive?: { **enabled**?: boolean } | Enabled by default in both Node.js and Web versions. | - | - |
http_headers?: Record<string, string> | Additional HTTP headers for outgoing ClickHouse requests. | - | Reverse proxy with authentication docs |
roles?: string | string[] | ClickHouse role name(s) to attach to the outgoing requests. | - | Using roles with the HTTP interface |
Node.js-specific configuration parameters
Setting | Description | Default Value | See Also |
---|---|---|---|
max_open_connections?: number | A maximum number of connected sockets to allow per host. | 10 | - |
tls?: { **ca_cert**: Buffer, **cert**?: Buffer, **key**?: Buffer } | Configure TLS certificates. | - | TLS docs |
keep_alive?: { **enabled**?: boolean, **idle_socket_ttl**?: number } | - | - | Keep Alive docs |
http_agent?: http.Agent | https.Agent Experimental feature. Learn more. | Custom HTTP agent for the client. | - | HTTP agent docs |
set_basic_auth_header?: boolean Experimental feature. Learn more. | Set the Authorization header with basic auth credentials. | true | this setting usage in the HTTP agent docs |
URL configuration
URL configuration will always overwrite the hardcoded values and a warning will be logged in this case.
It is possible to configure most of the client instance parameters with a URL. The URL format is http[s]://[username:password@]hostname:port[/database][?param1=value1¶m2=value2]
. In almost every case, the name of a particular parameter reflects its path in the config options interface, with a few exceptions. The following parameters are supported:
Parameter | Type |
---|---|
pathname | an arbitrary string. |
application_id | an arbitrary string. |
session_id | an arbitrary string. |
request_timeout | non-negative number. |
max_open_connections | non-negative number, greater than zero. |
compression_request | boolean. See below (1) |
compression_response | boolean. |
log_level | allowed values: OFF , TRACE , DEBUG , INFO , WARN , ERROR . |
keep_alive_enabled | boolean. |
clickhouse_setting_* or ch_* | see below (2) |
http_header_* | see below (3) |
(Node.js only) keep_alive_idle_socket_ttl | non-negative number. |
- (1) For booleans, valid values will be
true
/1
andfalse
/0
. - (2) Any parameter prefixed with
clickhouse_setting_
orch_
will have this prefix removed and the rest added to client'sclickhouse_settings
. For example,?ch_async_insert=1&ch_wait_for_async_insert=1
will be the same as:
Note: boolean values for clickhouse_settings
should be passed as 1
/0
in the URL.
- (3) Similar to (2), but for
http_header
configuration. For example,?http_header_x-clickhouse-auth=foobar
will be an equivalent of:
Connecting
Gather your connection details
To connect to ClickHouse with HTTP(S) you need this information:
-
The HOST and PORT: typically, the port is 8443 when using TLS or 8123 when not using TLS.
-
The DATABASE NAME: out of the box, there is a database named
default
, use the name of the database that you want to connect to. -
The USERNAME and PASSWORD: out of the box, the username is
default
. Use the username appropriate for your use case.
The details for your ClickHouse Cloud service are available in the ClickHouse Cloud console. Select the service that you will connect to and click Connect: