Open-Meteo Python SDK
The Open-Meteo Python SDK provides FlatBuffer data models for interacting with the Open-Meteo weather API. It allows developers to construct API requests and parse API responses using highly efficient binary serialization. The library is actively maintained with frequent updates (often monthly or bi-monthly) to support new weather models and variables as the Open-Meteo API evolves.
Warnings
- gotcha The `openmeteo-sdk` primarily provides FlatBuffer data models (schemas and generated classes) for constructing API requests and parsing responses. It does NOT include an HTTP client. You must use a separate library like `requests` to send the serialized FlatBuffer request and receive the binary response.
- gotcha Accessing data from the `WeatherApiResponse` and other FlatBuffer objects requires specific methods (e.g., `Hourly()`, `Variables(i)`, `Values(j)`, `Variable()`) rather than direct attribute access. This is due to the FlatBuffers design for efficient memory access.
- gotcha The Open-Meteo API is highly configurable. Requesting variables or models that are incompatible or unavailable for a given latitude/longitude or time range can result in empty data arrays in the response without an explicit error from the SDK itself. The SDK will simply parse what the API returns.
Install
-
pip install openmeteo-sdk
Imports
- WeatherApiRequest
from openmeteo_sdk.WeatherApiRequest import WeatherApiRequest
- WeatherApiResponse
from openmeteo_sdk.WeatherApiResponse import WeatherApiResponse
- Variables
from openmeteo_sdk.Variables import Variables
- flatbuffers
import flatbuffers
Quickstart
import requests
import flatbuffers
from openmeteo_sdk.WeatherApiRequest import WeatherApiRequest
from openmeteo_sdk.WeatherApiResponse import WeatherApiResponse
from openmeteo_sdk.Variables import Variables
def get_weather_data(latitude: float, longitude: float):
builder = flatbuffers.Builder(0)
request = WeatherApiRequest.WeatherApiRequest.StartWeatherApiRequest(builder)
WeatherApiRequest.WeatherApiRequest.AddLatitude(request, latitude)
WeatherApiRequest.WeatherApiRequest.AddLongitude(request, longitude)
WeatherApiRequest.WeatherApiRequest.AddHourly(request, Variables.Variables().TEMPERATURE_2M)
WeatherApiRequest.WeatherApiRequest.AddDaily(request, Variables.Variables().SUNSHINE_DURATION)
WeatherApiRequest.WeatherApiRequest.AddTimezone(request, builder.CreateString("Europe/Berlin"))
request_object = WeatherApiRequest.WeatherApiRequest.EndWeatherApiRequest(builder)
builder.Finish(request_object)
# Send request to Open-Meteo API
response = requests.post(
"https://api.open-meteo.com/v1/forecast?models=gfs_seamless",
data=builder.Output(),
headers={
"Content-Type": "application/flatbuffers",
"Accept": "application/flatbuffers"
}
)
response.raise_for_status()
# Parse response
weather_api_response = WeatherApiResponse.WeatherApiResponse.GetRootAsWeatherApiResponse(response.content)
# Access hourly data
if weather_api_response.Hourly() is not None:
hourly_temperatures = []
for i in range(weather_api_response.Hourly().VariablesLength()):
variable = weather_api_response.Hourly().Variables(i)
if variable.Variable() == Variables.Variables().TEMPERATURE_2M:
for j in range(variable.ValuesLength()):
hourly_temperatures.append(variable.Values(j))
print(f"Hourly Temperatures (2m): {hourly_temperatures[:5]}...") # Print first 5 for brevity
# Access daily data
if weather_api_response.Daily() is not None:
daily_sunshine = []
for i in range(weather_api_response.Daily().VariablesLength()):
variable = weather_api_response.Daily().Variables(i)
if variable.Variable() == Variables.Variables().SUNSHINE_DURATION:
for j in range(variable.ValuesLength()):
daily_sunshine.append(variable.Values(j))
print(f"Daily Sunshine Duration: {daily_sunshine[:5]}...") # Print first 5 for brevity
if __name__ == '__main__':
get_weather_data(latitude=52.52, longitude=13.41)