Authentication overview

Foreca's Weather API utilizes API keys as the primary method for authenticating developer requests. This approach ensures that only authorized applications can access and retrieve weather data. When an API key is provided with a request, the Foreca API backend verifies its validity and the permissions associated with the key, granting or denying access accordingly.

The API key acts as a unique identifier for your application, linking your requests to your Foreca developer account and your subscribed plan. This enables Foreca to manage access limits, monitor usage, and provide analytics specific to your integration. All communication with the Foreca API should occur over HTTPS/TLS to encrypt data in transit, protecting both the API key and the weather data exchanged Foreca API documentation on security.

While API keys are straightforward to implement, their security relies heavily on proper handling and storage. Developers are responsible for safeguarding their API keys to prevent unauthorized access to their Foreca API quota and data. Best practices include storing keys securely, avoiding hardcoding them, and rotating them periodically.

Supported authentication methods

Foreca primarily supports API key authentication for accessing its Weather API. This method is suitable for most application types, from server-side integrations to client-side requests where appropriate security measures are in place.

Method When to Use Security Level
API Key
  • Server-side applications
  • Backend services
  • Web applications where the key can be securely stored and managed
  • Integrations where simplicity and direct access control are prioritized
Moderate (dependent on secure key management)

API keys are typically passed as a query parameter or an HTTP header with each request. Foreca's API reference specifies the exact parameter name and location for transmitting the API key Foreca API reference details. While other authentication methods like OAuth 2.0 are common for user-centric APIs OAuth 2.0 specification, Foreca's focus on programmatic access to weather data makes API keys an efficient and effective solution for developer integrations.

Getting your credentials

To obtain your Foreca API key, you must register for a developer account on the Foreca Developer Portal. The process generally involves the following steps:

  1. Register for a Developer Account: Navigate to the Foreca Developer Portal and sign up. This typically requires providing an email address and creating a password.
  2. Choose a Plan: Select a suitable plan. Foreca offers a free Developer Plan that includes 500 API calls per month, which is sufficient for testing and small-scale projects. Paid plans are available for higher call volumes and additional features Foreca pricing page.
  3. Generate API Key: Once registered and a plan is selected, your API key will be generated and made available in your developer dashboard. This key is unique to your account and should be kept confidential.
  4. Access Dashboard: Log into your developer dashboard to retrieve your API key. The dashboard also provides access to usage statistics, billing information, and further documentation.

It is crucial to store your API key in a secure manner immediately after retrieval. Avoid exposing it in client-side code, public repositories, or unsecured environments. If you suspect your API key has been compromised, you should be able to revoke it and generate a new one from your Foreca developer dashboard.

Authenticated request example

Once you have obtained your API key, you can use it to make authenticated requests to the Foreca Weather API. The API key is typically included as a query parameter named apikey in your request URL. The following examples illustrate how to make a simple request to a Foreca endpoint using your API key.

Assume you want to get the current weather for a specific location. The base URL and endpoint might look like https://api.foreca.com/some_endpoint. You would append your API key to this URL.

HTTP GET Request (Conceptual)

GET /api/v1/current/Helsinki?apikey=YOUR_API_KEY_HERE HTTP/1.1
Host: api.foreca.com
User-Agent: YourApplication/1.0

Replace YOUR_API_KEY_HERE with your actual API key and Helsinki with the desired location or coordinates, as specified by the Foreca API documentation.

cURL Example

A practical way to test an authenticated request from your terminal is using cURL:

curl -X GET "https://api.foreca.com/api/v1/forecast/daily/Helsinki?apikey=YOUR_API_KEY_HERE&alt=0&tempunit=C&windunit=MS&periods=5" \ 
     -H "Accept: application/json"

This cURL command requests a 5-day daily forecast for Helsinki. Remember to substitute YOUR_API_KEY_HERE with your personal API key. The -H "Accept: application/json" header indicates that you prefer the response in JSON format. The Foreca API reference provides detailed information on available endpoints, parameters, and response structures Foreca API reference.

Python Example (using requests library)

import requests

api_key = "YOUR_API_KEY_HERE"
location = "London"
url = f"https://api.foreca.com/api/v1/current/{location}?apikey={api_key}"

try:
    response = requests.get(url)
    response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
    data = response.json()
    print(data)
except requests.exceptions.HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")
except Exception as err:
    print(f"An error occurred: {err}")

This Python snippet demonstrates how to make an authenticated request for current weather data. Ensure you replace YOUR_API_KEY_HERE and London with your actual key and desired location, respectively. Handling potential errors, such as network issues or invalid API keys, is crucial for robust application development.

Security best practices

Securing your Foreca API key and integration is paramount to prevent unauthorized access, misuse of your quota, and potential data breaches. Adhere to these best practices:

  1. Keep API Keys Confidential: Never hardcode API keys directly into client-side code (e.g., JavaScript in a browser), commit them to public version control systems (like GitHub without proper precautions), or expose them in publicly accessible files. They should be treated like passwords.
  2. Use Environment Variables or Secret Management: For server-side applications, store API keys in environment variables or use a dedicated secret management service (e.g., AWS Secrets Manager, Azure Key Vault, Google Cloud Secret Manager). This prevents keys from being directly included in your codebase Google Cloud Secret Manager overview.
  3. Restrict API Key Permissions (if applicable): While Foreca's API keys currently grant general access based on your plan, always check if the API provider offers granular permissions. If so, configure your key with the minimum necessary permissions required for your application's functionality.
  4. Implement Rate Limiting: Even with a valid API key, implement client-side rate limiting in your application to prevent accidental overuse of your API quota and to mitigate the impact of potential DoS attacks if your key were compromised. Foreca also enforces server-side rate limits, but client-side protection adds an extra layer.
  5. Rotate API Keys Periodically: Regularly generate new API keys and revoke old ones. This practice minimizes the window of opportunity for a compromised key to be exploited. A common rotation schedule might be every 90 days, or immediately if a compromise is suspected.
  6. Monitor API Usage: Regularly check your Foreca developer dashboard for API usage statistics. Unusual spikes in activity could indicate a compromised key or an issue with your application's logic.
  7. Use HTTPS/TLS: Always ensure all communication with the Foreca API occurs over HTTPS/TLS. This encrypts the data in transit, protecting your API key and the data payloads from interception by malicious actors Mozilla documentation on TLS.
  8. Configure Firewall Rules: If your application is hosted on a server, configure firewall rules to restrict outbound connections from your server only to necessary endpoints, including api.foreca.com.