noerw / hafas-client

JavaScript client for HAFAS public transport APIs.

Home Page:https://github.com/public-transport/hafas-client#hafas-client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hafas-client

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

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

Background

A company called HaCon sells a public transport management system called HAFAS. It is used by public transport providers across Europe to provide 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 endpoint-specific customisations that we call profiles, hafas-client abstracts most of these differences away, and supports additional features in some cases. Check the supported networks/endpoints list 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 hafas-client 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 would eventually rely on openly available data.

Installing

npm install hafas-client

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 a HAFAS endpoint that covers the area you want to get data for. Pass the profile and an expressive name for your program into the createClient entry point of this library:

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 the client object to query the HAFAS endpoint configured in the db profile:

// 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 Friendly Public Transport Format (FPTF) 1.2.1 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 of the profiles has more detailed example code.

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
Polskie Koleje Państwowe (PKP) pkp-hafas docs example code src
Belgian National Railways (SNCB/NMBS) - docs example code src
Iarnród Éireann (Irish Rail) - 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
Mobilitéitszentral (Luxembourg) - 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
Rhein-Main-Verkehrsverbund (RMV) - 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
Verkehrsverbund Süd-Niedersachsen (VSN) - docs example code src
Ingolstädter Verkehrsgesellschaft (INVG) - docs example code src
Verkehrsverbund Bremen/Niedersachsen (VBN) - docs example code src
Rostocker Straßenbahn AG (RSAG) - docs example code src
Verkehrsverbund Mittelthüringen (VMT) - docs example code src
Salzburg public transport (SVV) - docs example code src
Zürich public transport (ZVV) - 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

Related

Contributing

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

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

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

About

JavaScript client for HAFAS public transport APIs.

https://github.com/public-transport/hafas-client#hafas-client

License:ISC License


Languages

Language:JavaScript 100.0%