惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
IT之家
IT之家
博客园 - 叶小钗
雷峰网
雷峰网
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Day 24 of 100 Days of ClickHouse: Working with the ClickH...
Kanishga Subramani · 2026-06-19 · via DEV Community

When most people think about interacting with a database, they imagine using a client library, a graphical interface, or a command-line tool. While these are common approaches, ClickHouse also provides a built-in HTTP API that enables you to communicate with the database using standard HTTP requests.

This means that if your application, script, or service can send an HTTP request, it can interact with ClickHouse. Whether you're using Python, JavaScript, Go, Java, cURL, Postman, or even a simple shell script, the HTTP API provides a universal and lightweight way to execute queries, insert data, and manage your database.

In this article, we'll explore how the ClickHouse HTTP API works, how to use it effectively, and why it's such an important feature for modern applications.


What Is the ClickHouse HTTP API?

The ClickHouse HTTP API is a built-in interface that allows applications to communicate with ClickHouse over the HTTP protocol.

Instead of connecting through the native ClickHouse protocol, your application simply sends an HTTP request containing an SQL query, and ClickHouse executes it before returning the result.

By default, the HTTP API runs on port 8123 and supports both GET and POST requests.

Unlike the native protocol, the HTTP interface is:

  • Language independent
  • Easy to integrate with any application
  • Firewall friendly
  • Ideal for automation
  • Simple to test using browsers, cURL, or Postman

This flexibility makes it popular for scripting, automation, lightweight services, and REST-based applications.


How the HTTP API Works

The communication flow is straightforward:

Application
     │
     ▼
HTTP Request
     │
     ▼
ClickHouse Server
     │
     ▼
SQL Execution
     │
     ▼
HTTP Response

The response can be returned in many formats including JSON, CSV, TSV, Parquet, Arrow, and more.


Default Ports

ClickHouse exposes multiple interfaces.

Interface Default Port
Native TCP 9000
HTTP API 8123

For HTTP-based communication, you'll primarily work with port 8123.


Prerequisites

Before following along, ensure you have:

  • A running ClickHouse server
  • cURL installed
  • Basic SQL knowledge
  • Access to port 8123

Step 1: Verify the HTTP API

The simplest way to verify that the HTTP API is available is by checking the built-in ping endpoint.

curl http://localhost:8123/ping

Expected response:

Ok.

If you receive this response, your HTTP interface is working correctly.


Step 2: Execute Your First Query

One of the easiest ways to query ClickHouse is by passing SQL as a URL parameter.

Using GET:

curl "http://localhost:8123/?query=SELECT+version()"

Output:

25.3.1.1

Although GET works well for simple queries, POST requests are generally preferred for larger SQL statements.

Using POST:

curl -X POST http://localhost:8123/ \
  --data-binary "SELECT version()"

Output:

25.3.1.1

POST requests are cleaner and avoid URL length limitations.


Step 3: Authentication

If authentication is enabled, ClickHouse supports several methods.

URL Parameters

curl "http://localhost:8123/?user=default&password=yourpassword&query=SELECT+1"

HTTP Basic Authentication

curl -u default:yourpassword \
"http://localhost:8123/?query=SELECT+1"

HTTP Headers (Recommended)

curl http://localhost:8123/ \
-H "X-ClickHouse-User: default" \
-H "X-ClickHouse-Key: yourpassword" \
--data-binary "SELECT 1"

Using request headers is recommended because credentials aren't exposed in URLs or logs.


Step 4: Selecting a Database

Queries execute against the default database unless another database is specified.

Specify a database using the request parameter:

curl -u default:yourpassword \
"http://localhost:8123/?database=my_database" \
--data-binary "SELECT count() FROM my_table"

Or reference the database directly inside the SQL statement:

SELECT count()
FROM my_database.my_table;


Step 5: Querying Data

Executing SELECT statements is straightforward.

curl -u default:yourpassword \
http://localhost:8123/ \
--data-binary "SELECT * FROM default.products LIMIT 5"

By default, ClickHouse returns data in TabSeparated format.


Output Formats

One of the strengths of the HTTP API is its support for multiple output formats.

JSON

SELECT *
FROM default.products
FORMAT JSON

Useful for:

  • REST APIs
  • Web applications
  • JavaScript clients

CSV

SELECT *
FROM default.products
FORMAT CSV

Ideal for:

  • Excel
  • Data exports
  • ETL pipelines

JSONEachRow

SELECT *
FROM default.products
FORMAT JSONEachRow

Each row is returned as an individual JSON document, making it perfect for:

  • Streaming
  • Kafka
  • Log processing
  • Large datasets

Other Supported Formats

ClickHouse supports numerous formats including:

  • JSON
  • JSONCompact
  • JSONEachRow
  • CSV
  • TSV
  • Pretty
  • PrettyCompact
  • Parquet
  • Apache Arrow
  • TabSeparated

Choosing the appropriate format depends on how downstream systems consume the data.


Step 6: Inserting Data

The HTTP API also supports data ingestion.

Insert TabSeparated Data

curl -u default:yourpassword \
"http://localhost:8123/?query=INSERT+INTO+default.products+FORMAT+TabSeparated" \
--data-binary $'5\tMechanical Keyboard\tAccessories\n6\tMonitor 4K\tElectronics\n'


Insert CSV

curl -u default:yourpassword \
"http://localhost:8123/?query=INSERT+INTO+default.products+FORMAT+CSV" \
--data-binary @products.csv


Insert JSON

curl -u default:yourpassword \
"http://localhost:8123/?query=INSERT+INTO+default.products+FORMAT+JSONEachRow" \
--data-binary '
{"product_id":7,"name":"Webcam HD","category":"Accessories"}
{"product_id":8,"name":"Desk Lamp","category":"Furniture"}
'

This flexibility allows applications to ingest data using the format most convenient for them.


Step 7: Creating and Managing Tables

The HTTP API supports all standard Data Definition Language (DDL) operations.

Create a table:

CREATE TABLE default.logs
(
    server_id LowCardinality(String),
    level LowCardinality(String),
    message String,
    timestamp DateTime
)
ENGINE = MergeTree()
ORDER BY timestamp;

Drop a table:

DROP TABLE IF EXISTS default.logs;

Similarly, ALTER, TRUNCATE, OPTIMIZE, and other administrative statements are fully supported.


Useful HTTP Parameters

Several query parameters can simplify common tasks.

Parameter Purpose
query SQL query
user Username
password Password
database Database name
default_format Default response format
max_rows_to_read Scan limit
max_execution_time Query timeout
compress Enable response compression

These parameters help control execution behavior without modifying SQL itself.


Best Practices

To get the most out of the ClickHouse HTTP API:

  • Use POST requests for longer SQL statements.
  • Send credentials using HTTP headers instead of URLs.
  • Use JSONEachRow for streaming large datasets.
  • Set max_execution_time to prevent runaway queries.
  • Enable compression for large responses.
  • Always specify the database explicitly.
  • Use official ClickHouse client libraries in production when advanced features like connection pooling and automatic retries are required.

Following these practices results in more secure, reliable, and efficient integrations.


HTTP API vs Native Protocol

Feature HTTP API Native Protocol
Ease of Use ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐
Performance ⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
Driver Required No Yes
Universal Support Yes Driver Dependent
Automation Excellent Good
Bulk Inserts Good Excellent

The HTTP API is ideal for integrations, scripting, and lightweight services, while the native protocol remains the best option for performance-critical applications.


Real-World Use Cases

The ClickHouse HTTP API is commonly used for:

  • Building REST APIs
  • Internal dashboards
  • Automation scripts
  • Monitoring systems
  • CI/CD pipelines
  • Serverless applications
  • ETL workflows
  • Data export services
  • Microservices
  • Third-party integrations

Because HTTP is universally supported, integrating ClickHouse into existing systems becomes remarkably simple.


Final Thoughts

The ClickHouse HTTP API demonstrates how powerful database interactions can also be simple. By exposing SQL operations over standard HTTP, ClickHouse removes the dependency on specialized drivers and enables seamless integration with virtually any programming language or platform.

Whether you're executing queries, loading data, automating administrative tasks, or building cloud-native applications, the HTTP API offers a flexible and reliable interface that scales from quick experiments to production workloads.

As your ClickHouse deployments grow, understanding the HTTP API becomes increasingly valuable. It's a feature that bridges the gap between databases and modern software architectures, making automation, integration, and analytics significantly easier.

In the next article of this 100 Days of ClickHouse series, we'll continue exploring another powerful capability that helps you build faster, more scalable analytical systems.