Getting started overview
Integrating with Transport for Norway's (Entur) APIs involves a straightforward process from account creation to making your first authenticated request. The platform is designed to provide developers with access to comprehensive public transport data across Norway, including journey planning, real-time departures, and detailed stop information. This guide outlines the essential steps to get your application connected and retrieving data.
The core of the integration revolves around obtaining API keys, which are necessary for authenticating every request to the Entur API endpoints. Entur provides a developer portal with tools like an API playground to facilitate testing and development. Understanding the authentication mechanism, typically via an API key in the request headers, is crucial for successful interaction.
Here's a quick reference table for the steps involved:
| Step | What to Do | Where |
|---|---|---|
| 1. Sign Up | Register for a developer account. | Entur Developer Portal |
| 2. Get API Keys | Generate your unique API key(s) after registration. | Entur Developer Portal (Dashboard/Applications section) |
| 3. Understand Endpoints | Review available API endpoints and their functions. | Entur API documentation |
| 4. Make First Request | Construct and send an authenticated API call. | Your preferred development environment (e.g., cURL, Postman, custom code) |
| 5. Explore Data | Process and utilize the returned JSON data. | Your application logic |
Create an account and get keys
To access Transport for Norway's APIs, you must first create a developer account. This process typically involves registering on the Entur Developer Portal. During registration, you will likely provide an email address and create a password. Once your account is active, you can proceed to generate your API keys.
API keys are essential for authenticating your requests and ensuring secure access to the data. Entur's system uses these keys to identify your application and manage usage. Generally, you will find an 'Applications' or 'API Keys' section within your developer dashboard where you can create new keys. It's common practice to generate separate keys for different applications or environments (e.g., development, staging, production) to enhance security and simplify key management.
When you generate a key, make sure to copy it immediately, as some platforms only display the full key upon creation. Treat your API keys as sensitive credentials; do not embed them directly in client-side code, commit them to public version control systems, or share them unnecessarily. Instead, use environment variables or a secure configuration management system to store and retrieve them.
For detailed instructions on account creation and API key generation, refer to the official Entur API documentation. This documentation provides specific steps and best practices for managing your credentials.
Your first request
Once you have obtained your API key, you are ready to make your first authenticated request. Transport for Norway's APIs typically use a common authentication pattern where the API key is included in the request headers. For instance, you might include an X-API-Key header with your generated key.
Let's use the Journey Planner API as an example for a first request. This API allows you to query for public transport routes between two locations in Norway. The Entur API uses GraphQL, which means you send a single query string to a specific endpoint rather than hitting multiple RESTful endpoints. This approach can simplify data fetching by allowing you to request exactly what you need.
A common endpoint for GraphQL queries might be https://api.entur.io/journey-planner/v3/graphql. You would send a POST request to this endpoint with a JSON body containing your GraphQL query.
Example GraphQL Query (Journey Planner)
To find a journey, you might construct a GraphQL query like this. Note that actual fields and parameters may vary; consult the Entur API reference for precise details.
query JourneyQuery {
trip(from: {place: "Nordre Gate 1", coordinates: {latitude: 59.9139, longitude: 10.7522}}, to: {place: "Oslo S", coordinates: {latitude: 59.9113, longitude: 10.7525}}) {
journeys {
legs {
mode
from {
name
}
to {
name
}
duration
}
}
}
}
Making the Request with cURL
You can use cURL in your terminal to send this request. Replace YOUR_API_KEY with your actual API key.
curl -X POST \
https://api.entur.io/journey-planner/v3/graphql \
-H 'Content-Type: application/json' \
-H 'ET-Client-Name: Your-Application-Name' \
-H 'X-API-Key: YOUR_API_KEY' \
-d '{"query": "query JourneyQuery { trip(from: {place: \"Nordre Gate 1\", coordinates: {latitude: 59.9139, longitude: 10.7522}}, to: {place: \"Oslo S\", coordinates: {latitude: 59.9113, longitude: 10.7525}}) { journeys { legs { mode from { name } to { name } duration } } } }"}'
This cURL command sends a POST request with the GraphQL query in the request body, along with the necessary headers for content type and authentication. The ET-Client-Name header is often required by Entur to identify your application for logging and usage tracking purposes.
Expected Response
A successful response will return a JSON object containing journey information, structured according to your GraphQL query. It will likely include details about different journey legs, modes of transport, departure and arrival points, and durations. For example:
{
"data": {
"trip": {
"journeys": [
{
"legs": [
{
"mode": "foot",
"from": {
"name": "Nordre Gate 1"
},
"to": {
"name": "Jernbanetorget"
},
"duration": 300
},
{
"mode": "bus",
"from": {
"name": "Jernbanetorget"
},
"to": {
"name": "Oslo S"
},
"duration": 180
}
]
}
]
}
}
}
This example demonstrates a basic journey with a walking leg and a bus leg. The actual response will be more comprehensive, dependent on the specific query and available data.
Common next steps
After successfully making your first request, consider these common next steps to further integrate Transport for Norway's APIs into your application:
- Explore Other APIs: Beyond the Journey Planner, investigate the Real-time Data API for live departure/arrival information and the Stop Place API for detailed information about stops and stations. Each API serves different use cases and can enrich your application's functionality. The Entur API documentation provides comprehensive details on all available endpoints.
- Implement Error Handling: Develop robust error handling mechanisms to gracefully manage API responses that indicate issues, such as invalid API keys, rate limits, or malformed requests. This ensures a stable user experience even when unexpected problems occur.
- Manage Rate Limits: Be aware of the API rate limits associated with your account tier. Implement strategies like exponential backoff for retries to avoid exceeding these limits, which can lead to temporary blocking of your API access. For general guidance on rate limiting, refer to resources like Cloudflare's API Rate Limits documentation.
- Integrate into Your Application: Start integrating the API calls into your actual application logic. This involves parsing the JSON responses and displaying the data in a user-friendly format. Consider using client libraries if available for your programming language, which can simplify the interaction with GraphQL APIs.
- Secure API Keys: Reiterate the importance of securely storing and accessing your API keys. Avoid hardcoding them directly into your application's source code. Instead, use environment variables or a secure secrets management service.
- Monitor Usage: Utilize any provided dashboards or tools within the Entur Developer Portal to monitor your API usage. This helps in understanding your consumption patterns and anticipating when you might need to adjust your plan or optimize your calls.
- Stay Updated: Regularly check the Entur Developer Portal for API updates, new features, and deprecation notices. Subscribing to developer newsletters or RSS feeds can help you stay informed.
Troubleshooting the first call
Encountering issues during your first API call is common. Here are some troubleshooting steps to diagnose and resolve typical problems:
- Check API Key: Ensure your API key is correctly copied and included in the
X-API-Keyheader. Even a small typo can lead to authentication failures. Verify there are no leading or trailing spaces. - Verify Endpoint URL: Double-check that the API endpoint URL in your request matches the one specified in the Entur API documentation. Endpoints can change or have specific versions.
- Inspect Headers: Confirm that all required headers, such as
Content-Type: application/jsonandET-Client-Name, are present and correctly formatted. Missing or incorrect headers are a frequent cause of errors. - Review Request Body: For GraphQL APIs, the request body containing your query must be valid JSON. Ensure there are no syntax errors, missing commas, or unescaped characters within your GraphQL query string. Tools like Postman or Insomnia can help validate JSON syntax.
- Examine Error Messages: The API response will often include an error message if the call fails. Read these messages carefully, as they usually provide specific clues about what went wrong (e.g., "Invalid API Key", "Missing required parameter").
- Rate Limiting: If you are making many requests in a short period, you might hit rate limits. Wait a few minutes and try again. For production applications, implement proper rate-limiting handling.
- Network Connectivity: Ensure your development environment has stable internet access and is not blocked by a firewall from reaching the Entur API endpoint.
- Consult Documentation: Re-read the Entur API reference for the specific endpoint you are trying to use. Pay close attention to required parameters, data types, and authentication methods.
- Use API Playground: If Entur provides an API playground or sandbox environment, use it to test your queries directly. This can help isolate whether the issue is with your code or the API call itself.
- Community Support: If problems persist, check the Entur developer community forums or support channels. Other developers may have encountered similar issues and found solutions.