聚合数据火车订票查询
聚合数据(juhe.cn)提供的火车订票查询接口,用于查询两站之间的列车时刻表,支持按站点名称或站点编码精确查询。
接口概述
| 项目 | 内容 |
|---|
| ------ | ------ |
| 接口名称 | 列车站到站时刻表 |
| 请求地址 | https://apis.juhe.cn/fapigw/train/query |
| 请求方式 | GET / POST |
| 数据格式 | JSON |
| Content-Type | application/x-www-form-urlencoded |
| 可查日期范围 | 当前日期起 15 天内 |
| 接口 ID | 817 |
请求参数
| 参数名 | 类型 | 必填 | 说明 | 示例值 |
|---|
| -------- | ------ | ------ | ------ | -------- |
key | string | 是 | 应用 APPKEY(在个人中心→数据中心→我的API 查看) | 您申请的key |
search_type | string | 是 | 查询方式:1 — 按站点名称;2 — 按站点编码 | 1 |
departure_station | string | 是 | 出发站。search_type=1 时填中文站名(如「北京南」);=2 时填站点编码(3 位字母,如「VNP」) | 北京南 |
arrival_station | string | 是 | 到达站。同上 | 苏州北 |
date | string | 是 | 出发日期,格式 YYYY-MM-DD;限当日 + 15 天内 | 2026-06-06 |
filter | string | 否 | 车次筛选,默认全部。多个条件可组合:G-高铁/城际,D-动车,Z-直达特快,T-特快,K-快速,O-其他,F-复兴号,S-智能动车组 | G |
enable_booking | string | 否 | 1 — 仅返回可预订班次;2 — 返回所有(默认) | 1 |
departure_time_range | string | 否 | 出发时段筛选:凌晨([00:00-06:00),上午([06:00-12:00)),下午([12:00-18:00)),晚上([18:00-24:00)) | 下午 |
> ⚠️ 重要:search_type 的值为 1(名称)和 2(编码),而非 0 和 1。这是根据官方文档和实际测试验证的结果。
返回参数
顶层字段
| 字段 | 类型 | 说明 |
|---|
| ------ | ------ | ------ |
error_code | int | 返回码(0 表示成功,非 0 表示错误) |
reason | string | 返回说明 |
result | array | 车次列表(数组),查询成功时存在 |
result[] 中每个元素的字段
| 字段 | 类型 | 说明 | 示例值 |
|---|
| ------ | ------ | ------ | -------- |
train_no | string | 车次号 | G25 |
departure_station | string | 出发站名称 | 北京南 |
arrival_station | string | 到达站名称 | 苏州北 |
departure_station_code | string | 出发站点编码 | VNP |
arrival_station_code | string | 到达站点编码 | OHH |
departure_time | string | 出发时间 | 18:04 |
arrival_time | string | 到达时间 | 22:32 |
duration | string | 运行时长(格式 HH:mm) | 04:28 |
enable_booking | string | 12306 上是否可预订:Y-是,N-否 | Y |
prices | array | 票价信息数组 | 见下方 |
train_flags | array | 列车标签数组,如「智能动车组」「复兴号」「静音车厢」「支持选铺」 | ["智能动车组","复兴号"] |
prices[] 中每个元素的字段
| 字段 | 类型 | 说明 | 示例值 |
|---|
| ------ | ------ | ------ | -------- |
seat_name | string | 坐席名称 | 二等座 |
seat_type_code | string | 坐席类型代码 | O |
price | float | 票价(元) | 627 |
discount | string/null | 折扣信息 | null |
num | string | 余票数量。「有」表示充足,数字表示具体余票张数,「无」表示售罄 | 1 |
错误码
业务错误码(error_code)
| error_code | 说明 |
|---|
| ----------- | ------ |
0 | 请求成功 |
281701 | 查询失败,具体原因请参考返回 reason |
系统级错误码(通用)
| error_code | 说明 | 旧版(resultcode) |
|---|
| ----------- | ------ | ------------------- |
10001 | 错误的请求 KEY | 101 |
10002 | 该 KEY 无请求权限 | 102 |
10003 | KEY 过期 | 103 |
10004 | 错误的 OPENID | 104 |
10005 | 应用未审核超时,请提交认证 | 105 |
10007 | 未知的请求源 | 107 |
10008 | 被禁止的 IP | 108 |
10009 | 被禁止的 KEY | 109 |
10011 | 当前 IP 请求超过限制 | 111 |
10012 | 请求超过次数限制 | 112 |
10013 | 测试 KEY 超过请求限制 | 113 |
10014 | 系统内部异常 | 114 |
10015 | 当前应用没有请求该服务的权限 | — |
10020 | 接口维护 | 120 |
10021 | 接口停用 | 121 |
> 错误码格式说明(示例:281701):首位 2 表示服务级错误(1 为系统级错误),002 为服务模块代码(数据 ID),01 为具体错误代码。
返回数据示例
{
"reason": "success",
"result": [
{
"train_no": "G25",
"departure_station": "北京南",
"arrival_station": "苏州北",
"departure_station_code": "VNP",
"arrival_station_code": "OHH",
"departure_time": "18:04",
"arrival_time": "22:32",
"duration": "04:28",
"enable_booking": "Y",
"prices": [
{
"seat_name": "二等座",
"seat_type_code": "O",
"price": 627,
"discount": null,
"num": "有"
},
{
"seat_name": "一等座",
"seat_type_code": "M",
"price": 923,
"discount": null,
"num": "11"
},
{
"seat_name": "商务座",
"seat_type_code": "9",
"price": 1748,
"discount": null,
"num": "5"
}
],
"train_flags": ["智能动车组", "复兴号", "静音车厢"]
}
],
"error_code": 0
}
代码示例
cURL
# 按站点名称查询
curl -G "https://apis.juhe.cn/fapigw/train/query" \
--data-urlencode "key=YOUR_APPKEY" \
--data-urlencode "search_type=1" \
--data-urlencode "departure_station=北京南" \
--data-urlencode "arrival_station=苏州北" \
--data-urlencode "date=2026-06-06"
# 按站点编码查询
curl -G "https://apis.juhe.cn/fapigw/train/query" \
--data-urlencode "key=YOUR_APPKEY" \
--data-urlencode "search_type=2" \
--data-urlencode "departure_station=VNP" \
--data-urlencode "arrival_station=OHH" \
--data-urlencode "date=2026-06-06"
Python
import requests
# 基本参数配置
apiUrl = 'https://apis.juhe.cn/fapigw/train/query'
apiKey = 'YOUR_APPKEY' # 替换为你的 AppKey
# 接口请求入参配置
requestParams = {
'key': apiKey,
'search_type': '1', # 1-按站点名称,2-按站点编码
'departure_station': '北京南', # 出发站
'arrival_station': '苏州北', # 到达站
'date': '2026-06-06', # 日期(YYYY-MM-DD)
'filter': '', # 筛选车次类型(可选)
'enable_booking': '', # 1-仅可预订,2-全部(可选)
'departure_time_range': '', # 凌晨/上午/下午/晚上(可选)
}
# 发起接口网络请求
response = requests.get(apiUrl, params=requestParams)
# 解析响应结果
if response.status_code == 200:
result = response.json()
if result['error_code'] == 0:
trains = result['result']
print(f"共查询到 {len(trains)} 趟车次")
for train in trains:
print(f"\n{train['train_no']} | {train['departure_station']}→{train['arrival_station']}"
f" | {train['departure_time']}→{train['arrival_time']}"
f" | 历时 {train['duration']}")
if train.get('train_flags'):
print(f" 标签: {'/'.join(train['train_flags'])}")
for seat in train['prices']:
remain = seat['num']
print(f" {seat['seat_name']}: ¥{seat['price']} 余{remain}")
else:
print(f"查询失败: [{result['error_code']}] {result['reason']}")
else:
print('请求异常')
Node.js
const axios = require('axios');
const apiUrl = 'https://apis.juhe.cn/fapigw/train/query';
const apiKey = 'YOUR_APPKEY'; // 替换为你的 AppKey
const params = {
key: apiKey,
search_type: '1', // 1-按站点名称,2-按站点编码
departure_station: '北京南', // 出发站
arrival_station: '苏州北', // 到达站
date: '2026-06-06', // 日期(YYYY-MM-DD)
};
axios.get(apiUrl, { params })
.then(response => {
const result = response.data;
if (result.error_code === 0) {
const trains = result.result;
console.log(`共查询到 ${trains.length} 趟车次`);
trains.forEach(train => {
console.log(`\n${train.train_no} | ${train.departure_station}→${train.arrival_station} | ${train.departure_time}→${train.arrival_time} | 历时 ${train.duration}`);
if (train.train_flags && train.train_flags.length > 0) {
console.log(` 标签: ${train.train_flags.join('/')}`);
}
train.prices.forEach(seat => {
console.log(` ${seat.seat_name}: ¥${seat.price} 余${seat.num}`);
});
});
} else {
console.log(`查询失败: [${result.error_code}] ${result.reason}`);
}
})
.catch(error => {
console.error('请求异常', error.message);
});
车站编码说明
all_train_stations.json 文件中存储了全国所有火车站的编码信息,格式如下:
{
"北京": {
"station_code": "BJP",
"station_name": "北京",
"station_pinyin": "beijing",
"station_city": "北京"
},
"北京南": {
"station_code": "VNP",
"station_name": "北京南",
"station_pinyin": "beijingnan",
"station_city": "北京"
}
}
| 字段 | 说明 |
|---|
| ------ | ------ |
| 键 | 车站中文名称 |
station_code | 车站编码(3 位字母),用于 search_type=2 时查询 |
station_name | 车站中文名称 |
station_pinyin | 车站拼音 |
station_city | 所属城市 |
> 车站编码文件来源:https://juhe.oss-cn-hangzhou.aliyuncs.com/api_file/817/all_train_stations.json
使用注意事项
- 日期范围:仅可查询当前日期起 15 天内的车次,不支持历史日期
- API Key:需在聚合数据官网注册并开通接口权限后获取
- 查询方式:
search_type=1:使用中文站名查询,如「北京」「上海虹桥」search_type=2:使用 3 位字母编码查询,如「BJP」「SHH」
- 筛选参数:
filter 使用字母代码(G/D/Z/T/K/F/S/O),可组合多个 - 时段筛选:
departure_time_range 使用中文时段词(凌晨/上午/下午/晚上) - 余票信息:
num 值为「有」表示余票充足,数字表示具体余票张数,「无」表示售罄 - 日调用次数:不同套餐有不同限制,在聚合数据控制台查看
- 请求限制:QPS 限制约 1次/秒,超出返回
10011/10012 - 故障重试:若遇
281701 错误(无法识别的站点),检查站名是否标准或改用站点编码重试
文件清单
| 文件 | 说明 |
|---|
| ------ | ------ |
SKILL.md | 本技能文档 |
all_train_stations.json | 全国火车站编码对照表(约 485KB,3393 个车站) |
相关链接