SDKs overview

Aemet (Agencia Estatal de Meteorología) offers an Open Data API that provides access to a range of meteorological information for Spain, including weather forecasts, climatological data, and observation records. While Aemet's primary interface is a RESTful API, developers can utilize Software Development Kits (SDKs) and libraries to streamline integration processes. These tools abstract the underlying HTTP requests and JSON parsing, allowing developers to interact with the Aemet API using native language constructs.

The Aemet API requires an API key for authentication, which is obtained through registration on the official Aemet Open Data portal. This key must be included in the HTTP headers of all API requests. The API documentation, which details available endpoints and data structures, is primarily available in Spanish, with code examples provided to illustrate usage patterns.

SDKs and libraries for Aemet typically handle:

  • Authentication: Managing the inclusion of the API key in request headers.
  • Endpoint Abstraction: Providing functions or methods that map directly to Aemet API endpoints (e.g., get_current_weather, get_daily_forecast).
  • Data Parsing: Converting the JSON responses from the Aemet API into language-specific data structures (e.g., Python dictionaries, objects).
  • Error Handling: Implementing mechanisms to catch and interpret API-specific error codes and messages.

These tools are designed to reduce boilerplate code and potential integration errors, accelerating development cycles for applications that rely on Spanish weather data.

Official SDKs by language

Aemet officially supports a Python library for interacting with its Open Data API. This library is maintained by Aemet and serves as the primary recommended method for Python developers to access meteorological data programmatically. The official documentation for the Aemet API provides specific guidance on using this library, alongside direct API call examples.

Language Package/Library Name Installation Command Maturity
Python aemet pip install aemet Official, Maintained

Installation

The installation process for the official Aemet Python SDK is performed using pip, the standard package installer for Python. Before installation, ensure you have Python 3.x and pip installed on your system. Instructions for installing Python and pip can be found on the official Python website.

To install the aemet library, open your terminal or command prompt and execute the following command:

pip install aemet

This command downloads the latest version of the aemet package from the Python Package Index (PyPI) and installs it along with any necessary dependencies.

After installation, you can verify that the library is correctly installed by attempting to import it in a Python interpreter:

import aemet
print(aemet.__version__)

If no errors occur and a version number is printed, the installation was successful.

Quickstart example

This quickstart example demonstrates how to use the official Aemet Python SDK to retrieve the current weather forecast for a specific municipality in Spain. Before running this code, ensure you have obtained an API key from the Aemet Open Data portal and replaced YOUR_API_KEY with your actual key.

The example focuses on fetching the daily forecast for Madrid. The municipality code (28079 for Madrid) is a key identifier used by Aemet to specify locations. These codes can typically be found within the Aemet API documentation or through specific API endpoints designed to list municipalities.


import aemet
import os

# --- Configuration --- 
# Replace with your actual Aemet API Key
API_KEY = os.environ.get("AEMET_API_KEY", "YOUR_API_KEY") 

# Madrid municipality code
MUNICIPALITY_CODE = "28079"

# --- Initialize Aemet client ---
# The client handles authentication and API requests.
try:
    aemet_client = aemet.Aemet(api_key=API_KEY)
except Exception as e:
    print(f"Error initializing Aemet client: {e}")
    exit()

# --- Fetch daily forecast for Madrid ---
print(f"Fetching daily forecast for municipality code: {MUNICIPALITY_CODE}")
try:
    # The get_daily_forecast method returns a dictionary of forecast data.
    forecast_data = aemet_client.get_daily_forecast(MUNICIPALITY_CODE)
    
    if forecast_data:
        print("\n--- Madrid Daily Forecast ---")
        # Accessing specific elements from the forecast data structure.
        # The structure can vary; refer to Aemet API documentation for full details.
        
        # Example: print general state and temperatures
        for day_forecast in forecast_data.get('prediccion', {}).get('dia', []):
            date = day_forecast.get('fecha')
            estado_cielo = day_forecast.get('estadoCielo', [{}])[0].get('descripcion')
            temperatura_max = day_forecast.get('temperatura', {}).get('maxima')
            temperatura_min = day_forecast.get('temperatura', {}).get('minima')
            
            print(f"Date: {date}")
            print(f"  Sky: {estado_cielo}")
            print(f"  Max Temp: {temperatura_max}°C")
            print(f"  Min Temp: {temperatura_min}°C")
            print("-------------------------")
    else:
        print("No forecast data received or an error occurred.")

except aemet.AemetException as e:
    print(f"Aemet API error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

This script first initializes the aemet.Aemet client with your API key. It then calls get_daily_forecast with Madrid's municipality code. The returned data is a Python dictionary, which is then parsed to extract and print key forecast details such as the date, sky conditions, and maximum/minimum temperatures. Error handling is included to catch potential issues during API communication or data processing.

Community libraries

Beyond the official Python SDK, the Aemet API's open nature has led to the development of several community-contributed libraries and wrappers in various programming languages. These libraries are typically open-source projects hosted on platforms like GitHub and offer alternative ways to interact with the Aemet API, sometimes with different feature sets or language preferences.

While community libraries can provide flexibility and support for languages not officially covered, developers should consider several factors when choosing to use them:

  • Maintenance Status: Community projects may vary in their level of active maintenance, bug fixes, and compatibility with the latest Aemet API changes. Check the project's commit history and issue tracker.
  • Documentation Quality: Documentation for community libraries can range from comprehensive to minimal. Ensure the library provides sufficient guidance for integration.
  • Feature Completeness: Some community libraries might implement only a subset of the Aemet API's endpoints. Verify that the library supports the specific data you need.
  • Community Support: The availability of community forums or active contributors can be valuable for troubleshooting and getting assistance.
  • Security Considerations: When using third-party libraries, it is prudent to review their source code for any potential security vulnerabilities, especially when handling API keys or sensitive data. Information on secure coding practices can be found from organizations like the Open Web Application Security Project (OWASP).

Examples of community libraries can often be found by searching public code repositories for terms like "Aemet API" or "Aemet weather" combined with specific programming languages. These may include wrappers for JavaScript, PHP, or other environments, though their stability and feature set are not guaranteed by Aemet.

Developers are encouraged to consult the official Aemet Open Data API documentation for the most accurate and up-to-date information regarding API endpoints, data formats, and usage policies, regardless of whether an official or community library is used.