# NS reisinformatie

Actuele vertrektijden, vertragingen en storingen op het spoor. Gratis, met een sleutel uit het API-portaal van NS.

- Uitgever: Nederlandse Spoorwegen
- Gebied: Nederland · Laagste detail: station en rit
- Periode: heden · Ververst: elke minuut
- Toegang: Gratis sleutel · Licentie: Voorwaarden NS
- Koppelsleutels: stationscode
- Status: grijs
- URL: https://apiportal.ns.nl

## Wat kun je ermee?
- **Hoe vaak is jouw trein te laat?.** Sla de vertrektijden van één station een maand lang op en tel de vertragingen.

## Interessante koppelingen
- Meer storingen bij hitte of storm? → [knmi-daggegevens](https://www.opendataindex.nl/bron/knmi-daggegevens.md) via datum. Koppel storingen per dag aan maximumtemperatuur en windstoten.

## Vergelijkbare bronnen
- [Rijden de Treinen](https://www.rijdendetreinen.nl/open-data): Historische storingen en vertragingen als open data.

## API (OpenAPI 3.0.1)

- Basis: https://gateway.apiportal.ns.nl/reisinformatie-api
- Toegang: Gratis sleutel nodig, mee te sturen in de header Ocp-Apim-Subscription-Key. Zonder sleutel: HTTP 401 (getest). De sleutel van Open Data Index blijft op de server. (getest)
- Formaten: JSON
- Filteren: Geen algemene filtertaal: elk endpoint heeft zijn eigen query-parameters (zie hieronder).; 

### Endpoints
- `GET /api/v3/trips`: Trips
- `GET /api/v3/trips/trip`: Single trip
- `GET /api/v3/trips/booked-trip`: Booked trip
- `GET /api/v3/price`: Domestic prices (verouderd)
- `GET /api/v3/disruptions`: Disruptions (verouderd)
- `GET /api/v3/disruptions/{type}/{id}`: Single disruption (verouderd)
- `GET /api/v3/disruptions/station/{stationCode}`: Station disruptions (verouderd)
- `GET /api/v2/stations`: Stations (verouderd)
- `GET /api/v2/stations/nearest`: Get stations closest to provided coordinates (verouderd)
- `GET /api/v2/price`: Domestic prices
- `GET /api/v2/journey`: Journey details
- `GET /api/v2/departures`: Departures (verouderd)
- `GET /api/v2/arrivals`: Arrivals (verouderd)
- `GET /api/v1/calamities`: Calamities (verouderd)
- `GET /api/openapi`: OpenAPI Spec

### Parameters
- `dateTime`: Datetime that the user want to depart from his origin or or arrive at his destination · bij 4 endpoints (uit de beschrijving)
- `travelClass`: Class of travel to use when calculating product prices · bij 4 endpoints (uit de beschrijving)
- `lang`: Language to use for localizing the travel advice. Only a small subset of text is translated, mainly notes. Defaults to Dutch · bij 3 endpoints (uit de beschrijving)
- `fromStation`: NS station code of the origin station · bij 3 endpoints (uit de beschrijving)
- `toStation`: NS station code of the destination station · bij 3 endpoints (uit de beschrijving)
- `nsr`: NSR number for the customer making this request. Since the replacement of the NSRA backend in Reisinfo with PAS, we do not use the NSR … · bij 2 endpoints (uit de beschrijving)
- `accessibilityEquipment1`: Accessibility equipment to use when searching for trip assistance options (AVG/PAS) · bij 2 endpoints (uit de beschrijving)
- `accessibilityEquipment2`: Accessibility equipment to use when searching for trip assistance options (AVG/PAS) · bij 2 endpoints (uit de beschrijving)
- `product`: Name of the product that will be used in travel · bij 2 endpoints (uit de beschrijving)
- `discount`: Discount of travel to use when calculating product prices · bij 2 endpoints (uit de beschrijving)
- `travelRequestType`: directionsOnly only plans a google directions (walk/bike/car) advice · bij 2 endpoints (uit de beschrijving)
- `ctxRecon`: Reconstruction context to use as basis for finding trip. Can be found in the /api/v3/trips output · bij 2 endpoints (uit de beschrijving)
- `travelType`: Return the price for a single or return trip. Defaults to single · bij 2 endpoints (uit de beschrijving)
- `isJointJourney`: Set to true to return the price including joint journey discount · bij 2 endpoints (uit de beschrijving)
- `adults`: Number of adults to return the price for. Defaults to 1 · bij 2 endpoints (uit de beschrijving)
- `children`: Number of children to return the price for. Defaults to 0 · bij 2 endpoints (uit de beschrijving)
- `routeId`: Specific identifier for the route to take between the two stations. This routeId is returned in the /api/v3/trips call. · bij 2 endpoints (uit de beschrijving)
- `plannedArrivalTime`: Planned arrival time of the trip. Is used to find the correct route, if multiple routes are possible between the origin and destination … · bij 2 endpoints (uit de beschrijving)
- `limit`: The limit is only applied when call is performed with q parameter. If invoked by countrycodes only, no limit is applied · bij 2 endpoints (uit de beschrijving)
- `station`: NS Station code for the station to return departures for. Can be left empty if uicCode parameter is provided. · bij 2 endpoints (uit de beschrijving)
- `uicCode`: UIC code for the station to return departures for. Can be left empty if station parameter is provided. · bij 2 endpoints (uit de beschrijving)
- `maxJourneys`: Number of departures or departures to return. Defaults to 40 · bij 2 endpoints (uit de beschrijving)
- `originUicCode`: UIC code of the origin station · bij v3/trips (uit de beschrijving)
- `originLat`: Latitude of the origin location. Should be used together with originLng. If the origin is a station, just provide the uicCode instead of … · bij v3/trips (uit de beschrijving)
- `originLng`: Longitude of the origin location. Should be used together with originLat. If the origin is a station, just provide the uicCode instead of … · bij v3/trips (uit de beschrijving)
- `originName`: Name of the origin location. Will be returned in the response · bij v3/trips (uit de beschrijving)
- `destinationUicCode`: UIC code of the destination station · bij v3/trips (uit de beschrijving)
- `destinationLat`: Latitude of the destination location. Should be used together with destinationLng. If the destination is a station, just provide the … · bij v3/trips (uit de beschrijving)
- `destinationLng`: Longitude of the destination location. Should be used together with destinationLat. If the destination is a station, just provide the … · bij v3/trips (uit de beschrijving)
- `destinationName`: Name of the destination location. Will be returned in the response · bij v3/trips (uit de beschrijving)

### Limieten en valkuilen
- De beschrijving noemt als server https://apigateway.ns.nl/reisinformatie-api, maar de werkende gateway is https://gateway.apiportal.ns.nl/reisinformatie-api. (getest)
- 300 verzoeken per 5 minuten voor niet-betalende gebruikers; daarboven HTTP 429 met een Retry-After-kop. (uit de beschrijving)
- De NS-voorwaarden verbieden het NS-logo in je toepassing en het zo bewerken van gegevens dat er onjuiste informatie ontstaat. (uit de beschrijving)
- Antwoorden komen gecomprimeerd (gzip); gebruik curl --compressed of een client die dat zelf uitpakt. (getest)
- Arrivals en departures in deze API zijn verouderd: NS verwijst naar https://gateway.apiportal.ns.nl/timetable-api/v2/. (uit de beschrijving)
- Het NS-portaal toont de API-definities alleen na inloggen; alleen de spec van de Reisinformatie-API is via de gateway op te vragen. (getest)

### Voorbeeld
```
https://gateway.apiportal.ns.nl/reisinformatie-api/api/v2/departures?station=UT&maxJourneys=2
```

## Wat je ermee kunt bouwen
- [Dashboard met treinvertragingen en storingen](https://www.opendataindex.nl/bouwen/treinvertragingen-dashboard.md): Een pagina die elke minuut laat zien hoeveel treinen er rijden, welke te laat zijn en welke storingen er spelen, gebouwd op het open GTFS-Realtime-bestand van openOV en, met een gratis sleutel, de storingen van NS.

