npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hafas-client

v6.3.0

Published

Client for HAFAS public transport APIs.

Downloads

1,113

Readme

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 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.

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.