Find a file
2022-11-17 15:17:02 +01:00
.github Node 16+ 💥💚 2022-11-17 15:17:01 +01:00
docs arrivals/departures: return obj with realtimeDataUpdatedAt & results 💥📝 2022-11-17 15:17:02 +01:00
format add todos; add @MTRNord as contributor 2021-11-18 18:29:25 +01:00
lib handle METHOD_NA/NO_MATCH/PARAMETER errors 2022-11-17 15:17:02 +01:00
p VBB: remove vbb-parse-line 💥 2022-11-17 15:17:01 +01:00
parse parseWarning: {from,to}Loc -> {from,to}Location 💥 2022-11-17 15:17:01 +01:00
test arrivals/departures: return obj with realtimeDataUpdatedAt & results 💥📝 2022-11-17 15:17:02 +01:00
tools tools: highest-endpoint-version -> endpoint-hci-version 2022-02-22 20:27:08 +01:00
.editorconfig Node 6+, cleanup 2016-12-14 00:22:02 +01:00
.eslintrc.json tests: enable linter, fix code style 2022-06-18 18:33:26 +02:00
.gitattributes let Git treat E2E test fixtures as binary data 2020-07-19 21:14:56 +02:00
.gitignore minor tweaks 2020-09-10 23:57:03 +02:00
.gitmodules tools: add transport-apis submodule & pull-profile-base-data.sh 2021-01-26 17:26:25 +01:00
contributing.md document integration test workflow 📝 2020-06-15 15:39:09 +02:00
index.js arrivals/departures: return obj with realtimeDataUpdatedAt & results 💥📝 2022-11-17 15:17:02 +01:00
license.md changelog 📝; 5.24.1 2022-06-23 12:54:56 +02:00
package.json rework error handling 💥📝 2022-11-17 15:17:02 +01:00
readme.md tweak & restructure docs 📝, explicit defaults 2022-11-09 18:23:56 +01:00
retry.js throttle & retry: use default profile 🐛 2020-01-14 23:56:48 +01:00
throttle.js p-throttle@4 2021-06-28 23:34:53 +02:00

hafas-client

A client for the "mobile APIs" of HAFAS public transport management systems.

npm version build status ISC-licensed support Jannis via GitHub Sponsors chat with Jannis on Twitter

documentation

Background

A company called HaCon sells a public transport management system called HAFAS to public transport authorities and providers, mostly in Europe. It provides routing and departure information to their customers.

Most customers get their own, separate HAFAS deployments; They all use the same terminology and API calls, but have slightly different versions, configurations and sets of enabled features. Using built-in endpoint-specific customisations, hafas-client abstracts most of these differences away, and supports additional features in some cases. Check the list of supported networks/endpoints for more info.

Note: Currently, hafas-client only supports "mobile API" endpoints, which are designed for and used by the respective official mobile app(s); These endpoints almost always have mgate.exe in the URL. This library does not support "open API" endpoints (often they have rest-proxy or openapi in the URL) yet, but #134 contains work in progress.

Strictly speaking, permission is necessary to use this library with a HAFAS "mobile" endpoint. It merely tries to remove the technical barrier of accessing the data, in order to kick-start an ecosystem or apps and services that will eventually rely on openly available data.

Supported networks/endpoints

hafas-client has built-in support for many public transportation networks.

There are also libraries that use hafas-client and pass their own profile in:

HAFAS endpoint library
Betriebsstellen & disturbances in the German rail network db-netz-hafas

Installing

npm install hafas-client

with react-native

hafas-client as well its dependencies use Node-builtin modules and Node globals. To be able to use it within react-native, follow the instructions at node-libs-react-native.

Usage

Pick the profile for the HAFAS endpoint covering the area you want to get data for. Pass the profile and a descriptive name for your program into the createClient function:

const createClient = require('hafas-client')
const dbProfile = require('hafas-client/p/db')

// create a client with the Deutsche Bahn profile
const client = createClient(dbProfile, 'my-awesome-program')

You can now use client to query the HAFAS endpoint configured in the db profile:

// Berlin Jungfernheide to München Hbf
const res = await client.journeys('8011167', '8000261', {results: 1})
console.log(res)

journeys() returns a Promise that will resolve with an object with an array journeys that contains one Friendly Public Transport Format (FPTF) v2 draft journey.

{
	journeys: [ {
		origin: {
			type: 'station',
			id: '8089100',
			name: 'Berlin Jungfernheide (S)',
			location: { /* … */ },
			products: { /* … */ }
		},
		departure: '2017-12-19T17:05:30+01:00',
		plannedDeparture: '2017-12-19T17:05:00+01:00',
		departureDelay: 30,
		departurePlatform: '5',
		plannedDeparturePlatform: '5',

		destination: {
			type: 'station',
			id: '8000261',
			name: 'München Hbf',
			location: { /* … */ },
			products: { /* … */ }
		},
		arrival: '2017-12-19T22:44:00+01:00',
		plannedArrival: '2017-12-19T22:45:00+01:00',
		arrivalDelay: -60,
		arrivalPlatform: '11A',
		plannedArrivalPlatform: '13',

		legs: [ {
			id: '1|100067|48|81|17122017',
			line: {
				type: 'line',
				id: '41172',
				name: 'S 41',
				public: true,
				mode: 'train',
				product: 'suburban',
				operator: {
					type: 'operator',
					id: 's-bahn-berlin-gmbh',
					name: 'S-Bahn Berlin GmbH'
				}
			},
			direction: 'Ringbahn ->',

			origin: {
				type: 'station',
				id: '8089100',
				name: 'Berlin Jungfernheide (S)',
				location: {
					type: 'location',
					latitude: 52.530291,
					longitude: 13.299451
				},
				products: { /* … */ }
			},
			departure: '2017-12-19T17:05:30+01:00',
			plannedDeparture: '2017-12-19T17:05:00+01:00',
			departureDelay: 30,
			departurePlatform: '5',
			plannedDeparturePlatform: '5',

			destination: {
				type: 'station',
				id: '8089118',
				name: 'Berlin Beusselstraße'
				// …
			},
			arrival: '2017-12-19T17:08:00+01:00',
			plannedArrival: '2017-12-19T17:08:00+01:00',
			arrivalDelay: null,
			arrivalPlatform: '2a-b',
			plannedArrivalPlatform: '1'
		},
		/* more legs… */
		{
			walking: true,
			public: true,

			origin: {
				type: 'station',
				id: '730749',
				name: 'Berlin Hauptbahnhof (S+U), Berlin'
				// …
			},
			plannedDeparture: '2017-12-19T17:25:00+01:00',
			prognosedDeparture: null,
			departureDelay: null,

			destination: {
				type: 'station',
				id: '8098160',
				name: 'Berlin Hbf (tief)'
				// …
			},
			arrival: '2017-12-19T17:33:00+01:00',
			plannedArrival: '2017-12-19T17:33:00+01:00',
			arrivalDelay: null
		}, {
			id: '1|70906|0|81|17122017',
			line: { /* … */ },
			direction: 'München Hbf',

			origin: {
				type: 'station',
				id: '8098160',
				name: 'Berlin Hbf (tief)'
				// …
			},
			departure: '2017-12-19T17:35:00+01:00',
			plannedDeparture: '2017-12-19T17:37:00+01:00',
			departureDelay: -120,
			departurePlatform: '1',
			plannedDeparturePlatform: '1',

			destination: {
				type: 'station',
				id: '8000261',
				name: 'München Hbf',
				// …
			},
			arrival: '2017-12-19T22:44:00+01:00',
			plannedArrival: '2017-12-19T22:45:00+01:00',
			arrivalDelay: -60,
			arrivalPlatform: '11A',
			plannedArrivalPlatform: '13'
		} ],
		price: {
			amount: null,
			hint: 'No pricing information available.'
		}
		// …
	} ]
	// …
}

Each profile has more detailed example code.

in the browser

While hafas-client itself should work in the browser via a bundler like Webpack, most HAFAS API endpoints don't allow cross-origin resource sharing (CORS), so you won't be able query them (without a proxy server).

TypeScript

There are community-maintained TypeScript typings available as @types/hafas-client.

API

API documentation

More related libraries can be found via the npm package index.

Contributing

If you have a question, found a bug or want to propose a feature, please open an Issue.

This project needs help! Check the list of "help wanted" Issues.

If you're contributing code, please read the contribution guidelines.