
How to Access China A-Shares Real-time Market Data with an API
Learn how to access China A-share real-time market data using REST API and WebSocket. Explore stock quote retrieval, live price updates, K-line queries, and practical integration tips for fintech developers.
When building a stock dashboard, watchlist, or price alert system, developers need a reliable way to retrieve and update market data. A China A-share market data API provides a way to integrate stock quotes into an application without building the entire data collection layer from scratch.
The choice of interface depends on the task: REST works well for on-demand queries and historical data, while WebSocket is suitable for continuous real-time price updates.
Choose the Right API Interface
| Requirement | Recommended interface |
|---|---|
| Retrieve the latest transaction price | REST API |
| Receive continuous price updates | WebSocket API |
| Load historical candlestick data | REST K-line API |
| Update recent K-lines for multiple stocks | Batch K-line API |
Before making requests, check the A-share product code list and obtain your API token.
Retrieve A-Share Prices with REST
For a dashboard displaying the latest transaction price, AllTick provides an HTTP interface for stock quote queries. The following Python example shows how to construct a request:
import json
import requests
base_url = "https://quote.alltick.co/quote-stock-b-api"
url = f"{base_url}/trade-tick"
query = {
"trace": "demo-request-001",
"data": {
"symbol_list": [{"code": "600416.SH"}]
}
}
response = requests.get(
url,
params={
"token": "YOUR_API_TOKEN",
"query": json.dumps(query)
},
timeout=10
)
response.raise_for_status()
result = response.json()
print(result)
Replace the placeholder token with your own and confirm the stock code against the product list. Inspect the returned JSON before mapping its fields to your application.
For production use, handle timeouts and unsuccessful responses, validate the returned data before updating prices, and keep your token out of public source code. The AllTick API documentation provides further integration guidance.
Stream Real-Time Quotes with WebSocket
Repeated REST polling can create unnecessary requests when an application needs frequent price updates. WebSocket allows a client to subscribe to supported stocks and process incoming quote messages continuously.
AllTick's transaction quote subscription uses protocol ID 22004. A simplified subscription message looks like this:
{
"cmd_id": 22004,
"seq_id": 1,
"trace": "demo-subscription-001",
"data": {
"symbol_list": [{"code": "600416.SH"}]
}
}
Refer to the WebSocket transaction quote subscription guide for connection requirements and the complete message format.
In production, track the latest update time for each symbol, handle disconnections, and restore subscriptions after reconnecting. Check the WebSocket interface limits before increasing the number of simultaneous connections or subscribed stocks.
Query K-Line Data for Charts
Stock dashboards often need historical candlesticks alongside live prices. AllTick's HTTP K-line interface supports queries by stock code, interval, and number of candles.
For example, this request structure specifies 20 one-minute candles:
{
"trace": "kline-demo-001",
"data": {
"code": "600416.SH",
"kline_type": 1,
"kline_timestamp_end": 0,
"query_kline_num": 20,
"adjust_type": 0
}
}
Here, kline_type: 1 represents a one-minute interval, while query_kline_num: 20 requests 20 candles. A single K-line query can return up to 500 records. The currently supported stock adjustment type is 0.
One important limitation: AllTick does not support K-line push through WebSocket. Retrieve historical candles through the K-line query interface. For multiple stocks, the batch K-line interface can help update recent candles while reducing repeated individual queries. Cache historical data locally where appropriate.
Common Integration Issues
- No quote updates: Check the stock code, market trading status, and subscription response.
- Incorrect chart times: Verify timestamp units and apply a consistent timezone.
- Stale prices after reconnecting: Restore subscriptions and record the latest update time.
- Requests exceed limits: Review the HTTP interface restrictions and adjust polling frequency or caching.
Build Your A-Share Data Integration
For fintech developers, a practical approach is to use REST for initial quotes and historical K-lines, then WebSocket for continuous price updates. This separates historical data retrieval from live delivery and makes integration issues easier to diagnose.
Explore the AllTick API documentation to review the available interfaces and choose the data access method that fits your application.