Authentication overview
Access to the Railway Transport for France APIs, which are built on the Navitia platform, requires authentication for all requests. This ensures that API usage can be monitored, managed, and secured, preventing unauthorized access to the underlying data and services. The primary method for authentication involves using a unique API key, often referred to as a token, which identifies the application making the request.
Upon successful registration on the Railway Transport for France developer portal, a developer is issued an API key. This key must be included in the header of every API request. The system verifies this key against its records to confirm the request's legitimacy and to enforce any associated usage policies, such as rate limits defined by the free sandbox or paid tiers. This token-based approach is a common practice for RESTful APIs, providing a balance between security and ease of implementation for developers.
Supported authentication methods
The Railway Transport for France API primarily supports a single, streamlined authentication method: API Key authentication. This method is suitable for server-to-server communication and client-side applications where the key can be securely stored or managed.
| Method | When to Use | Security Level |
|---|---|---|
| API Key (Token-based) |
|
Moderate. Requires careful key management to prevent exposure. |
While other authentication protocols like OAuth 2.0 are widely adopted for user authorization flows, the Railway Transport for France APIs currently focus on straightforward API key usage for application-level authentication. This simplifies the integration process for developers seeking to access public transport data efficiently.
Getting your credentials
To obtain your API key for the Railway Transport for France APIs, follow these steps:
- Register on the Developer Portal: Navigate to the official Railway Transport for France developer portal. You will need to create an account, which typically involves providing an email address and setting a password.
- Access Your Dashboard: After registration and email verification, log in to your developer dashboard. This is usually where you manage your applications and API keys.
- Create a New Application: The portal will guide you through creating a new application. During this process, you may need to provide details about your project, such as its name and a brief description of its purpose.
- Generate Your API Key: Once your application is set up, the portal will generate a unique API key for it. This key is crucial for authenticating your requests. It's important to copy this key immediately and store it in a secure location, as it may not be retrievable directly from the portal again for security reasons.
- Understand Usage Tiers: Initially, your API key will likely be associated with the free Sandbox tier, offering up to 1000 requests per day. For higher usage limits, you will need to review the pricing page and upgrade to a paid plan, which entails a separate process on the developer portal.
The developer portal provides an interactive API explorer and comprehensive documentation, which can be helpful during the credential setup process and for testing your API key's functionality. For detailed instructions, consult the SNCF developer documentation.
Authenticated request example
Once you have obtained your API key, you can include it in your API requests. The standard method is to pass the API key in the Authorization header as a Bearer token. This example demonstrates an authenticated request using cURL, a common command-line tool for making HTTP requests.
curl -X GET \
'https://api.navitia.io/v1/coverage/fr-idf/lines?start_page=0&count=10' \
-H 'Authorization: Bearer YOUR_API_KEY'
In this example:
YOUR_API_KEYshould be replaced with the actual API key you obtained from the Railway Transport for France developer portal.https://api.navitia.io/v1/coverage/fr-idf/linesis an example endpoint for retrieving lines in the Île-de-France region. The specific endpoint will vary based on the data you wish to access, as detailed in the Navitia API reference.- The
-H 'Authorization: Bearer YOUR_API_KEY'part of the command adds the necessary HTTP header for authentication. TheBearerscheme is a widely used token type for HTTP authentication, as specified by RFC 6750 for Bearer Token Usage.
For programmatic examples in various languages, Railway Transport for France offers SDKs for Python, Java, Node.js, Ruby, and PHP. These SDKs typically abstract the header construction, making it easier to integrate authentication into your application code.
Security best practices
Securing your API keys is critical to prevent unauthorized access to your Railway Transport for France API quota and potential misuse of your application's identity. Adhere to these best practices:
- Never hardcode API keys: Avoid embedding API keys directly into your source code. Instead, use environment variables, configuration files, or a secure secrets management service. For example, AWS offers AWS Secrets Manager, and Google Cloud provides Secret Manager for Google Cloud to securely store and manage API keys and other sensitive credentials.
- Use environment variables for server-side applications: When deploying server-side applications, configure your API key as an environment variable. This keeps the key out of the codebase and allows for easy rotation without code changes.
- Employ proxy servers for client-side applications: For browser-based or mobile applications, make API requests through a secure backend proxy server. The proxy server adds the API key to the request before forwarding it to the Railway Transport for France API. This prevents the API key from being exposed in client-side code, which could be inspected by users.
- Restrict API key privileges: While the Railway Transport for France API keys typically grant access to specific data sets rather than user accounts, it's a general security principle to limit the scope of any API key to the minimum necessary permissions. Review the developer documentation for any granular permission settings that may apply.
- Implement rate limiting on your end: Even though the Railway Transport for France API enforces its own rate limits, implementing client-side rate limiting can help prevent accidental overuse of your quota and protect against denial-of-service attacks originating from your application.
- Regularly rotate API keys: Periodically generate new API keys and revoke old ones. This minimizes the window of opportunity for a compromised key to be exploited. The Railway Transport for France developer portal should provide functionality for key rotation.
- Monitor API usage: Regularly check your API usage statistics on the developer dashboard. Unusual spikes in usage could indicate a compromised key or an issue with your application.
- Secure your development environment: Ensure that your local development environment and version control systems are secure. Do not commit API keys or sensitive credentials to public repositories (e.g., GitHub). Utilize
.gitignorefiles to exclude sensitive configuration files from version control.