[hypequery](https://hypequery.com/) › ClickHouse for Node.js Scripts, Jobs, and APIs | hypequery
Index for agents: https://hypequery.com/llms.txt
Canonical page: https://hypequery.com/clickhouse-nodejs

> Use ClickHouse from Node.js scripts, jobs, and servers without rebuilding row types and HTTP wiring around every query.

[Open sourceApache 2.0](https://github.com/hypequery/hypequery)

ClickHouse Node.js

# Use ClickHouse from Node.js without hand-maintained query types

Scripts, workers, and Express servers all end up querying ClickHouse. hypequery gives them generated types and one place for each query, so a helper buried in a service file can become a typed API when it needs to.

[Start with hypequery ](https://hypequery.com/docs/quick-start)[See the REST API](https://hypequery.com/clickhouse-api)

Runtime

Node.js and Bun

Frameworks

Express, Next.js, custom Node servers

Type source

Generated from schema

## Node services accumulate query helpers fast

A server codebase rarely has just one query caller. Jobs, workers, route handlers, and admin tasks all want the same data, and each one tends to grow its own helper if there is no shared path.

## The HTTP layer keeps getting rebuilt

Once a query needs an endpoint, teams start writing validation, auth handling, error shaping, and docs around it. That is a lot of repeated backend work for what should still be one analytics query.

## Type mismatches hide inside server code

Node developers often spot the runtime mismatch later because the wrong shape is hidden inside service code and only surfaces after it has already passed through several layers.

Setup

## Start with a typed backend query layer

Generate the schema once, initialize the builder in your backend, and make that the standard way Node code reaches ClickHouse. That gives scripts, workers, and services the same typed starting point.

- Works well in Node.js services, scripts, and Bun runtimes
- Schema types generated from your live ClickHouse instance
- Correct ClickHouse-to-Node.js type mappings, so you don't have to guess them
- Composable query builder with support for typed filters, joins, CTEs, and raw SQL when needed
- Optional HTTP serving layer via @hypequery/serve handlers

Setup

### A backend entry point you can reuse across services and jobs

setup.ts

```
import { createQueryBuilder } from '@hypequery/clickhouse';
import type { DB } from './schema'; // generated from your ClickHouse schema

const db = createQueryBuilder<DB>({
  url: process.env.CLICKHOUSE_URL!, // e.g. http://localhost:8123
  username: process.env.CLICKHOUSE_USER!,
  password: process.env.CLICKHOUSE_PASSWORD!,
  database: 'analytics',
});

// db is fully typed — autocomplete on table names and column names
```

The point of this setup is consistency. Once the builder exists in one standard place, the rest of the Node codebase stops reinventing how it talks to ClickHouse.

With Express

## Promote a query into an endpoint without burying logic in the route

When Node code needs to expose analytics over HTTP, keep the query definition separate from the web framework and mount the handler around it. That keeps the route thin and the analytics logic reusable.

That separation is what makes the Node page distinct from the framework pages. The query can still run directly inside a job or script, and only becomes an HTTP concern when you actually need a server surface.

If your app is specifically App Router-first, use the Next.js page. This page is for the general backend case where Express-style or standalone Node runtimes are still the center of gravity.

Express integration

### A thin Express mount around a shared analytics definition

with-express.ts

```
import express from 'express';
import { createNodeHandler, initServe } from '@hypequery/serve';
import { z } from 'zod';

const app = express();

const { query, serve } = initServe({
  context: () => ({ db }),
});

const pageViews = query({
  input: z.object({ from: z.string(), to: z.string() }),
  query: async ({ ctx, input }) =>
    ctx.db
      .table('events')
      .where('event_name', 'eq', 'page_view')
      .where('created_at', 'gte', input.from)
      .where('created_at', 'lte', input.to)
      .groupBy(['page'])
      .count('id', 'views')
      .orderBy('views', 'DESC')
      .execute(),
});

const api = serve({ queries: { pageViews } });
app.all('/api/analytics/{*path}', createNodeHandler(api.handler));
```

The analytics logic no longer lives in the route.

Where teams usually get stuck

## Questions teams ask

### What this page is really for

It is for backend teams whose main concern is service code, jobs, and server routes, not client hooks or App Router rendering.

### Where Node teams usually feel pain

Usually when the same query appears in a worker, a route, and a report generator and every copy evolves separately.

### Why the server layer matters

Backend teams end up paying for every decision twice if query logic and route logic are fused together. This page is about keeping them separate.

### Where to branch next

Use the Next.js page for App Router specifics and the REST page if the endpoint layer itself is your main concern.

Examples

## Runnable examples

Every example runs against the same seeded ClickHouse dataset. Scaffold one with `npx @hypequery/cli create <slug> -y`, or browse the [full list](https://hypequery.com/examples).

[Recipe · TypeScript](https://hypequery.com/recipes/async-inserts-node)

### [ClickHouse async inserts from Node.js: per-row vs batch vs async_insert](https://hypequery.com/recipes/async-inserts-node)

[Insert the same 1,000 rows from Node per row, in client batches and with async_insert, and count the parts each creates: 1,000, 4, 10 and 1.](https://hypequery.com/recipes/async-inserts-node)

[Node.jsClickHouseIngestionReal time analytics](https://hypequery.com/recipes/async-inserts-node)

[Open recipe](https://hypequery.com/recipes/async-inserts-node)

[Guide · ClickHouse 25.8](https://hypequery.com/guides/clickhouse-docker-compose)

### [ClickHouse with Docker Compose: a pinned, persistent local server with a million-row sample dataset](https://hypequery.com/guides/clickhouse-docker-compose)

[Run ClickHouse 25.8 in Docker Compose with a healthcheck, a password, volumes and a 1M-row sample dataset, then connect from Node, Python and hypequery.](https://hypequery.com/guides/clickhouse-docker-compose)

[Quantile](https://hypequery.com/guides/clickhouse-docker-compose)

[Open guide](https://hypequery.com/guides/clickhouse-docker-compose)

[Example · TypeScript](https://hypequery.com/examples/clickhouse-rest-api-openapi)

### [A ClickHouse REST API with OpenAPI docs, API keys and rate limits](https://hypequery.com/examples/clickhouse-rest-api-openapi)

[Put a typed REST API in front of ClickHouse: named GET endpoints with validated params, API-key auth, rate limits, and an OpenAPI spec with live docs.](https://hypequery.com/examples/clickhouse-rest-api-openapi)

[Node.jsHonoClickHouseAPIAnalytics API](https://hypequery.com/examples/clickhouse-rest-api-openapi)

[Open example](https://hypequery.com/examples/clickhouse-rest-api-openapi)

[Example · TypeScript](https://hypequery.com/examples/materialized-view-rollups)

### [ClickHouse materialized view rollups: incremental vs refreshable](https://hypequery.com/examples/materialized-view-rollups)

[Pre-aggregate a million events two ways, incremental and refreshable, and query both through one typed hypequery dataset.](https://hypequery.com/examples/materialized-view-rollups)

[Node.jsClickHousePre-aggregationDashboardsTime series](https://hypequery.com/examples/materialized-view-rollups)

[Open example](https://hypequery.com/examples/materialized-view-rollups)

[Example · TypeScript](https://hypequery.com/examples/mcp-agent-analytics)

### [A governed ClickHouse MCP server: agents query the semantic layer, not the database](https://hypequery.com/examples/mcp-agent-analytics)

[Give Claude Code, Cursor or any MCP client ClickHouse analytics through four governed tools: only published datasets, no SQL, one tenant per server.](https://hypequery.com/examples/mcp-agent-analytics)

[Node.jsMCPClickHouseAI agentsMulti-tenantSemantic layer](https://hypequery.com/examples/mcp-agent-analytics)

[Open example](https://hypequery.com/examples/mcp-agent-analytics)

[Example · TypeScript](https://hypequery.com/examples/multi-tenant-embedded-analytics)

### [Multi-tenant embedded analytics on ClickHouse with signed tokens](https://hypequery.com/examples/multi-tenant-embedded-analytics)

[Embed tenant-scoped ClickHouse analytics in your product: your server mints a short-lived token, hypequery enforces the tenant on every query.](https://hypequery.com/examples/multi-tenant-embedded-analytics)

[Node.jsHonoClickHouseEmbedded analyticsMulti-tenantSaaS analytics](https://hypequery.com/examples/multi-tenant-embedded-analytics)

[Open example](https://hypequery.com/examples/multi-tenant-embedded-analytics)

[Example · TypeScript](https://hypequery.com/examples/postgres-app-clickhouse-analytics)

### [Add ClickHouse analytics to a Postgres + Drizzle app](https://hypequery.com/examples/postgres-app-clickhouse-analytics)

[Keep accounts in Postgres with Drizzle, serve usage from ClickHouse with hypequery, and join the two by account id in one API route.](https://hypequery.com/examples/postgres-app-clickhouse-analytics)

[Node.jsHonoDrizzlePostgresClickHouseSaaS analyticsUsage analyticsPostgres to ClickHouse](https://hypequery.com/examples/postgres-app-clickhouse-analytics)

[Open example](https://hypequery.com/examples/postgres-app-clickhouse-analytics)

Further reading

## Go deeper where it actually helps

### [ClickHouse JavaScript](https://hypequery.com/clickhouse-js)

[The broader JavaScript and TypeScript guide, with the same patterns in a framework-agnostic context.](https://hypequery.com/clickhouse-js)

[Open guide](https://hypequery.com/clickhouse-js)

### [ClickHouse Next.js](https://hypequery.com/clickhouse-nextjs)

[Integrating ClickHouse into Next.js App Router with server components and API routes.](https://hypequery.com/clickhouse-nextjs)

[Open guide](https://hypequery.com/clickhouse-nextjs)

### [ClickHouse REST API](https://hypequery.com/clickhouse-api)

[Full guide to serving typed ClickHouse queries as HTTP endpoints with @hypequery/serve.](https://hypequery.com/clickhouse-api)

[Open guide](https://hypequery.com/clickhouse-api)

### [ClickHouse TypeScript](https://hypequery.com/clickhouse-typescript)

[The full TypeScript workflow, covering schema types, typed queries, HTTP APIs, and React hooks.](https://hypequery.com/clickhouse-typescript)

[Open guide](https://hypequery.com/clickhouse-typescript)

### [ClickHouse in Node.js: Adding Analytics to a Production Backend](https://hypequery.com/blog/add-clickhouse-analytics-nodejs-backend)

[Add ClickHouse to an Express, Fastify or Nest app: typed queries, one client per process, timeouts and retries, testing, and SAMPLE.](https://hypequery.com/blog/add-clickhouse-analytics-nodejs-backend)

[Open guide](https://hypequery.com/blog/add-clickhouse-analytics-nodejs-backend)

Related pillars

[ClickHouse with TypeScript and Python](https://hypequery.com/clickhouse)[ClickHouse JavaScript](https://hypequery.com/clickhouse-js)[ClickHouse TypeScript](https://hypequery.com/clickhouse-typescript)[ClickHouse Next.js](https://hypequery.com/clickhouse-nextjs)[ClickHouse REST API](https://hypequery.com/clickhouse-api)

Next step

## Replace one backend query path end to end

Pick one script, worker, or route that currently owns its own ClickHouse query code, move it onto the shared builder, and then decide whether it also deserves a served API surface.

Open source, and it runs in your own backend. Want it hosted?

[Start with hypequery ](https://hypequery.com/docs/quick-start)[See the REST API guide](https://hypequery.com/clickhouse-api)
