Comprehensive Guide to NS API Understanding Dozens of Useful Endpoints and Application Examples

Introduction to NS API

The NS API is a powerful tool that provides access to a wide range of data and functionalities related to the Dutch Railways. Whether you’re building applications for train schedules, traveler information, or station details, this guide will introduce you to the most useful endpoints of the NS API complete with code snippets and examples.

Getting Started with NS API

To start using the NS API, you need to register and obtain an API key. This key will allow you to make authenticated requests to the endpoints. Below are some essential APIs of the NS API:

1. Train Schedules API

This API provides train schedule information including departure and arrival times, as well as delays and cancellations.

  
    GET /ns-api-treinplanner?fromStation=UT&toStation=ASD&dateTime=2023-10-01T10%3A00%3A00&departure=true
  

2. Station Information API

Retrieve details about all train stations, including names, codes, and locations.

  
    GET /ns-api-stations-v2
  
  
    [
      {
        "code": "UT",
        "name": "Utrecht Centraal",
        "latitude": 52.089444,
        "longitude": 5.110278
      },
      ...
    ]
  

3. Disruption Information API

Get information about ongoing disruptions on the railway network.

  
    GET /ns-api-storingen?actual=true&unplanned=true
  

4. Travel Recommendations API

Generate travel advice based on departure and arrival stations with options for via points, travel class, and more.

  
    GET /ns-api-reisinformatie?fromStation=ASD&toStation=UT
  

5. Price Information API

Get fare details for travels between different stations.

  
    GET /ns-api-prijzen-v2?fromStation=UT&toStation=ASD
  
  
    {
      "price": 7.50,
      "currency": "EUR"
    }
  

Application Example Using NS API

Let’s create an example application that gives real-time train schedule and disruption information. We’ll use the Train Schedules API and Disruption Information API.

  
    import requests

    API_KEY = 'YOUR_API_KEY'
    BASE_URL = 'https://api.ns.nl/public/'

    def get_train_schedule(from_station, to_station, date_time):
        url = f"{BASE_URL}/ns-api-treinplanner?fromStation={from_station}&toStation={to_station}&dateTime={date_time}&departure=true"
        response = requests.get(url, headers={'Ocp-Apim-Subscription-Key': API_KEY})
        return response.json()

    def get_disruptions():
        url = f"{BASE_URL}/ns-api-storingen?actual=true&unplanned=true"
        response = requests.get(url, headers={'Ocp-Apim-Subscription-Key': API_KEY})
        return response.json()

    # Example Usage
    from_station = 'UT'
    to_station = 'ASD'
    date_time = '2023-10-01T10:00:00'

    train_schedule = get_train_schedule(from_station, to_station, date_time)
    disruptions = get_disruptions()

    print(f"Train Schedule from {from_station} to {to_station}: {train_schedule}")
    print(f"Current Disruptions: {disruptions}")
  

In this example, we use the Train Schedules API to get the schedule from Utrecht Centraal (UT) to Amsterdam Centraal (ASD) and the Disruption Information API to fetch the current disruptions. Replace 'YOUR_API_KEY' with your real API key to get live data.

Leveraging the NS API allows developers to create robust applications for various travel needs and real-time information, enhancing user experience and operational efficiency.

Hash: 2f05bfc11d3898c64fbfacaf292c54621535c16db70666c008f435865af18d18

Leave a Reply

Your email address will not be published. Required fields are marked *