JavaScript client for the Deutsche Bahn HAFAS API.
Clone or download

readme.md

db-hafas

A client for the German Railways (DB). It acts as a consistent and straightforward interface on top of a verbose API.

This project is actually a thin wrapper around hafas-client. Its docs document the API in general.

db-rest architecture diagram

npm version dependency status ISC-licensed chat on gitter support me on Patreon

Installing

npm install db-hafas

API

Check the docs for hafas-client as well as its DB-specific customisations.

Getting Started

const createHafas = require('db-hafas')

const hafas = createHafas('my-awesome-program')

As an example, we will search for a route from Berlin Jungfernheide to München Hbf. To get the station IDs, use locations(query, [opt]).

hafas.journeys('8011167', '8000261', {results: 1})
.then((journeys) => console.log(journeys[0]))
.catch(console.error)

The output will be an array of journey objects in the Friendly Public Transport Format 1.0.1 format:

[ {
	legs: [ {
		id: '1|98397|0|81|26122017',
		origin: {
			type: 'station',
			id: '8011167',
			name: 'Berlin Jungfernheide',
			location: {
				type: 'location',
				latitude: 52.530408,
				longitude: 13.299424
			},
			products: {
				nationalExp: false,
				national: false,
				regionalExp: false,
				regional: true,
				suburban: true,
				bus: true,
				ferry: false,
				subway: true,
				tram: false,
				taxi: false
			}
		},
		departure: '2017-12-26T05:57:00.000+01:00', // ISO 8601 string
		departurePlatform: '3',
		delay: 0, // in seconds
		destination: {
			type: 'station',
			id: '8098160',
			name: 'Berlin Hbf (tief)',
			location: {
				type: 'location',
				latitude: 52.52585,
				longitude: 13.368883
			},
			products: {
				nationalExp: true,
				national: true,
				regionalExp: true,
				regional: true,
				suburban: true,
				bus: true,
				ferry: false,
				subway: true,
				tram: true,
				taxi: false
			}
		},
		arrival: '2017-12-26T06:01:00.000+01:00', // ISO 8601 string
		arrivalPlatform: '4',
		line: {
			type: 'line',
			id: '56552',
			name: 'RE 56552',
			public: true,
			mode: 'train',
			product: 'regional',
			class: 8,
			productCode: 3,
			operator: {
				type: 'operator',
				id: 'db-regio-ag-nordost',
				name: 'DB Regio AG Nordost'
			}
		},
		direction: 'Ludwigsfelde'
	}, {
		id: '1|72980|0|81|26122017',
		origin: {
			type: 'station',
			id: '8098160',
			name: 'Berlin Hbf (tief)',
			location: {
				type: 'location',
				latitude: 52.52585,
				longitude: 13.368883
			},
			products: {
				nationalExp: true,
				national: true,
				regionalExp: true,
				regional: true,
				suburban: true,
				bus: true,
				ferry: false,
				subway: true,
				tram: true,
				taxi: false
			}
		},
		departure: '2017-12-26T06:30:00.000+01:00', // ISO 8601 string
		departurePlatform: '1',
		destination: {
			type: 'station',
			id: '8000261',
			name: 'München Hbf',
			location: {
				type: 'location',
				latitude: 48.140364,
				longitude: 11.558735
			},
			products: {
				nationalExp: true,
				national: true,
				regionalExp: true,
				regional: true,
				suburban: true,
				bus: true,
				ferry: false,
				subway: true,
				tram: true,
				taxi: false
			}
		},
		arrival: '2017-12-26T11:02:00.000+01:00', // ISO 8601 string
		arrivalPlatform: '22',
		line: {
			type: 'line',
			id: '1505',
			name: 'ICE 1505',
			public: true,
			mode: 'train',
			product: 'nationalExp',
			class: 1,
			productCode: 0,
			operator: {
				type: 'operator',
				id: 'nahreisezug',
				name: 'Nahreisezug'
			}
		},
		direction: 'München Hbf'
	} ],
	// taken from the first leg
	origin: {
		type: 'station',
		id: '8011167',
		name: 'Berlin Jungfernheide',
		location: {
			type: 'location',
			latitude: 52.530408,
			longitude: 13.299424
		},
		products: {
			nationalExp: false,
			national: false,
			regionalExp: false,
			regional: true,
			suburban: true,
			bus: true,
			ferry: false,
			subway: true,
			tram: false,
			taxi: false
		}
	},
	departure: '2017-12-26T05:57:00.000+01:00', // ISO 8601 string
	// taken from the last part
	destination: {
		type: 'station',
		id: '8000261',
		name: 'München Hbf',
		location: {
			type: 'location',
			latitude: 48.140364,
			longitude: 11.558735
		},
		products: {
			nationalExp: true,
			national: true,
			regionalExp: true,
			regional: true,
			suburban: true,
			bus: true,
			ferry: false,
			subway: true,
			tram: true,
			taxi: false
		}
	},
	arrival: '2017-12-26T11:02:00.000+01:00', // ISO 8601 string
	price: {
		amount: 150, hint: null
	}
} ]

Similar Projects

  • Fahrplan.js – "A JavaScript client for Deutsche Bahn's timetable API"
  • db-prices – Find the cheapest routes using the DB Sparpreise API.
  • db-stations – An offline list of all DB stations.
  • vbb-stations – A client for the Berlin & Brandenburg public transport service (VBB).

Contributing

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