trip-ticket-client
v6.3.2
Published
Client for HAFAS public transport APIs.
Downloads
11
Maintainers
Readme
hafas-client
A client for the "mobile APIs" of HAFAS public transport management systems.
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
Related Projects
*.transport.rest
– Public APIs wrapping some HAFAS endpoints.BahnhofsAbfahrten
a.k.a.bahn.expert
– A very detailed public transport website for Germany. Uses HAFAS underneath, has an API.public-transport-enabler
– Java equivalent tohafas-client
, with support for more types of public transport APIs; Used by Öffi & Transportr.TripKit
– Swift equivalent tohafas-client
, with support for more types of public transport APIs; Used by ÖPNV Navigator.kpublictransport
– C++ equivalent tohafas-client
, with support for more types of public transport APIs; Used by KDE Itinerary.pyhafas
– Python equivalent tohafas-client
, with support for more types of public transport APIs.hafas-rs
– Rust equivalent tohafas-client
.fshafas
– F# port ofhafas-client
.hafas-client-php
– PHP port ofhafas-client
.- Transit – Kotlin client for mutliple kinds of public transport APIs, with support for HAFAS APIs.
nimhafas
– Nim HAFAS client (no longer maintained).- Travel-Status-DE-HAFAS – Perl HAFAS client, rather minimal.
- Friendly Public Transport Format – A format for APIs, libraries and datasets containing and working with public transport data.
observe-hafas-client
– Observe all departures/arrivals/etc. returned byhafas-client
.cached-hafas-client
– Pass in ahafas-client
instance, cache data from it.hafas-client-rpc
– Make JSON-RPC calls tohafas-client
via WebSockets & stdio.hafas-client-health-check
– Check if ahafas-client
instance and its endpoint work.hafas-rest-api
– Expose a HAFAS client via an HTTP REST API.- List of european long-distance transport operators, available API endpoints, GTFS feeds and client modules.
- Collection of european transport JavaScript modules.
hafas-find-stations
– Given a HAFAS client, find all stations in a bounding box.hafas-collect-departures-at
– Utility to collect departures, using any HAFAS client.find-hafas-data-in-another-hafas
– Find data from one HAFAS endpoint in the data of another HAFAS endpoint.hafas-gtfs-rt-feed
– Generate a GTFS Realtime (GTFS-RT) feed by polling a HAFAS endpoint.hafas-monitor-trips
– Using a HAFAS client, watch all trips in a bounding box.hafas-monitor-departures
– Pass in a HAFAS client, fetch all departures at any set of stations.hafas-record-delays
– Record delays fromhafas-monitor-departures
into a LevelDB.hafas-monitor-journeys
– Usehafas-client
to monitor journeys from A to B.hafas-discover-stations
– Pass in a HAFAS client, discover stations by querying departures.hafas-estimate-station-weight
– Pass in a HAFAS client, estimate the importance of a station.db-tickets
– A library to retrieve ticket information from Deutsche Bahn.
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.