Installation
pip install bahnapi
Python 3.9 or newer is required. The only runtime dependency is requests>=2.31.0.
Configuration
The Deutsche Bahn API requires credentials. Configure BahnAPI in one of two ways.
Programmatic configuration
import bahnapi
bahnapi.configure(
client_id="YOUR_CLIENT_ID",
api_key="YOUR_API_KEY",
timeout=15, # optional, seconds
)
Quick Start
import datetime as dt
import bahnapi
bahnapi.configure("YOUR_CLIENT_ID", "YOUR_API_KEY")
now = dt.datetime.now(dt.UTC)
departures = bahnapi.get_departures(
station_id="8011160", # Berlin Hbf
start_time=now,
end_time=now + dt.timedelta(hours=1),
include_recent_changes=True, # also merge /rchg
)
for dep in departures[:3]:
print(
dep["departure_planned"],
dep["departure_actual"],
dep["destination_name"],
dep["delay_minutes"],
)
Each entry is intentionally compact:
stop_id,station_evadeparture_planned,departure_actual,delay_minutesplatform_planned,platform_actualdestination_name,train_category,train_number,operator- optional
messageslist (only essential fields)
Station Search
from bahnapi import search_stations
matches = search_stations("Berlin Hbf", limit=5)
if matches:
print(matches[0]["eva"])
Use bahnapi.stations.resolve_station_eva(pattern) to enforce a single match; it raises StationLookupError when ambiguous or missing.
API Overview
bahnapi.configure(client_id, api_key, timeout=10)
bahnapi.get_departures(station_id, start_time, end_time, include_recent_changes=False)
bahnapi.search_stations(pattern, limit=None)
bahnapi.stations.resolve_station_eva(pattern)
Exceptions live in bahnapi.exceptions:
BahnAPIError(base class)AuthenticationErrorRateLimitErrorStationLookupError
License
Released under the MIT License (see LICENSE).
Acknowledgements
This project is unaffiliated with Deutsche Bahn. It simply wraps the official Timetables API provided via the DB API Marketplace. Always consult the official documentation for rate limits and conditions.*** End Patch
Metadata
Release files for bahnapi 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| bahnapi-0.1.0.tar.gz | 12.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bahnapi-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.6 kB
Release files / bahnapi-0.1.0.tar.gz
| Download URL | bahnapi-0.1.0.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97dfc2a2f627fefb035f82b6a0a55aa20908513b6dc7baf1405e797d72c87e96
|
|
BLAKE2b-256 checksum How to use checksums |
af394bd56d77a8e931611f015955b354f0164216daba001a8bbf678170403e72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / bahnapi-0.1.0-py3-none-any.whl
| Download URL | bahnapi-0.1.0-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4710845b3efbfd7326d067761fb6b46ed8f66c7ca533ed16ab51890ba791d3ca
|
|
BLAKE2b-256 checksum How to use checksums |
75d40801708445f1870bb419c8d7a3b66a20c09da413e8295aaa3b86988768a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Security for bahnapi 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.