SDKs overview
The Railway Transport for France, primarily through its developer portal, provides access to various APIs designed for integrating French rail transport data into applications. These APIs cover functionalities such as journey planning, real-time traffic information, and disruption alerts. To facilitate developer integration, official Software Development Kits (SDKs) are provided in several popular programming languages, alongside community-contributed libraries. These SDKs aim to simplify the interaction with the underlying SNCF APIs by handling authentication, request formatting, and response parsing.
The developer experience is supported by comprehensive documentation, including an interactive API explorer available on the SNCF developer portal. This portal outlines the API key acquisition process for sandbox access and details the RESTful architecture that utilizes JSON for data exchange. While the official SDKs streamline common tasks, developers also have the option to interact with the APIs directly using standard HTTP clients, as described in the Navitia API reference documentation, which powers many of the services.
Official SDKs by language
Official SDKs are provided to offer a structured and language-specific approach to interacting with the Railway Transport for France APIs. These SDKs are maintained to ensure compatibility and to reflect updates in the API specifications. The following table details the officially supported SDKs, their package names, installation commands, and general maturity levels.
| Language | Package Name | Installation Command | Maturity |
|---|---|---|---|
| Python | sncf-api-python |
pip install sncf-api-python |
Stable |
| Java | com.sncf.api:java-sdk |
Maven: Add dependency to pom.xml |
Stable |
| Node.js | @sncf/api-node |
npm install @sncf/api-node |
Stable |
| Ruby | sncf-api-ruby |
gem install sncf-api-ruby |
Stable |
| PHP | sncf/api-php-sdk |
composer require sncf/api-php-sdk |
Stable |
Installation
Installation procedures vary by programming language and package manager. Ensure you have the respective language runtime and package manager installed before proceeding. For detailed instructions specific to each SDK, developers should consult the official documentation on the SNCF developer portal.
Python
To install the Python SDK, use pip, the Python package installer:
pip install sncf-api-python
It is recommended to use a Python virtual environment to manage dependencies.
Java
For Java projects, typically managed with Maven or Gradle, add the SDK as a dependency. For Maven, include the following in your pom.xml:
<dependency>
<groupId>com.sncf.api</groupId>
<artifactId>java-sdk</artifactId>
<version>1.0.0</version> <!-- Replace with the latest version -->
</dependency>
Check the SNCF developer documentation for the most current version number.
Node.js
Install the Node.js SDK using npm, the Node.js package manager:
npm install @sncf/api-node
Alternatively, if using Yarn:
yarn add @sncf/api-node
Ruby
For Ruby projects, use Bundler by adding the gem to your Gemfile and then running bundle install:
gem 'sncf-api-ruby'
Or, install directly via RubyGems:
gem install sncf-api-ruby
PHP
PHP projects typically use Composer for dependency management. Add the SDK to your composer.json and then run composer install:
composer require sncf/api-php-sdk
Quickstart example
This quickstart example demonstrates how to use the Python SDK to fetch information about upcoming train journeys. Before running this code, ensure you have obtained an API key from the SNCF Developer Portal and replaced 'YOUR_API_KEY' with your actual key.
Python Quickstart: Fetching Journeys
This example retrieves journey plans between two specified locations using the Python SDK.
from sncf_api_python import SncfApi
# Replace 'YOUR_API_KEY' with your actual API key
api_key = 'YOUR_API_KEY'
sncf = SncfApi(api_key)
try:
# Example: Search for journeys from Paris Gare de Lyon to Marseille Saint-Charles
# Note: These are example IDs. You would typically use a 'places' or 'coverage' API to find exact stop_area IDs.
# For this example, we'll use common station names as input, assuming the SDK handles resolution.
# In a real application, you'd use specific Navitia stop_area IDs like 'stop_area:SNCF:87686006' for Paris Gare de Lyon
# and 'stop_area:SNCF:87722027' for Marseille Saint-Charles.
# A simplified approach for demonstration, often supported by SDKs for common queries:
from_place = 'Paris Gare de Lyon'
to_place = 'Marseille Saint-Charles'
# The actual API call might look like this, using specific Navitia IDs if available:
# journeys = sncf.journeys.get(from_id='stop_area:SNCF:87686006', to_id='stop_area:SNCF:87722027', datetime='20260601T090000')
# For a more user-friendly SDK abstraction, you might pass names directly, which the SDK resolves:
print(f"Searching for journeys from {from_place} to {to_place}...")
journeys_data = sncf.journeys.get_by_names(from_name=from_place, to_name=to_place, datetime='20260601T090000')
if journeys_data and 'journeys' in journeys_data:
print(f"Found {len(journeys_data['journeys'])} journeys:")
for journey in journeys_data['journeys']:
departure = journey['sections'][0]['departure_date_time'] if journey['sections'] else 'N/A'
arrival = journey['sections'][-1]['arrival_date_time'] if journey['sections'] else 'N/A'
duration = journey['duration'] # Duration in seconds
print(f" - Departure: {departure}, Arrival: {arrival}, Duration: {duration / 60} minutes")
# Print main sections of the journey
for section in journey['sections']:
if section['type'] == 'public_transport':
line_name = section['display_informations']['network'] if 'display_informations' in section and 'network' in section['display_informations'] else 'Train'
from_stop = section['from']['name']
to_stop = section['to']['name']
print(f" {line_name} from {from_stop} to {to_stop}")
elif section['type'] == 'walk':
print(f" Walk from {section['from']['name']} to {section['to']['name']}")
else:
print("No journeys found or an error occurred.")
except Exception as e:
print(f"An error occurred: {e}")
This example assumes the Python SDK provides a get_by_names method or similar abstraction to resolve place names to their internal identifiers. For direct API calls, you would typically first use a search or coverage API endpoint (like Navitia's places endpoint) to obtain the precise stop_area IDs for 'Paris Gare de Lyon' and 'Marseille Saint-Charles'. The datetime format YYYYMMDDTHHMMSS is standard for many transport APIs, as detailed in the Navitia API documentation.
Community libraries
Beyond the officially supported SDKs, the developer community often creates and maintains libraries that interact with public APIs. These community-driven projects can offer alternative language support, specialized functionalities, or different architectural approaches. While not officially endorsed or maintained by Railway Transport for France, they can provide valuable tools for developers. Before integrating a community library, it is advisable to review its documentation, community support, and recent update history to ensure it meets project requirements and security standards. Developers can often find these libraries on platforms like GitHub or language-specific package repositories.
Examples of areas where community libraries might emerge include:
- Specific use-case wrappers: Libraries tailored for niche applications, such as real-time departure boards for specific stations or tools for analyzing historical delay data.
- Alternative language bindings: Support for programming languages not covered by official SDKs, such as Go, C#, or Rust.
- Framework integrations: Libraries that integrate more seamlessly with specific web frameworks (e.g., Django, Flask, Ruby on Rails) by providing helpers for common tasks like routing or data display.
- Data visualization tools: Components or libraries that help visualize train routes, delays, or network coverage on maps, potentially leveraging other mapping APIs such as ArcGIS Developers or Google Maps Platform.
To discover relevant community contributions, developers can typically search package managers (e.g., PyPI for Python, npm for Node.js, RubyGems for Ruby) or code hosting platforms like GitHub using keywords such as "SNCF API", "Navitia SDK", or "French railway". Always verify the authenticity and maintenance status of third-party libraries before integrating them into production systems.