OpenAPI Client SDK for National Weather Service API (NWS / weather.gov)
Project description
nws-api-client
Type-safe and developer-friendly Python client for the National Weather Service API.
[!INFO] The first pass was built with Speakeasy, and I've been making some major quality-of-life adjustments.
[!NOTE] The National Weather Service would like to know who is using their API. Please include a user agent with all API requests where value = your company name and email (a contactable email address). This is the same as the HTTP header 'User-Agent'. They'd also like to be able to contact you if there's an issue with your use of the API.
Summary
MODIFIED National Weather Service weather.gov API: OpenAPI Client SDK for National Weather Service API (NWS / weather.gov)
For more information about the API: Full API documentation
Table of Contents
SDK Installation
uv
uv is the Python package manager I use and recommend.
uv add nws-api-client
PIP
pip install nws-api-client
Poetry
poetry add nws-api-client
SDK Example Usage
Text Products
Get your local NWS office's Area Forecast Discussion
# Synchronous Example
from nws_api_client import NwsAPIClient
import os
with NwsAPIClient(
user_agent=os.getenv("NWS_API_CLIENT_USER_AGENT", ""),
) as nac_client:
res = nac_client.products.get_available(type_id="AFD", location_id="LOT")
# Handle response
print(res)
The same SDK client can also be used to make asychronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from nws_api_client import NwsAPIClient
import os
async def main():
async with NwsAPIClient(
user_agent=os.getenv("NWS_API_CLIENT_USER_AGENT", ""),
) as nac_client:
res = await nac_client.products.get_available_async(type_id="AFD", location_id="LOT")
# Handle response
print(res)
asyncio.run(main())
Available Resources and Operations
Available methods
alerts
- get - Returns a specific alert
- get_active - Returns filtered (or all) currently active alerts
- get_active_by_region - Returns active alerts for the given marine region
- get_active_count - Returns info on the number of active alerts
- get_active_for_zone - Returns active alerts for the given NWS public zone/county identifier
- get_aviation_cwa - Returns a list of CWAs from a CWSU for a specific date and sequence
- get_aviation_sigmet - Returns a specific SIGMET/AIRMET
- get_aviation_sigmets_by_atsu_for_date - Returns a list of SIGMET/AIRMETs for the specified ATSU for the specified date
- get_center_weather_advisories - Returns a list of Center Weather Advisories from a CWSU
- get_sigmets_by_atsu - Returns a list of SIGMET/AIRMETs for the specified ATSU
- list_active_for_area - Returns active alerts for the given state or marine area
- list_sigmets - Returns a list of SIGMET/AIRMETs
- list_types - Returns a list of alert types
aviation
- get_aviation_cwa - Returns a list of CWAs from a CWSU for a specific date and sequence
- get_aviation_sigmet - Returns a specific SIGMET/AIRMET
- get_aviation_sigmets_by_atsu_for_date - Returns a list of SIGMET/AIRMETs for the specified ATSU for the specified date
- get_center_weather_advisories - Returns a list of Center Weather Advisories from a CWSU
- get_cwsu - Returns metadata about a Center Weather Service Unit
- get_sigmets_by_atsu - Returns a list of SIGMET/AIRMETs for the specified ATSU
- get_taf - Returns a single Terminal Aerodrome Forecast
- get_tafs - Returns Terminal Aerodrome Forecasts for the specified airport station.
- list_sigmets - Returns a list of SIGMET/AIRMETs
conditions
- get_latest_observation - Returns the latest observation for a station
- get_observation - Returns a single observation
- list_zone_observations - Returns a list of observations for a given zone
forecasts
- get - Returns a textual forecast for a 2.5km grid area
- get_hourly - Returns a textual hourly forecast for a 2.5km grid area
- get_raw - Returns raw numerical forecast data for a 2.5km grid area
- get_taf - Returns a single Terminal Aerodrome Forecast
- get_tafs - Returns Terminal Aerodrome Forecasts for the specified airport station.
- get_zone_forecast - Returns the current zone forecast for a given zone
geographic
- get - Returns a textual forecast for a 2.5km grid area
- get_active - Returns filtered (or all) currently active alerts
- get_active_by_region - Returns active alerts for the given marine region
- get_active_for_zone - Returns active alerts for the given NWS public zone/county identifier
- get_cwsu - Returns metadata about a Center Weather Service Unit
- get_hourly - Returns a textual hourly forecast for a 2.5km grid area
- get_latest_observation - Returns the latest observation for a station
- get_observation - Returns a single observation
- get_office - Returns metadata about a NWS forecast office
- get_point_info - Returns metadata (inc. forecast gridpoint) about a given lat,long pair
- get_raw - Returns raw numerical forecast data for a 2.5km grid area
- get_zone - Returns metadata about a given zone
- get_zone_forecast - Returns the current zone forecast for a given zone
- get_zone_stations - Returns a list of observation stations for a given zone
- get_zones_by_type - Returns a list of zones of a given type
- list_active_for_area - Returns active alerts for the given state or marine area
- list_gridpoint_stations - Returns a list of observation stations usable for a given 2.5km grid area
- list_observation_stations - Returns a list of observation stations.
- list_zone_observations - Returns a list of observations for a given zone
- list_zones - Returns a list of zones
metadata
- get_office - Returns metadata about a NWS forecast office
- get_point_info - Returns metadata (inc. forecast gridpoint) about a given lat,long pair
- get_profiler - Returns metadata about a given radar wind profiler
- get_queue - Returns metadata about a given radar queue
- get_radar_station - Returns metadata about a given radar station
- get_server - Returns metadata about a given radar server
- get_station_alarms - Returns metadata about a given radar station alarms
- get_zone - Returns metadata about a given zone
- get_zone_stations - Returns a list of observation stations for a given zone
- list_observation_stations - Returns a list of observation stations.
- list_servers - Returns a list of radar servers
- list_stations - Returns a list of radar stations
products
- get - Returns a specific text product
- get_available - Returns a list of text products of a given type for a given issuance location
- get_office_headline - Returns a specific news headline for a given NWS office
- list_by_type - Returns a list of text products of a given type
- list_glossary_terms - List glossary terms
- list_issuing_locations - Returns a list of valid text product issuance locations
- list_locations_by_type - Returns a list of valid text product issuance locations for a given product type
- list_office_headlines - Returns a list of news headlines for a given NWS office
- list_products - Returns a list of text products
- list_types - Returns a list of valid text product types and codes
- list_types_by_location - Returns a list of valid text product types for a given issuance location
radar
- get_profiler - Returns metadata about a given radar wind profiler
- get_queue - Returns metadata about a given radar queue
- get_radar_station - Returns metadata about a given radar station
- get_server - Returns metadata about a given radar server
- get_station_alarms - Returns metadata about a given radar station alarms
- list_servers - Returns a list of radar servers
- list_stations - Returns a list of radar stations
stations
- get_observations - Returns a list of observations for a given station
- get_station - Returns metadata about a given observation station
Global Parameters
A parameter is configured globally. This parameter may be set on the SDK client instance itself during initialization. When configured as an option during SDK initialization, This global value will be used as the default on the operations that use it. When such operations are called, there is a place in each to override the global value, if needed.
For example, you can set officeId to components.NWSOfficeID.MSO at SDK initialization and then you do not have to pass the same value on calls to operations like get_office. But if you want to do so you may, which will locally override the global setting. See the example code below for a demonstration.
Available Globals
The following global parameter is available. Global parameters can also be set via environment variable.
| Name | Type | Description | Environment |
|---|---|---|---|
| office_id | components.NWSOfficeID | NWS office ID | NWS_API_CLIENT_OFFICE_ID |
Example
from nws_api_client import NwsAPIClient
from nws_api_client.models import components
import os
with NwsAPIClient(
user_agent=os.getenv("NWS_API_CLIENT_USER_AGENT", ""),
) as nac_client:
res = nac_client.geographic.get_office(office_id=components.NWSOfficeID.GGW)
# Handle response
print(res)
Error Handling
Handling errors in this SDK should largely match your expectations. All operations return a response object or raise an exception.
By default, an API error will raise a errors.NWSAPIError exception, which has the following properties:
| Property | Type | Description |
|---|---|---|
.status_code |
int | The HTTP status code |
.message |
str | The error message |
.raw_response |
httpx.Response | The raw HTTP response |
.body |
str | The response content |
When custom error responses are specified for an operation, the SDK may also raise their associated exceptions. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the get_async method may raise the following exceptions:
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.ProblemDetailError | 400 | application/problem+json |
| errors.NWSAPIError | 4XX, 5XX | */* |
Example
from nws_api_client import NwsAPIClient
from nws_api_client.models import errors
import os
with NwsAPIClient(
user_agent=os.getenv("NWS_API_CLIENT_USER_AGENT", ""),
) as nac_client:
res = None
try:
res = nac_client.alerts.get(id="urn:oid:2.49.0.1.840.0.404b3149af6da23b497bb9705fadc6ead564a967.005.1")
# Handle response
print(res)
except errors.ProblemDetailError as e:
# handle e.data: errors.ProblemDetailErrorData
raise(e)
except errors.NWSAPIError as e:
# handle exception
raise(e)
Custom HTTP Client
The Python SDK makes API calls using the httpx HTTP library. In order to provide a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration, you can initialize the SDK client with your own HTTP client instance.
Depending on whether you are using the sync or async version of the SDK, you can pass an instance of HttpClient or AsyncHttpClient respectively, which are Protocol's ensuring that the client has the necessary methods to make API calls.
This allows you to wrap the client with your own custom logic, such as adding custom headers, logging, or error handling, or you can just pass an instance of httpx.Client or httpx.AsyncClient directly.
For example, you could specify a header for every request that this sdk makes as follows:
from nws_api_client import NwsAPIClient
import httpx
http_client = httpx.Client(headers={"x-custom-header": "someValue"})
s = NwsAPIClient(client=http_client)
or you could wrap the client with your own custom logic:
from nws_api_client import NwsAPIClient
from nws_api_client.httpclient import AsyncHttpClient
import httpx
class CustomClient(AsyncHttpClient):
client: AsyncHttpClient
def __init__(self, client: AsyncHttpClient):
self.client = client
async def send(
self,
request: httpx.Request,
*,
stream: bool = False,
auth: Union[
httpx._types.AuthTypes, httpx._client.UseClientDefault, None
] = httpx.USE_CLIENT_DEFAULT,
follow_redirects: Union[
bool, httpx._client.UseClientDefault
] = httpx.USE_CLIENT_DEFAULT,
) -> httpx.Response:
request.headers["Client-Level-Header"] = "added by client"
return await self.client.send(
request, stream=stream, auth=auth, follow_redirects=follow_redirects
)
def build_request(
self,
method: str,
url: httpx._types.URLTypes,
*,
content: Optional[httpx._types.RequestContent] = None,
data: Optional[httpx._types.RequestData] = None,
files: Optional[httpx._types.RequestFiles] = None,
json: Optional[Any] = None,
params: Optional[httpx._types.QueryParamTypes] = None,
headers: Optional[httpx._types.HeaderTypes] = None,
cookies: Optional[httpx._types.CookieTypes] = None,
timeout: Union[
httpx._types.TimeoutTypes, httpx._client.UseClientDefault
] = httpx.USE_CLIENT_DEFAULT,
extensions: Optional[httpx._types.RequestExtensions] = None,
) -> httpx.Request:
return self.client.build_request(
method,
url,
content=content,
data=data,
files=files,
json=json,
params=params,
headers=headers,
cookies=cookies,
timeout=timeout,
extensions=extensions,
)
s = NwsAPIClient(async_client=CustomClient(httpx.AsyncClient()))
Resource Management
The NwsAPIClient class implements the context manager protocol and registers a finalizer function to close the underlying sync and async HTTPX clients it uses under the hood. This will close HTTP connections, release memory and free up other resources held by the SDK. In short-lived Python programs and notebooks that make a few SDK method calls, resource management may not be a concern. However, in longer-lived programs, it is beneficial to create a single SDK instance via a context manager and reuse it across the application.
from nws_api_client import NwsAPIClient
import os
def main():
with NwsAPIClient(
user_agent=os.getenv("NWS_API_CLIENT_USER_AGENT", ""),
) as nac_client:
# Rest of application here...
# Or when using async:
async def amain():
async with NwsAPIClient(
user_agent=os.getenv("NWS_API_CLIENT_USER_AGENT", ""),
) as nac_client:
# Rest of application here...
Debugging
You can setup your SDK to emit debug logs for SDK requests and responses.
You can pass your own logger class directly into your SDK.
from nws_api_client import NwsAPIClient
import logging
logging.basicConfig(level=logging.DEBUG)
s = NwsAPIClient(debug_logger=logging.getLogger("nws_api_client"))
You can also enable a default debug logger by setting an environment variable NWS_API_CLIENT_DEBUG to true.
Development
Maturity
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, I recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
Contributions
While I value open-source contributions to this SDK, this library is currently generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
Initial SDK Created by Speakeasy
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nws_api_client-0.13.1.tar.gz.
File metadata
- Download URL: nws_api_client-0.13.1.tar.gz
- Upload date:
- Size: 93.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.9.22 Linux/6.8.0-1027-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6cb0c2b1fc47bbbb77d6c7e3c302c7d2b68cedd191d4e491993139c66e140562
|
|
| MD5 |
8cee44cfbee7fea85eecde8337680b20
|
|
| BLAKE2b-256 |
3b27277a89be0fc92c972929ab0b764715baee5ee16b2a2bb52491140df7a816
|
File details
Details for the file nws_api_client-0.13.1-py3-none-any.whl.
File metadata
- Download URL: nws_api_client-0.13.1-py3-none-any.whl
- Upload date:
- Size: 193.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.9.22 Linux/6.8.0-1027-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c208eb8dc635fe6895f891d7d838730f310c18591c694dd485d7094ba0607c6
|
|
| MD5 |
afb43ae14af82c36461fa54ac9f4867e
|
|
| BLAKE2b-256 |
83c1e18645be973fce85f138949fb059ddaff260f90ed788d41ce7c7fefe1932
|