Getting started overview

This guide outlines the process for new users to begin integrating with the Transport for Germany API. It covers account registration, API key management, and the execution of a foundational API call using provided SDKs or a direct HTTP request. The aim is to enable developers to retrieve real-time and historical public transit data for Germany efficiently.

Transport for Germany provides data for mobility applications, logistics optimization, and urban planning. Its API offerings include real-time GTFS-RT feeds, historical GTFS data, and route planning functionalities. These services are accessible via a RESTful API architecture, complemented by client libraries for Python, JavaScript, and Go to facilitate development.

The initial setup involves creating an account on the Transport for Germany homepage, generating an API key, and using this key to authenticate requests. The platform offers a free Developer Plan with 5,000 requests per month, allowing for preliminary testing and development before committing to a paid tier. Familiarity with HTTP methods and JSON data structures is beneficial for API integration.

Quick reference table

Step What to do Where
1. Sign up Create a new Transport for Germany developer account. Transport for Germany homepage
2. Get API Key Locate and copy your unique API key from the developer dashboard. Developer Portal > API Keys section
3. Install SDK (Optional) Install the Python, JavaScript, or Go SDK for easier integration. Transport for Germany documentation
4. Make First Request Execute a simple API call using your API key. API Reference > Quickstart Examples
5. Explore Data Review the JSON response and available data fields. API Response Body

Create an account and get keys

To begin using the Transport for Germany API, you must first create a developer account and generate an API key. This key authenticates your requests and links them to your usage plan.

Account registration

  1. Navigate to the Transport for Germany homepage.
  2. Click on the "Sign Up" or "Get Started" button.
  3. Provide the required registration details, typically including an email address and a password.
  4. Verify your email address if prompted, by clicking a link sent to your registered email.
  5. Once verified, log in to your newly created developer account.

API key generation

  1. After logging in, access the developer dashboard. This is usually found via a link labeled "Dashboard," "My Account," or "API Keys."
  2. Within the dashboard, locate the "API Keys" section.
  3. Generate a new API key. Some platforms allow you to name your keys for organizational purposes, which is recommended for managing multiple applications.
  4. Copy the generated API key. This key is a sensitive credential and should be stored securely. It will be used to authenticate all your API requests.

Your API key is essential for accessing the API. Treat it as a password and avoid embedding it directly in client-side code or public repositories. For server-side applications, use environment variables or a secure configuration management system. The Transport for Germany API reference provides further details on secure API key handling.

Your first request

After obtaining your API key, you can make your first API request. This section demonstrates how to query the Real-time GTFS-RT API, which provides live updates on public transport. We will show examples using curl for a direct HTTP request and Python for an SDK-based approach.

API endpoint

The primary endpoint for real-time GTFS-RT data is typically:

GET https://api.transportfor.germany/v1/gtfs-rt/feed

Refer to the Transport for Germany API reference for specific endpoint details and available parameters.

Authentication

API requests are authenticated by including your API key in the Authorization header as a Bearer token.

Authorization: Bearer YOUR_API_KEY

Example using curl (direct HTTP request)

The following curl command demonstrates how to make a basic authenticated request to retrieve the real-time GTFS-RT feed. Replace YOUR_API_KEY with your actual API key.

curl -X GET \
  'https://api.transportfor.germany/v1/gtfs-rt/feed' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY'

This command requests the real-time feed and specifies that the client prefers a JSON response. The -H flag is used to add HTTP headers for Accept and Authorization.

Example using Python SDK

First, ensure you have the Transport for Germany Python SDK installed:

pip install transport-for-germany

Then, you can use the following Python code to make a request:

import os
from transport_for_germany import Client

# Retrieve API key from environment variable for security
api_key = os.environ.get("TRANSPORT_FOR_GERMANY_API_KEY")

if not api_key:
    raise ValueError("TRANSPORT_FOR_GERMANY_API_KEY environment variable not set.")

client = Client(api_key=api_key)

try:
    # Make a request to the GTFS-RT feed endpoint
    feed_data = client.gtfs_rt.get_feed()
    print(feed_data)
except Exception as e:
    print(f"An error occurred: {e}")

This Python example initializes the client with your API key, ideally sourced from an environment variable for security, and then calls the get_feed() method to retrieve the real-time data. The response will be a Python dictionary representing the JSON data.

Upon successful execution of either the curl command or the Python script, you should receive a JSON response containing real-time public transport data. This typically includes vehicle positions, trip updates, and service alerts, formatted according to the GTFS-Realtime specification.

Common next steps

After successfully making your first API call, consider these next steps to further integrate with Transport for Germany:

  • Explore API reference: Review the comprehensive API reference documentation to understand all available endpoints, parameters, and response structures. This includes historical GTFS data and route planning APIs.
  • Implement error handling: Integrate robust error handling into your application to manage various API responses, including rate limits, authentication failures, and data unavailability. The API typically returns standard HTTP status codes and JSON error messages.
  • Manage rate limits: Be aware of the rate limits associated with your chosen plan. Implement a strategy, such as exponential backoff, to handle 429 Too Many Requests responses gracefully. Details on specific rate limits are available on the Transport for Germany pricing page.
  • Utilize webhooks (if available): For certain event-driven scenarios, explore if Transport for Germany offers webhooks for push notifications, reducing the need for constant polling. While not explicitly listed, many modern APIs include such features. For general information on securing webhooks, refer to Twilio's webhook security guide.
  • Upgrade your plan: If your application requires higher request volumes or additional features, consider upgrading from the free Developer Plan to a paid tier on the pricing page.
  • Contribute to the community: Engage with the Transport for Germany developer community if available, for support and to share integration experiences.

Troubleshooting the first call

Encountering issues during your first API call is common. Here are some troubleshooting steps:

  • Check API key: Ensure your API key is correctly copied and included in the Authorization: Bearer YOUR_API_KEY header or passed correctly to the SDK client. A common error is a mistyped key or an incorrect header format.
  • Verify endpoint URL: Double-check that the API endpoint URL is accurate, including the protocol (https://), domain, and path. Refer to the API reference for the correct URL.
  • Review HTTP status codes:
    • 401 Unauthorized: Indicates an issue with your API key or its format. Ensure it's active and correctly placed in the Authorization header.
    • 403 Forbidden: Your API key might not have the necessary permissions for the requested endpoint, or your account might be suspended. Check your developer dashboard.
    • 404 Not Found: The endpoint URL is incorrect or the resource does not exist.
    • 429 Too Many Requests: You have exceeded your rate limit. Wait for the reset period or consider upgrading your plan.
    • 5xx Server Error: An issue on the Transport for Germany server side. These are usually temporary; retry your request after a short delay.
  • Inspect JSON response for errors: Many APIs return detailed error messages within the JSON response body even for non-200 status codes. Parse the response to understand specific issues.
  • Check network connectivity: Ensure your development environment has an active internet connection and no firewall rules are blocking outgoing requests to api.transportfor.germany.
  • Consult SDK documentation: If using an SDK, review its specific documentation for common setup issues or examples.
  • Contact support: If problems persist, and you have exhausted troubleshooting steps, contact Transport for Germany support through their support channels. Provide details of your request, including the endpoint, headers, and any error messages received.