2017-12-17 18:16:04 +01:00
# `journeys(from, to, [opt])`
`from` and `to` each must be in one of these formats:
```js
// a station ID, in a format compatible to the profile you use
'900000013102'
// an FPTF `station` object
{
type: 'station',
id: '900000013102',
name: 'foo station',
location: {
type: 'location',
latitude: 1.23,
longitude: 3.21
}
}
// a point of interest, which is an FPTF `location` object
{
type: 'location',
id: '123',
2019-02-07 17:47:50 +01:00
poi: true,
2017-12-17 18:16:04 +01:00
name: 'foo restaurant',
latitude: 1.23,
longitude: 3.21
}
// an address, which is an FTPF `location` object
{
type: 'location',
address: 'foo street 1',
latitude: 1.23,
longitude: 3.21
}
```
With `opt` , you can override the default options, which look like this:
```js
{
2018-05-28 20:34:24 +02:00
// Use either `departure` or `arrival` to specify a date/time.
departure: new Date(),
arrival: null,
2018-03-04 23:25:08 +01:00
earlierThan: null, // ref to get journeys earlier than the last query
laterThan: null, // ref to get journeys later than the last query
2018-09-03 15:29:06 +02:00
2018-10-29 18:36:11 +01:00
results: null, // number of journeys – `null` means "whatever HAFAS returns"
2017-12-17 18:16:04 +01:00
via: null, // let journeys pass this station
2018-06-13 20:39:33 +02:00
stopovers: false, // return stations on the way?
2019-01-23 12:51:54 +08:00
transfers: -1, // Maximum nr of transfers. Default: Let HAFAS decide.
2017-12-17 18:16:04 +01:00
transferTime: 0, // minimum time for a single transfer in minutes
accessibility: 'none', // 'none', 'partial' or 'complete'
bike: false, // only bike-friendly journeys
products: {
2018-03-13 21:45:23 +01:00
// these entries may vary from profile to profile
2017-12-17 18:16:04 +01:00
suburban: true,
subway: true,
tram: true,
bus: true,
ferry: true,
express: true,
regional: true
},
2018-04-30 12:49:33 +02:00
tickets: false, // return tickets? only available with some profiles
2018-06-25 18:33:22 +02:00
polylines: false, // return a shape for each leg?
2018-06-28 13:45:56 +02:00
remarks: true, // parse & expose hints & warnings?
2018-06-25 18:33:22 +02:00
// Consider walking to nearby stations at the beginning of a journey?
2018-11-05 14:04:48 +01:00
walkingSpeed: 'normal', // 'slow', 'normal', 'fast'
2018-07-09 12:40:38 +02:00
startWithWalking: true,
2018-07-23 20:42:22 +02:00
language: 'en', // language to get results in
scheduledDays: false // parse which days each journey is valid on
2017-12-17 18:16:04 +01:00
}
```
## Response
2018-09-22 19:24:34 +02:00
*Note:* As stated in the [*Friendly Public Transport Format* `1.2.0` ](https://github.com/public-transport/friendly-public-transport-format/tree/1.2.0 ), the returned `departure` and `arrival` times include the current delay. The `departureDelay` /`arrivalDelay` fields express how much they differ from the schedule.
2017-12-17 18:16:04 +01:00
2018-01-05 15:01:32 +01:00
As an example, we're going to use the [VBB profile ](../p/vbb ):
2017-12-17 18:16:04 +01:00
```js
const createClient = require('hafas-client')
const vbbProfile = require('hafas-client/p/vbb')
2018-07-19 21:55:03 +02:00
const client = createClient(vbbProfile, 'my-awesome-program')
2017-12-17 18:16:04 +01:00
// Hauptbahnhof to Heinrich-Heine-Str.
client.journeys('900000003201', '900000100008', {
results: 1,
2018-06-13 20:39:33 +02:00
stopovers: true
2017-12-17 18:16:04 +01:00
})
.then(console.log)
.catch(console.error)
```
2019-01-16 21:40:45 +08:00
The `Promise` returned by `journeys()` will resolve with an object with the `journeys` & `earlierRef` /`laterRef` fields. It might look like this:
2017-12-17 18:16:04 +01:00
```js
2019-01-16 21:40:45 +08:00
{
journeys: [ {
2018-03-04 23:25:08 +01:00
legs: [ {
2018-11-21 23:56:39 +01:00
tripId: '1|32615|6|86|10072018',
2019-12-10 19:40:13 +01:00
direction: 'S Ahrensfelde',
line: {
type: 'line',
id: '16845',
fahrtNr: '12345',
name: 'S7',
public: true,
mode: 'train',
product: 'suburban',
operator: {
type: 'operator',
id: 's-bahn-berlin-gmbh',
name: 'S-Bahn Berlin GmbH'
},
symbol: 'S',
nr: 7,
metro: false,
express: false,
night: false
},
2018-03-04 23:25:08 +01:00
origin: {
type: 'station',
id: '900000003201',
name: 'S+U Berlin Hauptbahnhof',
location: {
type: 'location',
latitude: 52.52585,
longitude: 13.368928
},
products: {
suburban: true,
subway: true,
tram: true,
bus: true,
ferry: false,
express: true,
regional: true
}
},
2019-12-10 19:40:13 +01:00
departure: '2018-07-10T23:54:00+02:00',
plannedDeparture: '2018-07-10T23:53:00+02:00',
departureDelay: 60,
departurePlatform: '15',
plannedDeparturePlatform: '14',
2018-03-04 23:25:08 +01:00
destination: {
type: 'station',
2018-07-16 12:39:13 +02:00
id: '900000100004',
name: 'S+U Jannowitzbrücke',
2018-03-04 23:25:08 +01:00
location: {
type: 'location',
latitude: 52.504806,
longitude: 13.303846
},
2018-07-16 12:39:13 +02:00
products: { /* … */ }
2018-03-04 23:25:08 +01:00
},
2019-04-23 17:55:46 +02:00
arrival: '2018-07-11T00:02:00+02:00',
2019-12-10 19:40:13 +01:00
plannedArrival: '2018-07-11T00:01:00+02:00',
2018-07-16 12:39:13 +02:00
arrivalDelay: 60,
arrivalPlatform: '3',
2019-12-10 19:40:13 +01:00
plannedArrivalPlatform: '3',
2018-06-13 20:35:06 +02:00
stopovers: [ {
2018-06-25 11:33:26 +02:00
stop: {
2018-03-04 23:25:08 +01:00
type: 'station',
id: '900000003201',
name: 'S+U Berlin Hauptbahnhof',
2019-12-10 19:40:13 +01:00
/* … */
2018-03-04 23:25:08 +01:00
},
2019-12-10 19:40:13 +01:00
2018-03-04 23:25:08 +01:00
arrival: null,
2019-12-10 19:40:13 +01:00
plannedArrival: null,
arrivalPlatform: null,
plannedArrivalPlatform: null,
2018-03-04 23:25:08 +01:00
departure: null,
2019-12-10 19:40:13 +01:00
plannedDeparture: null,
departurePlatform: null,
plannedDeparturePlatform: null,
2018-07-16 12:39:13 +02:00
remarks: [
{type: 'hint', code: 'bf', text: 'barrier-free'},
{type: 'hint', code: 'FB', text: 'Bicycle conveyance'}
]
2018-03-04 23:25:08 +01:00
}, {
2018-06-25 11:33:26 +02:00
stop: {
2018-03-04 23:25:08 +01:00
type: 'station',
2018-07-16 12:39:13 +02:00
id: '900000100001',
name: 'S+U Friedrichstr.',
2019-12-10 19:40:13 +01:00
/* … */
2018-03-04 23:25:08 +01:00
},
2019-12-10 19:40:13 +01:00
cancelled: true,
arrival: null,
plannedArrival: '2018-07-10T23:55:00+02:00',
prognosedArrival: '2018-07-10T23:56:00+02:00',
2018-07-16 12:39:13 +02:00
arrivalDelay: 60,
arrivalPlatform: null,
2019-12-10 19:40:13 +01:00
plannedArrivalPlatform: null,
departure: null,
plannedDeparture: '2018-07-10T23:56:00+02:00',
prognosedDeparture: '2018-07-10T23:57:00+02:00',
2018-07-16 12:39:13 +02:00
departureDelay: 60,
departurePlatform: null,
2019-12-10 19:40:13 +01:00
plannedDeparturePlatform: null,
2018-07-16 12:39:13 +02:00
remarks: [ /* … */ ]
},
/* … */
{
2019-12-10 19:40:13 +01:00
stop: {
2018-03-04 23:25:08 +01:00
type: 'station',
2018-07-16 12:39:13 +02:00
id: '900000100004',
name: 'S+U Jannowitzbrücke',
2019-12-10 19:40:13 +01:00
/* … */
2018-03-04 23:25:08 +01:00
},
2019-12-10 19:40:13 +01:00
2019-04-23 17:55:46 +02:00
arrival: '2018-07-11T00:02:00+02:00',
2019-12-10 19:40:13 +01:00
plannedArrival: '2018-07-11T00:01:00+02:00',
2018-07-16 12:39:13 +02:00
arrivalDelay: 60,
arrivalPlatform: null,
2019-12-10 19:40:13 +01:00
plannedArrivalPlatform: null,
2019-04-23 17:55:46 +02:00
departure: '2018-07-11T00:02:00+02:00',
2019-12-10 19:40:13 +01:00
plannedDeparture: '2018-07-11T00:02:00+02:00',
2018-07-16 12:39:13 +02:00
departureDelay: null,
departurePlatform: null,
2019-12-10 19:40:13 +01:00
plannedDeparturePlatform: null,
2018-07-16 12:39:13 +02:00
remarks: [ /* … */ ]
2018-03-04 23:25:08 +01:00
} ]
2018-07-16 12:39:13 +02:00
}, {
2019-12-10 19:40:13 +01:00
public: true,
walking: true,
distance: 558,
2018-07-16 12:39:13 +02:00
origin: {
type: 'station',
id: '900000100004',
name: 'S+U Jannowitzbrücke',
location: { /* … */ },
products: { /* … */ }
},
2019-12-10 19:40:13 +01:00
departure: '2018-07-11T00:01:00+02:00',
2018-07-16 12:39:13 +02:00
destination: {
type: 'station',
id: '900000100008',
name: 'U Heinrich-Heine-Str.',
location: { /* … */ },
products: { /* … */ }
},
2019-12-10 19:40:13 +01:00
arrival: '2018-07-11T00:10:00+02:00'
2018-03-23 21:19:25 +01:00
} ]
2019-01-16 21:40:45 +08:00
} ],
2018-03-04 23:25:08 +01:00
earlierRef: '…', // use with the `earlierThan` option
laterRef: '…' // use with the `laterThan` option
2019-01-16 21:40:45 +08:00
}
2017-12-17 18:16:04 +01:00
```
2018-01-04 22:37:47 +01:00
Some [profiles ](../p ) are able to parse the ticket information, if returned by the API. For example, if you pass `tickets: true` with the [VBB profile ](../p/vbb ), each `journey` will have a tickets array that looks like this:
2017-12-17 18:16:04 +01:00
```js
[ {
name: 'Berlin Tarifgebiet A-B: Einzelfahrausweis – Regeltarif',
price: 2.8,
tariff: 'Berlin',
coverage: 'AB',
variant: 'adult',
amount: 1
}, {
name: 'Berlin Tarifgebiet A-B: Einzelfahrausweis – Ermäßigungstarif',
price: 1.7,
tariff: 'Berlin',
coverage: 'AB',
variant: 'reduced',
amount: 1,
reduced: true
}, /* … */ {
name: 'Berlin Tarifgebiet A-B: Tageskarte – Ermäßigungstarif',
price: 4.7,
tariff: 'Berlin',
coverage: 'AB',
variant: '1 day, reduced',
amount: 1,
reduced: true,
fullDay: true
}, /* … */ {
name: 'Berlin Tarifgebiet A-B: 4-Fahrten-Karte – Regeltarif',
price: 9,
tariff: 'Berlin',
coverage: 'AB',
variant: '4x adult',
amount: 4
} ]
```
2017-12-18 21:11:35 +01:00
If a journey leg has been cancelled, a `cancelled: true` will be added. Also, `departure` /`departureDelay` /`departurePlatform` and `arrival` /`arrivalDelay` /`arrivalPlatform` will be `null` .
2018-03-04 23:25:08 +01:00
2019-01-16 21:40:45 +08:00
To get more journeys earlier/later than the current set of results, pass `earlierRef` /`laterRef` into `opt.earlierThan` /`opt.laterThan` . For example, query *later* journeys as follows:
2018-03-04 23:25:08 +01:00
```js
const hbf = '900000003201'
const heinrichHeineStr = '900000100008'
client.journeys(hbf, heinrichHeineStr)
2019-01-16 21:40:45 +08:00
.then((res) => {
const lastJourney = res.journeys[res.journeys.length - 1]
2018-03-23 21:19:25 +01:00
console.log('departure of last journey', lastJourney.legs[0].departure)
2018-03-04 23:25:08 +01:00
// get later journeys
return client.journeys(hbf, heinrichHeineStr, {
2019-01-16 21:40:45 +08:00
laterThan: res.laterRef
2018-03-04 23:25:08 +01:00
})
})
2019-01-16 21:40:45 +08:00
.then((laterRes) => {
const firstLaterJourney = laterRes.journeys[laterRes.journeys.length - 1]
console.log('departure of first (later) journey', firstLaterJourney.legs[0].departure)
2018-03-04 23:25:08 +01:00
})
.catch(console.error)
```
```
2019-04-23 17:55:46 +02:00
departure of last journey 2017-12-17T19:07:00+01:00
departure of first (later) journey 2017-12-17T19:19:00+01:00
2018-03-04 23:25:08 +01:00
```
2018-04-30 12:49:33 +02:00
2018-06-29 14:42:40 +02:00
If you pass `polylines: true` , each journey leg will have a `polyline` field. Refer to [the section in the `trip()` docs ](trip.md#polyline-option ) for details.
2018-07-23 20:42:22 +02:00
2019-12-12 18:42:44 +01:00
If you pass `scheduledDays: true` , each journey will have a `scheduledDays` object looking like this:
2018-07-23 20:42:22 +02:00
```js
{
'2018-01-01': true,
'2018-01-02': false,
// …
'2018-10-12': true,
'2018-10-13': true,
// …
'2019-01-02': false,
'2019-01-03': false
}
```