Skip to main content

Convenience wrapper for Deutsche Bahn Timetables API.

Project description

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_eva
  • departure_planned, departure_actual, delay_minutes
  • platform_planned, platform_actual
  • destination_name, train_category, train_number, operator
  • optional messages list (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)
  • AuthenticationError
  • RateLimitError
  • StationLookupError

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bahnapi-0.1.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bahnapi-0.1.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file bahnapi-0.1.0.tar.gz.

File metadata

  • Download URL: bahnapi-0.1.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for bahnapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 97dfc2a2f627fefb035f82b6a0a55aa20908513b6dc7baf1405e797d72c87e96
MD5 4dbda9b0185cb6d03df91cf57685db5e
BLAKE2b-256 af394bd56d77a8e931611f015955b354f0164216daba001a8bbf678170403e72

See more details on using hashes here.

File details

Details for the file bahnapi-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bahnapi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for bahnapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4710845b3efbfd7326d067761fb6b46ed8f66c7ca533ed16ab51890ba791d3ca
MD5 d228d7577af270e7b996a5d2630a676c
BLAKE2b-256 75d40801708445f1870bb419c8d7a3b66a20c09da413e8295aaa3b86988768a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page