6.3.2 • Published 2 months ago

hafas-client v6.3.2

Weekly downloads
690
License
ISC
Repository
github
Last release
2 months ago

hafas-client

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

npm version 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 endpointlibrary
Betriebsstellen & disturbances in the German rail networkdb-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.

Because the operators of the HAFAS endpoint should be able to contact you about excessive traffic, please pass a link to your project/program (or an email address) into createClient():

import {createClient} from 'hafas-client'
import {profile as dbProfile} from 'hafas-client/p/db/index.js'

// Adapt this to your project! createClient() won't work with this string.
const userAgent = 'link-to-your-project-or-email'

// create a client with the Deutsche Bahn profile
const client = createClient(dbProfile, userAgent)

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

Related Projects

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.

6.3.2

2 months ago

6.3.1

2 months ago

6.3.0

8 months ago

5.26.5

9 months ago

6.2.2

9 months ago

6.2.1

10 months ago

6.2.0

12 months ago

6.1.1

1 year ago

6.0.5

2 years ago

6.1.0

1 year ago

6.0.3

2 years ago

6.0.4

2 years ago

5.26.4

2 years ago

5.26.3

2 years ago

6.0.2

2 years ago

6.0.0-alpha.2

2 years ago

5.27.0-alpha.1

2 years ago

5.27.0-alpha.2

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

5.26.2

2 years ago

5.26.1

2 years ago

5.26.0

2 years ago

6.0.0-alpha.1

2 years ago

5.24.1

2 years ago

5.25.0

2 years ago

5.24.0

3 years ago

5.23.0

3 years ago

5.22.2

3 years ago

5.22.1

3 years ago

5.22.0

3 years ago

5.21.1

3 years ago

5.20.2

3 years ago

5.21.0

3 years ago

5.20.1

3 years ago

5.20.0

3 years ago

5.19.1

3 years ago

5.19.0

3 years ago

5.18.0

3 years ago

5.17.0

3 years ago

5.16.0

4 years ago

5.15.2

4 years ago

5.15.1

4 years ago

5.15.0

4 years ago

5.14.0

4 years ago

5.13.0

4 years ago

5.12.0

4 years ago

5.11.0

4 years ago

5.10.1

4 years ago

5.10.0

4 years ago

5.9.0

4 years ago

5.8.0

4 years ago

5.7.1

4 years ago

5.7.0

4 years ago

5.6.3

4 years ago

5.6.2

4 years ago

5.6.1

4 years ago

5.5.1

5 years ago

5.6.0

5 years ago

5.5.0

5 years ago

5.4.0

5 years ago

5.3.1

5 years ago

5.3.0

5 years ago

5.2.0

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.4

5 years ago

4.8.1

5 years ago

4.6.3

5 years ago

5.0.3

5 years ago

5.0.2

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.8.0

5 years ago

4.7.0

5 years ago

4.6.2

5 years ago

4.6.1

5 years ago

3.10.3

5 years ago

2.10.4

5 years ago

4.6.0

5 years ago

4.5.2

5 years ago

3.10.2

5 years ago

4.5.1

5 years ago

4.5.0

5 years ago

4.4.0

5 years ago

4.3.0

5 years ago

4.2.2

5 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.10.1

6 years ago

4.0.0-alpha.2

6 years ago

4.0.0-alpha.1

6 years ago

3.10.0

6 years ago

3.9.1

6 years ago

3.9.0

6 years ago

3.8.1

6 years ago

3.8.0

6 years ago

3.7.0

6 years ago

3.6.2

6 years ago

3.6.1

6 years ago

3.5.0

6 years ago

3.4.3

6 years ago

3.4.2

6 years ago

3.4.1

6 years ago

3.4.0

6 years ago

3.3.1

6 years ago

3.3.0

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.10.3

6 years ago

3.0.0-alpha.21

6 years ago

3.0.0-alpha.20

6 years ago

2.10.2

6 years ago

3.0.0-alpha.19

6 years ago

3.0.0-alpha.18

6 years ago

3.0.0-alpha.17

6 years ago

3.0.0-alpha.16

6 years ago

2.10.1

6 years ago

2.10.0

6 years ago

2.9.1

6 years ago

3.0.0-alpha.15

6 years ago

3.0.0-alpha.14

6 years ago

3.0.0-alpha.13

6 years ago

3.0.0-alpha.12

6 years ago

3.0.0-alpha.11

6 years ago

3.0.0-alpha.10

6 years ago

3.0.0-alpha.9

6 years ago

3.0.0-alpha.8

6 years ago

2.9.0

6 years ago

3.0.0-alpha.7

6 years ago

2.8.1

6 years ago

3.0.0-alpha.6

6 years ago

3.0.0-alpha.5

7 years ago

3.0.0-alpha.4

7 years ago

2.8.0

7 years ago

2.7.5

7 years ago

2.7.4

7 years ago

2.7.3

7 years ago

3.0.0-alpha.3

7 years ago

2.7.2

7 years ago

2.7.1

7 years ago

2.7.0

7 years ago

2.6.0

7 years ago

2.5.3

7 years ago

2.5.2

7 years ago

3.0.0-alpha.2

7 years ago

2.5.1

7 years ago

3.0.0-alpha.1

7 years ago

2.5.0

7 years ago

2.4.2

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.4

7 years ago

2.3.3

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

2.0.0-alpha.8

7 years ago

2.0.0-alpha.7

7 years ago

2.0.0-alpha.6

7 years ago

2.0.0-alpha.5

7 years ago

1.3.1

7 years ago

2.0.0-alpha.3

7 years ago

2.0.0-alpha.2

7 years ago

2.0.0-alpha.1

7 years ago

1.3.0

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.7.0

8 years ago

0.6.2

8 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.0

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago