Authentication overview
Navitia provides an API for accessing public transport data, including real-time schedules, journey planning, and geographical information. To ensure controlled and secure access to its services, Navitia requires authentication for all API requests. This mechanism allows Navitia to identify the origin of requests, apply rate limits, and manage access based on subscription levels. Proper authentication is a prerequisite for consuming any of Navitia's API endpoints, from journey planning to detailed stop information.
The authentication process is designed to be straightforward, typically involving the inclusion of a unique identifier in your API requests. This identifier acts as a digital key, granting your application permission to interact with the Navitia platform. Adhering to the specified authentication methods and security guidelines is critical for maintaining the integrity and availability of your applications that rely on Navitia's data.
Supported authentication methods
Navitia primarily utilizes API keys for authenticating requests to its services. An API key is a unique token that you include with each request to identify your application. This method is common for web APIs due to its simplicity and ease of implementation. It provides a balance of security and usability for developers building public transit applications.
For more advanced enterprise integrations or specific use cases, discussions with Navitia's support team may reveal alternative or complementary authentication schemes, but the public-facing API largely operates on the API key model. The table below summarizes the primary authentication method:
| Method | When to Use | Security Level |
|---|---|---|
| API Key | All standard API interactions, server-side applications, and client-side applications where the key can be securely managed. | Moderate (requires secure storage and transmission) |
While API keys offer convenience, it is important to understand their limitations and implement practices that mitigate potential security risks, such as unauthorized access or key compromise. The Open Web Application Security Project (OWASP) provides guidance on API security best practices, which can be applied when working with API keys.
Getting your credentials
To begin using the Navitia API, you need to obtain an API key. This key serves as your primary credential for authenticating requests. The process typically involves registering an account on the Navitia developer portal and creating an application.
- Register on the Navitia Developer Portal: Navigate to the official Navitia documentation page (Navitia documentation portal). Look for a section related to API access or developer registration. You will likely need to provide an email address and create a password to set up your account.
- Create an Application: Once registered and logged in, you will typically find a dashboard or a section to manage your applications. Here, you can create a new application, which will be associated with your API key. You might need to provide a name for your application and, optionally, a description or its intended use.
- Generate/Retrieve API Key: Upon creating an application, the system will generate a unique API key for it. This key will be displayed on your application's details page. It is crucial to copy this key immediately and store it securely, as it may not be displayed again for security reasons, or you might only be able to view a masked version. If you lose your key, you may need to generate a new one.
- Review Usage Policies: Before making requests, familiarize yourself with Navitia's usage policies, rate limits, and terms of service, which are usually outlined within the developer portal or linked from it. This ensures compliance and helps in designing your application to function within the allowed parameters.
Navitia offers a Sandbox environment for testing purposes, which allows developers to experiment with the API using a limited number of requests. This is an ideal starting point to test your authentication setup before moving to a production environment.
Authenticated request example
Once you have obtained your API key, you will typically include it in the HTTP headers of your requests. Navitia's API expects the key to be passed in an Authorization header, prefixed with Token. This is a common pattern for API key authentication, as described in various API design guides, including those by Cloudflare's API documentation.
Here's an example of an authenticated request using curl, which is a widely used 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: Token YOUR_API_KEY'
In this example:
-X GETspecifies the HTTP method (GET).'https://api.navitia.io/v1/coverage/fr-idf/lines?start_page=0&count=10'is the target API endpoint, requesting a list of lines within the Île-de-France (fr-idf) coverage area.-H 'Authorization: Token YOUR_API_KEY'is the critical part for authentication. You must replaceYOUR_API_KEYwith the actual API key you obtained from the Navitia developer portal.
For applications developed with Python or JavaScript, Navitia provides SDKs that simplify the process of making authenticated requests. These SDKs often abstract away the details of header construction, allowing you to configure your API key once and have it applied to all subsequent requests.
Python SDK Example:
import navitia_client
# Initialize the client with your API key
client = navitia_client.Client(api_key="YOUR_API_KEY")
# Make an authenticated request
response = client.lines.get_lines(region="fr-idf", count=10)
for line in response.lines:
print(line.name)
JavaScript SDK Example:
import { NavitiaClient } from 'navitia-client';
// Initialize the client with your API key
const client = new NavitiaClient('YOUR_API_KEY');
// Make an authenticated request
client.lines.getLines('fr-idf', { count: 10 })
.then(response => {
response.lines.forEach(line => console.log(line.name));
})
.catch(error => console.error('Error fetching lines:', error));
These examples demonstrate how to integrate your API key into requests, whether directly via HTTP headers or through the convenience of an SDK.
Security best practices
Securing your Navitia API key is paramount to protect your application from unauthorized access, prevent misuse of your allocated request quotas, and safeguard sensitive data. Adhering to these best practices will help maintain the integrity and security of your integration:
- Do Not Embed Keys Directly in Client-Side Code: Never hardcode your API key directly into client-side code (e.g., JavaScript in a web browser, mobile application bundles). This exposes your key to anyone who inspects your application's code, making it vulnerable to extraction and misuse. Instead, use a backend proxy or server-side component to make requests to Navitia on behalf of your client.
- Use Environment Variables for Server-Side Applications: For server-side applications, store your API key in environment variables rather than directly in your codebase. This prevents the key from being committed to version control systems (like Git) and keeps it separate from your application logic. Most cloud platforms and CI/CD pipelines offer secure ways to manage environment variables.
- Restrict API Key Usage (if applicable): While Navitia's API keys currently apply broadly, some APIs allow you to restrict API keys by IP address or HTTP referrer. If Navitia introduces such features in the future, configure your key to only accept requests from your authorized servers or domains.
- Rotate API Keys Regularly: Periodically generate new API keys and revoke old ones. This practice reduces the window of opportunity for a compromised key to be exploited. The exact frequency depends on your security policy and the sensitivity of your application.
- Monitor API Usage: Regularly check your API usage statistics within the Navitia developer portal. Unexpected spikes in usage could indicate that your API key has been compromised. Set up alerts if the platform supports them.
- Secure Your Development Environment: Ensure that your development machines and build pipelines are secure. Malicious software or improper access controls in these environments can lead to the compromise of your API keys before they even reach production.
- Use HTTPS for All Communications: All communication with the Navitia API should occur over HTTPS (TLS). This encrypts the data in transit, protecting your API key and other sensitive information from eavesdropping. Navitia's API endpoints are designed to enforce HTTPS.
- Implement a Backend Proxy: For client-side applications (e.g., single-page applications, mobile apps), implement a secure backend proxy server. Your client application sends requests to your proxy, which then adds the API key and forwards the request to Navitia. This keeps the API key hidden from the client and allows for additional logging, rate limiting, and caching logic on your side.
- Educate Your Team: Ensure that all developers and operations personnel are aware of these security best practices and understand the importance of safeguarding API credentials.
By diligently applying these security measures, you can significantly reduce the risk of API key compromise and ensure the reliable and secure operation of your applications powered by Navitia's public transport data.