List direct flights and trains from a city

Use the Trip Topology API to find direct flights and trains from a city and list the airports and stations they reach.

To list direct flights from a city with the API, query /v1/destinations with a city ID. It looks across all its associated stations and airports. Each row is a reachable place. Look at the row’s modes, not place.modes: the latter lists all modes at the destination, even those with no ride from this origin. There is no mode filter on this endpoint, so filter each page in your client.

This Node.js example uses the quickstart environment variables. Save it as destinations.mjs and run node destinations.mjs. It requests up to 100 entries per page and follows the cursor until there are no more results.

const url = new URL("/v1/destinations", process.env.TRIP_TOPOLOGY_URL);
url.search = new URLSearchParams({ from: "geonames:2988507", limit: "100" });
const headers = { Authorization: `Bearer ${process.env.TRIP_TOPOLOGY_KEY}` };

const matches = [];
for (;;) {
  const response = await fetch(url, { headers });
  const body = await response.json();
  if (!response.ok) throw new Error(`${response.status} ${body.code}: ${body.detail}`);

  for (const { place, modes, service_count } of body.data) {
    if (modes.includes("air") || modes.includes("rail")) {
      matches.push({ id: place.id, modes, service_count });
    }
  }
  if (!body.meta.next_cursor) break;
  url.searchParams.set("cursor", body.meta.next_cursor);
}
console.log(JSON.stringify(matches, null, 2));

Two entries from Paris’s results: Haneda by air and Lyon Part Dieu by rail.

[
  {
    "id": "HND",
    "modes": ["air"],
    "service_count": 39
  },
  {
    "id": "StopArea:OCE87723197",
    "modes": ["rail"],
    "service_count": 178
  }
]

A reachable airport is not necessarily a flight destination. The Paris response also lists AMS with both air and rail. service_count counts distinct recurring patterns, not flights or trains per day. To inspect the schedule for a result, call /v1/connections with the same origin and the result’s place.id as to.