🚉 Public Transport SDK
A client library for public transport APIs, for Rust, Python, Swift, Kotlin and TypeScript. The libraries use a shared model for stops, departures and trips that covers several server types, so an application can query any supported network the same way.
The project is at an early stage. It can search for locations by name, list the departures at a stop and plan trips between two places on servers that speak one of six protocols:
- EFA, the JSON interface of MDV's journey planner, used by many German transport associations
- MOTIS, the routing engine behind Transitous
- TRIAS 1.2, the German XML standard VDV 431-2
- OJP 1.0, its European successor
- HAFAS mgate, the JSON interface behind the apps of many operators
- OpenTripPlanner 2 with its Transmodel API
The library ships with networks that were checked against their live servers, named by BundledNetwork and listed by Registry::bundled(): VVO, VRR, Transitous, Entur and the HAFAS servers of about 35 operators in Europe and the USA. Further networks can be described in code or read from entries in the format of transport-apis. Each network says where it has coverage, so an application can pick the networks for a position.
Design
The core crate does no I/O. It builds HTTP requests and parses HTTP responses, and the caller sends them. This keeps the core usable from other languages and from WebAssembly, where each platform brings its own HTTP stack. The crate documentation of publictransport shows the request and parse steps and what the HTTP client has to do.
Rust applications that do not want to bring their own HTTP client use publictransport-client, which sends the requests with reqwest. Packages for Python, Swift, Kotlin and TypeScript wrap the same core, each with a client on its platform's HTTP stack: httpx, URLSession, OkHttp and fetch.
The documentation has a guide for each language, and examples has a short program for each kind of request.
Install
| Language | Package | |
|---|---|---|
| Rust | cargo add publictransport-client, or publictransport alone to bring your own HTTP client |
crates/client, crates/core |
| Python | uv add publictransport or pip install publictransport |
packages/python |
| Swift | .package(url: "https://github.com/kiliankoe/publictransport", from: "0.1.2"), product PublicTransport |
packages/swift |
| Kotlin | implementation("io.github.kiliankoe:publictransport:0.1.2") |
packages/kotlin |
| TypeScript | pnpm add @kiliankoe/publictransport or npm install @kiliankoe/publictransport |
packages/typescript |
Shared Core
Every language runs the same Rust core, so a request is built and an answer is read the same way everywhere.
- The model is defined once, in Rust. Each language gets the types that UniFFI generates from it. To make things more idiomatic, the generator's settings and a small script adapt details, such as
scripts/swift-idioms.pyandscripts/kotlin-idioms.py. TypeScript runs the core as WebAssembly and gets its types from wasm-bindgen and tsify, steered by attributes on the Rust types. - Written by hand in each language are the client on the platform's HTTP stack, and the bridges to the platform's own types, such as
TimeZonein Swift or the error classes in TypeScript. - A convenience that every language can use goes into the Rust code, not into one package.
Development
Clone with --recurse-submodules, or run git submodule update --init afterwards: the network data of transport-apis is a submodule under crates/core/data. Networks that repository does not have yet are in crates/core/data/networks, in the same format.
The development environment is a nix flake; direnv allow or nix develop provides the toolchain. The Justfile has the commands of the project, and just lists them. CI runs these checks, which just check runs too:
The Python package in packages/python, the Swift package in packages/swift, the Kotlin package in packages/kotlin and the TypeScript package in packages/typescript have their own READMEs. The documentation site in site is an mdBook that includes the READMEs and the examples, so write user documentation there; just site-serve shows it, and a release publishes it. The Swift package has its Package.swift at the root of this repository, where SwiftPM looks for it. The crate in bindings/uniffi builds the library for Python, Swift and Kotlin, and scripts/build-swift.sh generates the Swift code and builds the XCFramework. The Kotlin code is generated by the Gradle build. The crate in bindings/wasm is the WebAssembly module of the TypeScript package, which packages/typescript/build.sh builds.
The tests replay responses recorded from real servers. crates/core/tests/fixtures/record.sh records them again. just check-networks asks every bundled network's live server for stops, departures and trips, to see which still work. scripts/validate-xml-requests.sh checks the TRIAS and OJP requests that the library builds against the official schemas, which it fetches from the VDV repositories.
Please also see CONTRIBUTING.md for further details.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option. NOTICE names the parts that have another license: the code that UniFFI generates and the network data.