Installations
npm install trip-ticket-client
Developer Guide
Typescript
No
Module System
ESM
Min. Node Version
>=16
Node Version
18.19.0
NPM Version
10.2.3
Score
54.3
Supply Chain
92.9
Quality
74.1
Maintenance
100
Vulnerability
97.9
License
Releases
Unable to fetch releases
Total Downloads
Cumulative downloads
Total Downloads
131
Last day
0%
1
Compared to previous day
Last week
0%
2
Compared to previous week
Last month
-10%
9
Compared to previous month
Last year
0%
131
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
15
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
1npm 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()
:
1import {createClient} from 'hafas-client' 2import {profile as dbProfile} from 'hafas-client/p/db/index.js' 3 4// Adapt this to your project! createClient() won't work with this string. 5const userAgent = 'link-to-your-project-or-email' 6 7// create a client with the Deutsche Bahn profile 8const client = createClient(dbProfile, userAgent)
You can now use client
to query the HAFAS endpoint configured in the db
profile:
1// Berlin Jungfernheide to München Hbf 2const res = await client.journeys('8011167', '8000261', {results: 1}) 3console.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
.
1{ 2 journeys: [ { 3 origin: { 4 type: 'station', 5 id: '8089100', 6 name: 'Berlin Jungfernheide (S)', 7 location: { /* … */ }, 8 products: { /* … */ } 9 }, 10 departure: '2017-12-19T17:05:30+01:00', 11 plannedDeparture: '2017-12-19T17:05:00+01:00', 12 departureDelay: 30, 13 departurePlatform: '5', 14 plannedDeparturePlatform: '5', 15 16 destination: { 17 type: 'station', 18 id: '8000261', 19 name: 'München Hbf', 20 location: { /* … */ }, 21 products: { /* … */ } 22 }, 23 arrival: '2017-12-19T22:44:00+01:00', 24 plannedArrival: '2017-12-19T22:45:00+01:00', 25 arrivalDelay: -60, 26 arrivalPlatform: '11A', 27 plannedArrivalPlatform: '13', 28 29 legs: [ { 30 id: '1|100067|48|81|17122017', 31 line: { 32 type: 'line', 33 id: '41172', 34 name: 'S 41', 35 public: true, 36 mode: 'train', 37 product: 'suburban', 38 operator: { 39 type: 'operator', 40 id: 's-bahn-berlin-gmbh', 41 name: 'S-Bahn Berlin GmbH' 42 } 43 }, 44 direction: 'Ringbahn ->', 45 46 origin: { 47 type: 'station', 48 id: '8089100', 49 name: 'Berlin Jungfernheide (S)', 50 location: { 51 type: 'location', 52 latitude: 52.530291, 53 longitude: 13.299451 54 }, 55 products: { /* … */ } 56 }, 57 departure: '2017-12-19T17:05:30+01:00', 58 plannedDeparture: '2017-12-19T17:05:00+01:00', 59 departureDelay: 30, 60 departurePlatform: '5', 61 plannedDeparturePlatform: '5', 62 63 destination: { 64 type: 'station', 65 id: '8089118', 66 name: 'Berlin Beusselstraße' 67 // … 68 }, 69 arrival: '2017-12-19T17:08:00+01:00', 70 plannedArrival: '2017-12-19T17:08:00+01:00', 71 arrivalDelay: null, 72 arrivalPlatform: '2a-b', 73 plannedArrivalPlatform: '1' 74 }, 75 /* more legs… */ 76 { 77 walking: true, 78 public: true, 79 80 origin: { 81 type: 'station', 82 id: '730749', 83 name: 'Berlin Hauptbahnhof (S+U), Berlin' 84 // … 85 }, 86 plannedDeparture: '2017-12-19T17:25:00+01:00', 87 prognosedDeparture: null, 88 departureDelay: null, 89 90 destination: { 91 type: 'station', 92 id: '8098160', 93 name: 'Berlin Hbf (tief)' 94 // … 95 }, 96 arrival: '2017-12-19T17:33:00+01:00', 97 plannedArrival: '2017-12-19T17:33:00+01:00', 98 arrivalDelay: null 99 }, { 100 id: '1|70906|0|81|17122017', 101 line: { /* … */ }, 102 direction: 'München Hbf', 103 104 origin: { 105 type: 'station', 106 id: '8098160', 107 name: 'Berlin Hbf (tief)' 108 // … 109 }, 110 departure: '2017-12-19T17:35:00+01:00', 111 plannedDeparture: '2017-12-19T17:37:00+01:00', 112 departureDelay: -120, 113 departurePlatform: '1', 114 plannedDeparturePlatform: '1', 115 116 destination: { 117 type: 'station', 118 id: '8000261', 119 name: 'München Hbf', 120 // … 121 }, 122 arrival: '2017-12-19T22:44:00+01:00', 123 plannedArrival: '2017-12-19T22:45:00+01:00', 124 arrivalDelay: -60, 125 arrivalPlatform: '11A', 126 plannedArrivalPlatform: '13' 127 } ], 128 price: { 129 amount: null, 130 hint: 'No pricing information available.' 131 } 132 // … 133 } ] 134 // … 135}
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.
No vulnerabilities found.
No security vulnerabilities found.