Skip to main content

A wrapper around the DB Website API for querying train connections.

Project description

🚆 weiche

This is an unofficial library wrapper around the API of the Deutsche Bahn (DB) website for identifying connections. It returns realtime, live data. If you do not need this in your project I strongly recommend not to use this API as it might break any moment if DB changes the API for getting station and train connections.

It has a API compatibility layer to the schiene package for easy drop-in replacement as schiene no longer works.

Requirements

  • Python 3.10 and above
  • requests package for the synchronous API and Schiene wrapper
  • aiohttp for the asynchronous API

Features

  • Get connections between two stations
  • Search stations by name
  • Get station information

Setup

$ python -m venv /path/to/venv $ /path/to/venv/bin/python -m pip install weiche[sync,async,schiene]

Usage

Use the synchronous API

from datetime import datetime
from weiche import SynchronousApi

api = SynchronousApi()

amsterdam_query = api.search_locations('Amsterdam', limit=1)
amsterdam = amsterdam_query[0]
munich_query = api.search_locations('München Hbf', limit=1)
munich = munich_query[0]

api.search_connections(
    at=datetime(2025,5,10,18,0,0),
    from_location=munich.id,
    to_location=amsterdam.id,
    limit=10,
)
Response from Synchronous API

The return format is an object which can also be found in the objects.py file.

Look for the Connection class and see the API definition there.

Use the asynchronous API

import asyncio
from datetime import datetime
from weiche import AsynchronousApi

async def search():
    api = AsynchronousApi()

    amsterdam_query = await api.search_locations('Amsterdam', limit=1)
    amsterdam = amsterdam_query[0]
    munich_query = await api.search_locations('München Hbf', limit=1)
    munich = munich_query[0]

    return await api.search_connections(
        at=datetime(2025,5,10,18,0,0),
        from_location=munich.id,
        to_location=amsterdam.id,
        limit=10,
    )

asyncio.get_event_loop().run_until_complete(search())
Response from Asynchronous API

The return format is an object which can also be found in the objects.py file.

Look for the Connection class and see the API definition there.

Use the schiene compatibility layer

from weiche import Schiene

Schiene().connections('Mannheim HbF', 'Stuttgart HbF')
Response from Schiene Layer
[{'arrival': '15:47',
  'canceled': False,
  'departure': '14:30',
  'details': '',
  'price': 55.0,
  'products': ['ICE'],
  'time': '01:16',
  'transfers': 0,
  'ontime': True,
  'delay': None},
 {'arrival': '16:21',
  'canceled': False,
  'departure': '14:38',
  'details': '',
  'price': 50.99,
  'products': ['SBAHN', 'ICE'],
  'time': '01:42',
  'transfers': 1,
  'ontime': True,
  'delay': None},
 {'arrival': '16:25',
  'canceled': False,
  'departure': '14:46',
  'details': '',
  'price': 50.99,
  'products': ['ICE', 'REGIONAL'],
  'time': '01:23',
  'transfers': 1,
  'ontime': True,
  'delay': None},
 {'arrival': '16:45',
  'canceled': False,
  'departure': '15:31',
  'details': '',
  'price': 55.0,
  'products': ['ICE'],
  'time': '01:14',
  'transfers': 0,
  'ontime': True,
  'delay': None},
 {'arrival': '17:27',
  'canceled': False,
  'departure': '15:34',
  'details': '',
  'price': 55.0,
  'products': ['ICE', 'REGIONAL'],
  'time': '01:53',
  'transfers': 1,
  'ontime': True,
  'delay': None},
 {'arrival': '17:15',
  'canceled': False,
  'departure': '15:35',
  'details': '',
  'price': 21.8,
  'products': ['REGIONAL', 'REGIONAL'],
  'time': '01:40',
  'transfers': 1,
  'ontime': True,
  'delay': None},
 {'arrival': '17:44',
  'canceled': False,
  'departure': '15:35',
  'details': '',
  'price': 21.8,
  'products': ['REGIONAL', 'REGIONAL'],
  'time': '02:09',
  'transfers': 1,
  'ontime': True,
  'delay': None},
 {'arrival': '17:33',
  'canceled': False,
  'departure': '15:36',
  'details': '',
  'price': 24.99,
  'products': ['SBAHN', 'ICE'],
  'time': '02:06',
  'transfers': 1,
  'ontime': False,
  'delay': {'delay_departure': 0, 'delay_arrival': 9}},
 {'arrival': '18:20',
  'canceled': False,
  'departure': '16:38',
  'details': '',
  'price': 47.99,
  'products': ['SBAHN', 'ICE'],
  'time': '01:42',
  'transfers': 1,
  'ontime': True,
  'delay': None},
 {'arrival': '18:24',
  'canceled': False,
  'departure': '16:46',
  'details': '',
  'price': 50.99,
  'products': ['ICE', 'REGIONAL'],
  'time': '01:38',
  'transfers': 1,
  'ontime': True,
  'delay': None}]

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

weiche-0.9.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

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

weiche-0.9.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file weiche-0.9.1.tar.gz.

File metadata

  • Download URL: weiche-0.9.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for weiche-0.9.1.tar.gz
Algorithm Hash digest
SHA256 5b09f114399b3480f423f116f7838e79051a84e1908b5e4eab1c45e380ccb791
MD5 e149b7f0870f89977127eff4e004344e
BLAKE2b-256 2c302465839ddf928f7c4d692e9128f9f511574bfb362ad965d40a93b3ddbe04

See more details on using hashes here.

File details

Details for the file weiche-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: weiche-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for weiche-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0b6ddc355230502551eb07a176849c26d5f7c84bb75555e8e7208b4c0a7ecfd
MD5 a5f46c6a13ea1ad0f1ee24157c792387
BLAKE2b-256 5a92eb0eea7aab4a8aa5e816da735ab500bf6f84c844587cb5df9214cdabfdbd

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