Find a file
2020-01-05 17:55:49 +01:00
docs adapt docs to c2dc874 📝 2020-01-05 17:55:49 +01:00
format fix bitmask handling 🐛 2019-06-24 18:37:39 +02:00
lib rename findIdxRefs -> findInTree, remove resolveIdxRefs abstraction 2020-01-05 17:51:27 +01:00
p profile-based whitelist for walkingSpeed 2020-01-05 17:50:56 +01:00
parse parseWhen, parsePlatform: let when & platform fall back to plan data 2020-01-05 17:55:37 +01:00
test adapt tests to c2dc874 2020-01-05 17:55:37 +01:00
tools/debug-cli move debug-cli into tools dir 2019-08-16 17:57:22 +02:00
.appveyor.yml Appveyor Windows CI 💚 2018-08-23 21:29:39 +02:00
.editorconfig Node 6+, cleanup 2016-12-14 00:22:02 +01:00
.gitignore generate & send install-unique IDs 2018-07-26 19:04:00 +02:00
.travis.yml require Node >=8.3.0 💥 💚 2019-02-28 16:44:09 +01:00
index.js createClient(): request parameter -> opt parameter 💥 2020-01-05 17:52:17 +01:00
license.md pass line name, Gitter badge, 0.6.2 2017-02-27 16:51:19 +01:00
package.json upgrade deps & dev deps 2020-01-05 17:51:27 +01:00
readme.md adapt docs to c2dc874 📝 2020-01-05 17:55:49 +01:00
retry.js createClient(): request parameter -> opt parameter 💥 2020-01-05 17:52:17 +01:00
throttle.js createClient(): request parameter -> opt parameter 💥 2020-01-05 17:52:17 +01:00

hafas-client

A client for HAFAS public transport APIs. Sort of like public-transport-enabler, but with a smaller scope. It contains customisations for several public transportation networks

npm version build status ISC-licensed chat on gitter support me on Patreon

Background

There's a company called HaCon that sells a public transport management system called HAFAS. It is used by companies all over Europe to serve routing and departure information for apps. All those endpoints are similar, with the same terms and API routes, but have slightly different options, filters and sets of enabled features.

hafas-client contains all logic for communicating with these, as well as serialising from and parsing to Friendly Public Transport Format (FPTF) 1.2.0. Endpoint-specific customisations (called profiles here) increase the quality of the returned data.

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

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')

// Berlin Jungfernheide to München Hbf
client.journeys('8011167', '8000261', {results: 1})
.then(console.log).catch(console.error)

journeys() returns a Promise that will resolve with an object with an array journeys that contains one FPTF 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.'
		}
		/* … */
	} ]
	/* … */
}

API

API documentation

supported networks/endpoints

hafas-client has built-in customisations (called "profiles") for these public transportation networks:

HAFAS endpoint wrapper library docs example code source code
Deutsche Bahn (DB) db-hafas docs example code src
Berlin & Brandenburg public transport (VBB) vbb-hafas docs example code src
Berlin public transport (BVG) bvg-hafas docs example code src
Österreichische Bundesbahnen (ÖBB) oebb-hafas docs example code src
Nahverkehr Sachsen-Anhalt (NASA)/INSA insa-hafas docs example code src
Nahverkehrsverbund Schleswig-Holstein (NAH.SH) nahsh-hafas docs example code src
Austin, Texas (CMTA/CapMetro) - docs example code src
S-Bahn München - docs example code src
Saarfahrplan/VGS (Saarland) - docs example code src
Société Nationale des Chemins de Fer Luxembourgeois (CFL) - docs example code src
Hamburg public transport (HVV) - docs example code src
Nordhessischer Verkehrsverbund (NVV) (Hesse) - docs example code src
DB Busradar NRW (DB Regio Bus) - docs example code src

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

Contributing

If you have a question, found a bug or want to propose a feature, have a look at the issues page.