Find the cheapest train day from London with one curl call
Trainline sells rail tickets across the UK and Europe, and it does have an official API: the Global API from Trainline Partner Solutions, built for travel sellers. If you are one, start there. If you are building a side
Trainline sells rail tickets across the UK and Europe, and it does have an official API: the Global API from Trainline Partner Solutions, built for travel sellers. If you are one, start there. If you are building a side project, a travel bot or an agent that just needs to know "which day next week is cheapest from London to Edinburgh", the fares are on the website and the partner API is the wrong size.
This tutorial gets those fares as JSON with one HTTP call to the unofficial Trainline API on maviapi. maviapi gives websites hosted JSON APIs with documented endpoints and a published response schema. It is an independent integration, not affiliated with Trainline.
1. Get a key
Sign up and create a key at https://maviapi.com/dashboard/keys. The Free plan includes 1,000 credits a month, with no card. Both endpoints used below cost 5 credits per successful call, and failed requests are free. Every answer tells you what it cost in the x-maviapi-credits header.
Put the key in your shell:
export MAVI_KEY=YOUR_KEY
2. Resolve a station
The fares endpoint accepts a plain place name and uses the best match, but it helps to see what a name resolves to. /locations searches Trainline's stations:
curl -s 'https://api.maviapi.com/v1/sites/thetrainline/locations?q=edinburgh&limit=3' \
-H "Authorization: Bearer $MAVI_KEY"
{
"data": {
"query": "edinburgh",
"locations": [
{ "name": "Edinburgh (Waverley)", "code": "urn:trainline:generic:loc:EDB9328gb", "type": "station", "country": "GB", "subtitle": "City centre, Main" },
{ "name": "Haymarket", "code": "urn:trainline:generic:loc:HYM9419gb", "type": "station", "country": "GB", "subtitle": "City centre, West End" },
{ "name": "Edinburgh Park", "code": "urn:trainline:generic:loc:EDP9395gb", "type": "station", "country": "GB", "subtitle": "Business park, near M8" }
]
},
"cached": false,
"source": "thetrainline:locations-search",
"pagination": { "total": 10, "limit": 3, "offset": 0, "count": 3, "has_more": true }
}
You can pass either the name (edinburgh) or the code to the fares endpoint. Use the code when a name is ambiguous and you want one exact station.
3. Price a window of days
/fares takes an origin, a destination, a first day and a number of days (1 to 14). For each day it returns the cheapest single (one-way) fare and the departure time of the cheapest train:
curl -s 'https://api.maviapi.com/v1/sites/thetrainline/fares?from=london&to=edinburgh&date=2026-12-07&days=5' \
-H "Authorization: Bearer $MAVI_KEY"
This is what came back when we ran it on 28 November 2026:
{
"data": {
"origin": { "name": "London", "code": "urn:trainline:generic:loc:182gb" },
"destination": { "name": "Edinburgh (Waverley)", "code": "urn:trainline:generic:loc:EDB9328gb" },
"currency": "GBP",
"journey_type": "single",
"date_from": "2026-12-07",
"date_to": "2026-12-11",
"cheapest_price": 27.4,
"cheapest_date": "2026-12-08",
"days": [
{ "date": "2026-12-07", "cheapest_price": 29.2, "currency": "GBP", "departures": ["2026-12-07T22:03:00.000Z"] },
{ "date": "2026-12-08", "cheapest_price": 27.4, "currency": "GBP", "departures": ["2026-12-08T22:03:00.000Z"] },
{ "date": "2026-12-09", "cheapest_price": 27.4, "currency": "GBP", "departures": ["2026-12-09T22:03:00.000Z"] },
{ "date": "2026-12-10", "cheapest_price": 27.4, "currency": "GBP", "departures": ["2026-12-10T22:03:00.000Z"] },
{ "date": "2026-12-11", "cheapest_price": 33.9, "currency": "GBP", "departures": ["2026-12-11T05:45:00.000Z"] }
]
},
"cached": false,
"source": "thetrainline:graphql",
"pagination": { "total": 5, "limit": 50, "offset": 0, "count": 5, "has_more": false }
}
Your prices will differ, since these are live fares. A few things worth knowing about the shape:
-
cheapest_priceandcheapest_dateat the top already answer "which day", so you do not have to sortdaysyourself. - Departure times are ISO timestamps in UTC. Convert them before you show them to someone in another time zone.
-
datedefaults to today, anddaysto 3. - A day Trainline holds no fare for may be missing, or come back with
dateandcheapest_priceset tonull. Guard for that before doing arithmetic. -
currencytakes GBP, EUR, USD, CHF, DKK, SEK or NOK.
4. Turn it into something readable
With jq, one line per day:
curl -s 'https://api.maviapi.com/v1/sites/thetrainline/fares?from=london&to=edinburgh&date=2026-12-07&days=5' \
-H "Authorization: Bearer $MAVI_KEY" \
| jq -r '.data.days[] | "\(.date) £\(.cheapest_price) \(.departures[0] // "-")"'
2026-12-07 £29.2 2026-12-07T22:03:00.000Z
2026-12-08 £27.4 2026-12-08T22:03:00.000Z
2026-12-09 £27.4 2026-12-09T22:03:00.000Z
2026-12-10 £27.4 2026-12-10T22:03:00.000Z
2026-12-11 £33.9 2026-12-11T05:45:00.000Z
5. Compare destinations
Now the question a weekend planner actually asks: from London, where is cheapest next week, and on which day? Loop over a few destinations and keep only the summary:
for to in york manchester edinburgh; do
curl -s -G 'https://api.maviapi.com/v1/sites/thetrainline/fares' \
--data-urlencode from=london \
--data-urlencode "to=$to" \
--data-urlencode date=2026-12-07 \
--data-urlencode days=7 \
-H "Authorization: Bearer $MAVI_KEY" \
| jq -r '.data | "\(.destination.name): £\(.cheapest_price) on \(.cheapest_date)"'
done
York: £14 on 2026-12-07
Manchester Piccadilly: £14 on 2026-12-09
Edinburgh (Waverley): £27.4 on 2026-12-08
That is three calls and 15 credits. Drop it in a cron job and post the result to Slack, or feed it to a page that shows the cheapest day for each route.
What happens when Trainline changes its site
This is the part a scraper makes you own. maviapi runs health checks against every API, and when an extraction fails, a repair build goes out after it passes a live request. Endpoints only grow: a field you read today keeps its name and type, and new fields are added beside it. Every change is listed in the API's public changelog. Two headers help when you care about freshness: x-maviapi-fetched-at says when the data was read from the site, and Age gives the same in seconds.
From an AI agent
The same endpoints are available as MCP tools. To try them in Claude Code without an account:
claude mcp add --transport http maviapi https://mcp.maviapi.com/demo
Then ask "cheapest train from London to York next week" and the agent calls /fares instead of opening the booking page.
The full endpoint reference, with a playground you can run in the browser, is at https://maviapi.com/apis/thetrainline. If the site you need is not in the catalog, you can request it there too: paste its URL and describe the data, and you get a tested draft API to review before it goes live.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.