Skip to content

Latest commit

 

History

History
340 lines (303 loc) · 13.7 KB

readme.md

File metadata and controls

340 lines (303 loc) · 13.7 KB

sncf

JavaScript client for the sncf API. Complies with the friendly public transport format. Inofficial, using endpoints by SNCF. Ask them for permission before using this module in production. Still in progress.

npm version Build Status dependency status dev dependency status license chat on gitter

Installation

npm install --save sncf

Usage

This package contains data in the Friendly Public Transport Format.

stations(query)

Using sncf.stations, you can search train stations operated by SNCF.

const stations = require('sncf').stations

stations("Montpellier")
.then(console.log)
.catch(console.error)

Returns a Promise that will resolve in an array of stations in the Friendly Public Transport Format which looks as follows:

[
    {
        type: 'station',
        id: 'FRMPL',
        name: 'Montpellier (FR)'
    }
    // …
]

journeys(origin, destination, date = Date.now(), opt = {})

Using sncf.journeys, you can get directions and prices for routes from A to B. Still in progress!

const journeys = require('sncf').journeys

const frankfurt = 'DEFRA'
const lyon = 'FRLYS'

journeys(frankfurt, lyon, new Date(), {duration: 24*60*60*1000})
.then(console.log)
.catch(console.error)

defaults, partially overridden by the opt parameter, looks like this:

const defaults = {
    duration: 6*60*60*1000, // searches for journeys in the next 6 hours starting at 'date' (parameter)
    direct: false, // direct connections only
    class: 2, // one of [1, 2]
    language: 'fr',
    busOnly: false
}

Returns a Promise that will resolve with an array of journeys in the Friendly Public Transport Format which looks as follows. Note that the legs are not fully spec-compatible, as the schedule is missing in legs.

[
    {
        "type": "journey",
        "id": "8774c636-6f77-40e4-8399-f55550b5726c",
        "origin": {
            "type": "station",
            "id": "DEFRH",
            "name": "FRANKFURT MAIN HBF"
        },
        "destination": {
            "type": "station",
            "id": "FRLPD",
            "name": "LYON PART DIEU"
        },
        "departure": "2017-07-11T00:48:00.000Z", // JS Date() object
        "arrival": "2017-07-11T09:02:00.000Z", // JS Date() object
        "legs": [
            {
                "origin": {
                    "type": "station",
                    "id": "DEFRH",
                    "name": "FRANKFURT MAIN HBF"
                },
                "destination": {
                    "type": "station",
                    "id": "CHAJP",
                    "name": "BALE CFF"
                },
                "departure": "2017-07-11T00:48:00.000Z", // JS Date() object
                "arrival": "2017-07-11T04:22:00.000Z", // JS Date() object
                "vehicle": {
                    "type": "TRAIN"
                }
            },
            {
                "origin": {
                    "type": "station",
                    "id": "CHAJP",
                    "name": "BALE CFF"
                },
                "destination": {
                    "type": "station",
                    "id": "FRLPD",
                    "name": "LYON PART DIEU"
                },
                "departure": "2017-07-11T05:24:00.000Z", // JS Date() object
                "arrival": "2017-07-11T09:02:00.000Z", // JS Date() object
                "vehicle": {
                    "type": "TRAIN"
                }
            }
        ],
        "price": {
            "amount": 174.8,
            "currency": "EUR",
            "fares": [
                {
                    "price": {
                        "amount": 241.2,
                        "currency": "EUR"
                    },
                    "model": "UPSELL",
                    "appliedDiscount": 0,
                    "passengers": [
                        {
                            "clientId": "0",
                            "travelerId": null,
                            "price": 241.2,
                            "age": "ADULT",
                            "fidelityCard": "NONE",
                            "fidelityPoints": null,
                            "globalPassenger": false,
                            "promoCodeType": null,
                            "fareInformations": [
                                {
                                    "fareName": "Tarif normal Adulte",
                                    "fareCondition": "BILLET AVEC RESERVATION : échange et remboursement sans frais avant départ, 50% après départ. BILLET SANS RESERVATION : échange et remboursement avec 10% de retenue, avant et après départ .",
                                    "fareCode": "TRNO",
                                    "fareSpecificRule": null,
                                    "fareSequence": null,
                                    "cosLevel": null,
                                    "returnMandatory": false,
                                    "passengerType": "PT00AD",
                                    "classOfService": "A",
                                    "segmentId": 4,
                                    "passengerClientId": "0",
                                    "promoCodeApplied": false,
                                    "fixedPriceCuiQuotation": false,
                                    "fakeFare": false
                                },
                                {
                                    "fareName": "TGV LOISIR",
                                    "fareCondition": "Billet échangeable et remboursable avec retenue de 5 € à compter de 30 jours avant le départ, portée à 15 € la veille et le jour du départ.  A ces frais s'ajoute l'éventuelle différence de prix entre l'ancien et le nouveau billet.  Billet non échangeable et non remboursable après le départ.",
                                    "fareCode": "PR11",
                                    "fareSpecificRule": null,
                                    "fareSequence": null,
                                    "cosLevel": "06",
                                    "returnMandatory": false,
                                    "passengerType": "PT00AD",
                                    "classOfService": "AG",
                                    "segmentId": 5,
                                    "passengerClientId": "0",
                                    "promoCodeApplied": false,
                                    "fixedPriceCuiQuotation": false,
                                    "fakeFare": false
                                }
                            ],
                            "passengerType": "HUMAN",
                            "encartedPrems": false,
                            "specificSeatRequired": true,
                            "promoCodeApplied": false
                        }
                    ],
                    "animals": [],
                    "bookingFee": {
                        "amount": 5,
                        "currency": "EUR",
                        "type": "FDD"
                    },
                    "bicycle": false,
                    "placementOptions": true
                },
                {
                    "price": {
                        "amount": 174.8,
                        "currency": "EUR"
                    },
                    "model": "SEMIFLEX",
                    "appliedDiscount": 0,
                    "passengers": [
                        {
                            "clientId": "0",
                            "travelerId": null,
                            "price": 174.8,
                            "age": "ADULT",
                            "fidelityCard": "NONE",
                            "fidelityPoints": null,
                            "globalPassenger": false,
                            "promoCodeType": null,
                            "fareInformations": [
                                {
                                    "fareName": "Tarif normal Adulte",
                                    "fareCondition": "BILLET AVEC RESERVATION : échange et remboursement sans frais avant départ, 50% après départ. BILLET SANS RESERVATION : échange et remboursement avec 10% de retenue, avant et après départ .",
                                    "fareCode": "TRNO",
                                    "fareSpecificRule": null,
                                    "fareSequence": null,
                                    "cosLevel": null,
                                    "returnMandatory": false,
                                    "passengerType": "PT00AD",
                                    "classOfService": "B",
                                    "segmentId": 4,
                                    "passengerClientId": "0",
                                    "promoCodeApplied": false,
                                    "fixedPriceCuiQuotation": false,
                                    "fakeFare": false
                                },
                                {
                                    "fareName": "TGV LOISIR",
                                    "fareCondition": "Billet échangeable et remboursable avec retenue de 5 € à compter de 30 jours avant le départ, portée à 15 € la veille et le jour du départ.  A ces frais s'ajoute l'éventuelle différence de prix entre l'ancien et le nouveau billet.  Billet non échangeable et non remboursable après le départ.",
                                    "fareCode": "PR11",
                                    "fareSpecificRule": null,
                                    "fareSequence": null,
                                    "cosLevel": "01",
                                    "returnMandatory": false,
                                    "passengerType": "PT00AD",
                                    "classOfService": "BP",
                                    "segmentId": 5,
                                    "passengerClientId": "0",
                                    "promoCodeApplied": false,
                                    "fixedPriceCuiQuotation": false,
                                    "fakeFare": false
                                }
                            ],
                            "passengerType": "HUMAN",
                            "encartedPrems": false,
                            "specificSeatRequired": true,
                            "promoCodeApplied": false
                        }
                    ],
                    "animals": [],
                    "bookingFee": {
                        "amount": 5,
                        "currency": "EUR",
                        "type": "FDD"
                    },
                    "bicycle": false,
                    "placementOptions": true
                }
            ],
            "unsellableReason": null
        },
        "perturbations": false
    }
    // …
]

prices(origin, destination, date = Date.now(), opt = {})

Using sncf.prices, you can get price information for routes from A to B for a whole month. Note that the only information taken from the date parameter will be its month according to the 'Europe/Paris' timezone, meaning that 2017-07-31T22:00:00 would actually give you the results for August 2017.

const prices = require('sncf').prices

const frankfurt = 'DEFRA'
const lyon = 'FRLYS'

prices(frankfurt, lyon, new Date(), {class: 1})
.then(console.log)
.catch(console.error)

defaults, partially overridden by the opt parameter, looks like this:

const defaults = {
    direct: false, // direct connections only
    class: 2, // one of [1, 2]
    language: 'fr'
}

Returns a Promise that will resolve in an array which looks as follows:

[
    {
        "price": {
            "amount": 76,
            "currency": "EUR"
        },
        "date": "2017-07-31T22:00:00.000Z" // actually August 1 in Paris time zone!
    },
    {
        "price": {
            "amount": 76,
            "currency": "EUR"
        },
        "date": "2017-08-01T22:00:00.000Z"
    },
    // …
    {
        "price": {
            "amount": 44,
            "currency": "EUR"
        },
        "date": "2017-08-30T22:00:00.000Z"
    }
]

See also

  • FPTF - "Friendly public transport format"
  • FPTF-modules - modules that also use FPTF

Contributing

If you found a bug, want to propose a feature or feel the urge to complain about your life, feel free to visit the issues page.