Skip to content

最后更新

更新日志

Trainline API

正常只读

Trainline(thetrainline.com)上任意两个车站之间的火车票价和发车时间。 来源: www.thetrainline.com.

基础 URL
https://api.maviapi.com/v1/sites/thetrainline
2
端点
52 min ago
最近验证
100%
可用率 / 7 天
563ms
p50 延迟
1
自动修复 / 累计
GET/fares5 credits per call

票价

两地之间的票价窗口:出发地、目的地、窗口的日期范围、其中最便宜的一天及其价格,以及 `days`(按日期排序的有价日期列表,每天包含最便宜的单程票价和最便宜车次的发车时间)。Trainline 没有票价数据的日期可能缺失,或返回时 date 和 cheapest_price 为 null。

Bearer auth. Pass your key as Authorization: Bearer <key> on every request.
stringOptionalin query

origin, a place or station name (best match is used, as in /locations) or a Trainline urn code

stringOptionalin query

destination, a place or station name or a Trainline urn code

stringOptionalin query

first day of the window, YYYY-MM-DD (UTC); defaults to today

integerOptionalin query

number of days to price starting at `date`, 1-14

stringOptionalin query

currency of the fares (GBP, EUR, USD, CHF, DKK, SEK or NOK)

originobject2 fields
namestringe.g."London"
codestringe.g."urn:trainline:generic:loc:182gb"
destinationobject2 fields
namestringe.g."Manchester Piccadilly"
codestringe.g."urn:trainline:generic:loc:MAN2968gb"
currencystringe.g."GBP"
journey_typestringe.g."single"
date_fromstringe.g."2026-11-27"
date_tostringe.g."2026-11-29"
cheapest_pricenumbere.g.21.9
cheapest_datestringe.g."2026-11-28"
daysobject[]4 fields
datestringe.g."2026-11-27"
cheapest_priceintegere.g.36
currencystringe.g."GBP"
departuresstring[]
paginationobject3 fields
countintegere.g.3
has_morebooleane.g.false
next_cursornull
fetched_atstringe.g."2026-11-27T10:16:44.709Z"
ageintegere.g.0
Try itLive
GET
https://api.maviapi.com/v1/sites/thetrainline/fares?from=london&to=manchester&days=3&currency=GBP
Request
curl "https://api.maviapi.com/v1/sites/thetrainline/fares?from=london&to=manchester&days=3&currency=GBP" \
-H "Authorization: Bearer $MAVI_KEY"
Example681 B
{
  "origin": {
    "name": "London",
    "code": "urn:trainline:generic:loc:182gb"
  },
  "destination": {
    "name": "Manchester Piccadilly",
    "code": "urn:trainline:generic:loc:MAN2968gb"
  },
  "currency": "GBP",
  "journey_type": "single",
  "date_from": "2026-11-27",
  "date_to": "2026-11-29",
  "cheapest_price": 21.9,
  "cheapest_date": "2026-11-28",
  "days": [
    {
      "date": "2026-11-27",
      "cheapest_price": 36,
      "currency": "GBP",
      "departures": [
        "2026-11-27T19:46:00.000Z"
      ]
    }
  ],
  "pagination": {
    "count": 3,
    "has_more": false,
    "next_cursor": null
  },
  "fetched_at": "2026-11-27T10:16:44.709Z",
  "age": 0
}

Get all the data, no code

Collect every page of GET /fares into a dataset, for one request or for a whole list of them, then download it as CSV or JSON. A page costs the same as one call of the endpoint; the download is free.

Use Trainline API from Claude, ChatGPT and your coding tools

Add this URL as a custom connector and sign in to maviapi. Nothing to install.

How to connect
https://mcp.maviapi.com/mcp

Or run it locally, with each endpoint of Trainline API as its own tool:

claude mcp add maviapi -e MAVIAPI_TOOLS=thetrainline -- npx -y '@maviapi/mcp'

Import Trainline API into Postman and other tools

How to import

Paste a link into Postman, Insomnia, an agent framework or a no-code tool to get every endpoint with its parameters and response fields. The link always serves the current endpoints, so importing it again picks up new ones.

  • OpenAPI 3.1
    https://api.maviapi.com/v1/apis/thetrainline/openapi.json
  • Postman
    https://api.maviapi.com/v1/apis/thetrainline/postman.json

The Postman collection sends the shared demo key until you put your own key in its maviapi_key variable.

使用 Trainline API 进行开发

Trainline 销售英国和欧洲各地的火车票。/fares 回答的是行程灵活的旅客会问的问题:接下来的哪一天这条线路最便宜?它接收两个地名,找到对应的车站,并在最长 14 天的范围内返回每天的最低单程票价、对应的发车时间,以及整体最便宜的一天。

可以用它做什么

票价日历
用 days=14 调用 /fares,每天画一根柱:每天都已带有当天的最低价。
低价票提醒
每天检查一条线路,当 cheapest_price 低于您愿意支付的价格时发出通知。
火车与飞机比价
把某天最便宜的火车与 Kiwi.com 或 FlightList API 中最便宜的航班放在一起比较。

调用前须知

  • 票价均为单程。往返请分别查询两个方向的价格。
  • 价格可以是 GBP、EUR、USD、CHF、DKK、SEK 或 NOK:请传入 currency。
  • from 和 to 接收名称或 /locations 返回的车站代码。一个城市有多个车站时请使用代码。

选择一个端点开始

  • 票价 GET /fares

    两地之间的票价窗口:出发地、目的地、窗口的日期范围、其中最便宜的一天及其价格,以及 `days`(按日期排序的有价日期列表,每天包含最便宜的单程票价和最便宜车次的发车时间)。Trainline 没有票价数据的日期可能缺失,或返回时 date 和 cheapest_price 为 null。

    GET /fares 的参数和响应字段
    请求参数
    名称类型 / 位置必填说明
    fromstring / query否origin, a place or station name (best match is used, as in /locations) or a Trainline urn code
    tostring / query否destination, a place or station name or a Trainline urn code
    datestring / query否first day of the window, YYYY-MM-DD (UTC); defaults to today
    daysinteger / query否number of days to price starting at `date`, 1-14
    currencystring / query否currency of the fares (GBP, EUR, USD, CHF, DKK, SEK or NOK) 可选值: GBP, EUR, USD, CHF, DKK, SEK, NOK.
    响应字段
    字段类型说明
    originobject
    origin.namestring
    origin.codestring
    destinationobject
    destination.namestring
    destination.codestring
    currencystring
    journey_typestring
    date_fromstring
    date_tostring
    cheapest_pricenumber
    cheapest_datestring
    daysobject[]
    days[].datestring
    days[].cheapest_priceinteger
    days[].currencystring
    days[].departuresstring[]
    paginationobject
    pagination.countinteger
    pagination.has_moreboolean
    pagination.next_cursornull
    fetched_atstring
    ageinteger
  • 地点 GET /locations

    与名称匹配的 Trainline 车站和地点,最佳匹配排在最前,并附带 /fares 接受的 urn 代码。

    GET /locations 的参数和响应字段
    请求参数
    名称类型 / 位置必填说明
    qstring / query否search term, a station or place name (e.g. "london", "paris nord")
    响应字段
    字段类型说明
    querystring
    locationsobject[]
    locations[].namestring
    locations[].codestring
    locations[].typestring
    locations[].countrystring
    locations[].subtitlestring
    paginationobject
    pagination.countinteger
    pagination.has_moreboolean
    pagination.next_cursornull
    fetched_atstring
    ageinteger

将 Trainline 数据导出为 CSV

下面的每个列表都可以下载为 CSV 文件,每条记录一行。用上方控制台下面的 "Get all the data, no code" 卡片把列表收集到数据集中。数据集会逐页读取列表,之后可下载为 CSV、JSON 或 JSONL。

列表CSV 列
票价GET /faresdate, cheapest_price, currency, departures
地点GET /locationsname, code, type, country, subtitle

嵌套的值(列表或对象)以 JSON 写入同一个单元格。 数据集和导出如何工作

Trainline API 常见问题

能获取什么数据、费用多少、如何调用。

Trainline 有官方 API 吗?

有,Trainline 提供自己的 API:Trainline Global API(核实日期:2026-11-25)。本页的 Trainline API 与它无关,是非官方 API:它是 maviapi 针对 thetrainline.com 上的公开数据提供的独立托管 API,并非由 Trainline 开发、认可或支持。

Trainline API 可以获取哪些数据?

Trainline API 有 2 个端点。每个端点都返回 JSON,参数和字段见上方的文档:

  • GET /fares: 两地之间的票价窗口:出发地、目的地、窗口的日期范围、其中最便宜的一天及其价格,以及 `days`(按日期排序的有价日期列表,每天包含最便宜的单程票价和最便宜车次的发车时间)。
  • GET /locations: 与名称匹配的 Trainline 车站和地点,最佳匹配排在最前,并附带 /fares 接受的 urn 代码。

Trainline API 的价格是多少?

调用用您 maviapi 套餐中的积分支付,只有成功的调用才会计费。此 API 的每个端点每次调用消耗 5 个积分。在 Hobby 套餐下(每月 $49,含 250,000 个积分),1,000 次调用约 $0.98。失败的调用不收费。Free 套餐每月提供 1,000 个积分,无需信用卡。超出部分请查看套餐和积分价格。

如何调用 Trainline API?

向端点发送 HTTPS 请求,并在 Authorization 请求头中带上您的 API 密钥。例如 GET /fares:

cURL
curl 'https://api.maviapi.com/v1/sites/thetrainline/fares' \
  -H "Authorization: Bearer $MAVI_KEY"

在您的 API 密钥页面创建密钥,或者先在上方的控制台中用免费测试密钥试用任意端点。

以 JSON 获取 Trainline 数据有哪些方式?

以 JSON 获取 Trainline 数据的每种方式适合不同的需求:

  • Trainline Global API: 需要写入权限、账号数据或与 Trainline 签订协议时更合适。
  • 自己写爬虫: 完全由自己掌控,不依赖任何服务商,但解析、被拦截的请求以及 thetrainline.com 每次改版后的修复都要自己负责。更适合从很少变化的页面一次性获取数据。
  • Apify: 一个 Actor 市场,Actor 由您自己运行,输入、输出和价格由各个 Actor 决定。当已有 Actor 能返回您需要的数据,或者您想托管自己的代码时更合适。参见maviapi 与 Apify 的对比。
  • RapidAPI: 一个汇集了众多提供方 API 的平台,用一个 RapidAPI 密钥调用,每个 API 单独定价。当您已经在使用该平台,并且其中某个 API 能满足需求时更合适。来源:RapidAPI docs。
  • maviapi: 本页的 Trainline API:2 个带文档的端点、公开的响应结构,以及一个可用于整个目录的密钥。当这些数据是您产品的一部分,而您不想维护爬虫时更合适。

thetrainline.com 改版后会怎样?

您的代码不需要改动。thetrainline.com 修改页面时,maviapi 会让 Trainline API 保持可用,您继续调用同样的端点,并得到本页文档所述结构的 JSON。它当前的运行状况见状态页,变更记录见它的更新日志。

更新会破坏我的 Trainline API 集成吗?

不会。您正在使用的端点,其结构永远不会改变:新的字段、可选参数和端点只会加在旁边,会破坏调用的变更则作为新端点发布。请忽略您不认识的响应字段,新内容请关注更新日志。

  • Tripadvisor API

    来自 Tripadvisor 的行程规划数据:支持地点搜索,并可按地点(geo_id)获取景点、餐厅和酒店,包含评分、评论、价格和坐标。先用 /search 解析地名,再通过…

  • Kiwi.com API

    搜索并比较 Kiwi.com 上的航班:返回带实时价格、航空公司、航线和起降时间的行程,还可查询地点和航线。

  • GetYourGuide API

    GetYourGuide 的游览和活动列表:价格、评分、可订日期和目的地,另有单个活动的详情,并可通过站点地图批量发现活动。

  • Booking.com API

    按地点和入住日期搜索 Booking.com 的住宿,查看住宿详情、价格和住客评分。

  • Airbnb API

    按地点和日期搜索 Airbnb 短租房源,并获取房源详情:数据取自 Airbnb 服务端渲染的搜索页和房源页,以结构清晰的 JSON 返回。

  • FlightList API

    在任意日期范围内搜索低价单程航班:票价、航班时刻、航空公司和每个航段的航班号,另有机场和 IATA 代码查询。与 flightlist.io 的搜索功能相对应。