         transportationsParam += "&transportations[]=\(transportation)"
       }
     }
-    
-    let url = "https://transport.opendata.ch/v1/connections?"+"from=\(from)&to=\(to)\(dateParam)\(timeParam)\(transportationsParam)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
+    let timeTypeParam = timeType == .arrival ? "&isArrivalTime=1" : "&isArrivalTime=0"
+    var accessibilityParam = "&accessibility="
+    switch (accessibility) {
+    case .advanced_notice : accessibilityParam += "advanced_notice"
+    case .assisted_boarding : accessibilityParam += "assisted_boarding"
+    case .independent_boarding : accessibilityParam += "independent_boarding"
+    default: accessibilityParam = ""
+    }
+    var optionsParam = ""
+    if (options.contains(.bike)) {
+      optionsParam += "&bike=1"
+    }
+    if (options.contains(.couchette)) {
+      optionsParam += "&couchette=1"
+    }
+    if (options.contains(.direct)) {
+      optionsParam += "&direct=1"
+    }
+    if (options.contains(.sleeper)) {
+      optionsParam += "&sleeper=1"
+    }
+    
+    let url = "https://transport.opendata.ch/v1/connections?"+"\(fromParam)\(toParam)\(limitParam)\(pageParam)\(dateParam)\(timeParam)\(transportationsParam)\(timeTypeParam)\(accessibilityParam)\(optionsParam)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
     return url
   }
   
-  static func connectionsForLocations(from: String, to: String, date: String = "", time: String = "", transportations: [TransportationType] = [], completionHandler: @escaping (Connections?, Error?) -> Void) {
+  static func connectionsForLocations(from: String,
+                                      to: String,
+                                      limit: Int = 4,
+                                      page: Int = 0,
+                                      date: String = "",
+                                      time: String = "",
+                                      transportations: [TransportationType] = [],
+                                      timeType: TimeType = .departure,
+                                      accessibility: AccessibilityType = .any,
+                                      options: [OptionsType] = [],
+                                      completionHandler: @escaping (Connections?, Error?) -> Void) {
     // set up URLRequest with URL
-    let endpoint = TransportAPI.endpointForConnections(from: from, to: to, date: date, time: time, transportations: transportations)
+    let endpoint : String
+    do {
+      endpoint = try TransportAPI.endpointForConnections(from: from, to: to, date: date, time: time, transportations: transportations)
+      callURL(endpoint, completionHandler: completionHandler);
+    } catch {
+      completionHandler(nil, error)
+      return;
+    }
+  }
+  
+  // MARK: Stationboard query
+  
+  static func endpointForStationboard(stationName: String,
+                                      stationId: String = "",
+                                      dateTime: String = "",
+                                      transportations: [TransportationType] = []) throws -> String {
+    let stationNameParam = stationName == "" ? "" : "&station="+stationName
+    let stationIdParam = stationId == "" ? "" : "&id="+stationId
+    if stationNameParam == "" && stationIdParam == "" {
+      throw BackendError.invalidParameter(reason: "Must supply either station name or id")
+    }
+    let dateTimeParam = dateTime == "" ? "" : "&dateTime="+dateTime
+    
+    
+    var transportationsParam = ""
+    if transportations.count > 0 {
+      for transportation in transportations {
+        transportationsParam += "&transportations[]=\(transportation)"
+      }
+    }
+    
+    let url = "https://transport.opendata.ch/v1/stationboard?"+"\(stationNameParam)\(dateTimeParam)\(transportationsParam)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
+    return url
+  }
+  
+  static func stationboardForStation(stationName: String,
+                                     stationId: String = "",
+                                     dateTime: String = "",
+                                     transportations: [TransportationType] = [],
+                                     completionHandler: @escaping (Stationboard?, Error?) -> Void) {
+    // set up URLRequest with URL
+    let endpoint : String
+    do {
+      endpoint = try TransportAPI.endpointForStationboard(stationName: stationName,
+                                                          stationId: stationId,
+                                                          dateTime: dateTime,
+                                                          transportations: transportations)
+      callURL(endpoint, completionHandler: completionHandler);
+    } catch {
+      completionHandler(nil, error)
+      return;
+    }
+  }
+  
+  // MARK: Utility
+  
+  // Do the network activity
+  private static func callURL<T: Codable>(_ endpoint: String, completionHandler: @escaping (T?, Error?) -> Void) {
     NSLog("Making request "+endpoint)
     guard let url = URL(string: endpoint) else {
       print("Error: cannot create connections URL")


class TransportAPI {
  
  enum QueryType {case all, station, poi, address }
  
  // Transportation types are not supported by new TransportAPI back end; ignored for now
  enum TransportationType {case all, ice_tgv_rj, ec_ic, ir, re_d, ship, s_sn_r, bus, cableway, arz_ext, tramway_underground }
  
  // MARK: Locations query
  

enum BackendError: Error {
  case urlError(reason: String)
  case objectSerialization(reason: String)
  case invalidParameter(reason: String)
}

extension String {
