
How to Get Real-Time Market Data with an A-Share API: A Guide to REST API Integration and Data Processing
Learn how to retrieve the latest A-share market quotes using an API, with a guide to REST API integration, Python requests, market data processing, and choosing between REST API and WebSocket.
When developing a stock market dashboard, retrieving the price of a single stock is only the first step. You also need to consider whether the data returned by the API corresponds to the intended stock, whether the market timestamp is reliable, and whether price changes are properly reflected in the application.
For fintech developers, choosing a suitable A-share API can reduce the work involved in building and maintaining a market-data collection pipeline. This article focuses on practical development requirements and explains how to retrieve the latest A-share quotes through a REST API, process the response data, and choose an integration method based on update frequency.
Choose the Right API Before Retrieving Real-Time Market Data
A-share market applications typically require two types of data: the latest quotes and historical candlestick data.
The latest quotes are suitable for displaying information such as the current price, trading volume, and trade time. Historical candlestick data is used to draw charts, calculate technical indicators, and initialize historical charts. These data types serve different purposes, so historical candlestick queries cannot replace requests for the latest quotes.
AllTick provides stock market data APIs for A-shares, Hong Kong stocks, and US stocks. Its REST API includes endpoints for retrieving the latest quotes, historical candlesticks, order-book depth, and basic stock information. Applications that only need on-demand market data can start with the REST API. If an application needs to receive market updates continuously, WebSocket subscriptions can also be considered.
Retrieve A-Share Market Data with Python
Before integrating the API, obtain an API key and confirm that the target stock is included in AllTick's supported instrument list. A-share stock codes use exchange suffixes. For example, 600416.SH identifies a Shanghai Stock Exchange instrument, while 000627.SZ identifies a Shenzhen Stock Exchange instrument.
If you need the latest quotes, use /v1/quote/{queryData} rather than the historical candlestick endpoint. AllTick's newer REST API uses the X-API-Key request header for authentication. Query parameters in GET requests must also be encoded according to the API documentation.
The following Python example first demonstrates how to structure the request parameters and call the historical candlestick endpoint to validate the data integration workflow.
import json
import os
import requests
API_KEY = os.environ["ALLTICK_API_KEY"]
query_data = {
"trace": "ashare-kline-001",
"data": {
"code": "600416.SH",
"kline_type": 1,
"kline_timestamp_end": 0,
"query_kline_num": 2,
"adjust_type": 0
}
}
url = "https://quote.alltick.co/quote-stock-b-api/v1/kline"
response = requests.get(
url,
params={
"query": json.dumps(query_data, separators=(",", ":"))
},
headers={"X-API-Key": API_KEY},
timeout=10
)
response.raise_for_status()
result = response.json()
if result.get("ret") != 200:
raise RuntimeError(f"API error: {result.get('msg')}")
print(result["data"])This code requests the two most recent one-minute candlesticks. It is intended to help validate the request, authentication, and response-parsing workflow. It does not retrieve real-time latest quotes and should not be treated as a substitute for validating the quote endpoint.
For production integration, configure the quote endpoint's request parameters according to the current REST API documentation, then verify that the returned fields meet the application's requirements.
What to Check When Processing Market Data
After receiving data from the API, do not pass every field directly to the frontend without processing it. At least three issues should be addressed.
Keep the market timestamp separate from the reception timestamp. The market timestamp indicates when the market event occurred, while the reception timestamp indicates when the application received the data. Recording both makes it easier to investigate delays in API responses, network transmission, and frontend updates.
Prevent older data from overwriting newer data. When a system processes multiple requests concurrently, responses may arrive in a different order from the requests that triggered them. When updating a cache, design validation rules based on available market timestamps and data identifiers rather than allowing the last response to arrive to overwrite the current value automatically.
Do not mistake a lack of updates for an API failure. The A-share market has different trading phases, including call auctions, continuous trading, and the midday break. If no new trades occur outside active trading periods, the latest trade data may remain unchanged for some time. To determine whether an issue exists, consider the trading calendar, market-data timestamps, API status, and error messages together.
The latest-quote endpoint can also provide data such as the latest traded price, trading volume, turnover, trade time, and trade direction. However, it is not an endpoint for retrieving historical tick-by-tick records. If your strategy research requires complete historical tick data, first confirm that the relevant product and endpoint support that requirement.
REST API or WebSocket?
The choice depends on the application's market-data update requirements. Neither approach is always better than the other.
- REST API: Suitable for page initialization, on-demand latest-quote queries, historical candlestick loading, and dashboards with relatively low request frequency.
- WebSocket: Suitable for applications that need to receive continuous market updates. It can reduce the overhead of repeatedly sending HTTP requests, but the application must handle disconnections, resubscriptions, and data recovery.
For applications displaying a small number of stocks, completing the initial integration with a REST API may be sufficient. If many instruments need continuous updates, consider WebSocket subscriptions and local caching. Regardless of the approach, validate the solution against plan-specific request limits, instrument coverage, and actual update requirements.
How to Choose an A-Share Market Data API for Your Application
When evaluating an API, do not focus on price alone. Confirm whether the target stocks are supported, whether the required market-data types are available, how much historical data is provided, and whether the request frequency meets the application's needs.
AllTick's Stock Market Data API product page describes its market-data capabilities for A-shares, Hong Kong stocks, and US stocks. Developers can also consult the REST API documentation to verify request parameters, authentication methods, and response structures.
For developers looking for an A-share real-time market data API, a practical approach is to select a small number of target stocks and validate the latest quotes, market timestamps, and error responses before gradually introducing caching or WebSocket subscriptions. This helps identify data-pipeline problems early and avoids investing in a complex system before the API integration has been validated.
Please note that AllTick market data is provided for informational purposes and should not be treated directly as trading or investment advice. Before using the data, also confirm the applicable data licensing terms and redistribution restrictions.