Skip to Content
cwa

Quick start

Install cwa, set your API key and run a first query.

cwa lets you query Taiwan’s Central Weather Administration (CWA) open data by place name. This page walks you through installing it, setting your API key and getting the weather for one township.

Installation

Requires Python 3.13 or later.

pip install cwa

With uv, run uv add cwa.

Optional features

The base install covers every query. The features below need extra packages, installed as pip install "cwa[polars]":

ExtraAddsWithout it
cwa[polars]to_polars() exportThe call raises MissingDependencyError
cwa[pandas]to_pandas() exportThe call raises MissingDependencyError
cwa[arrow]to_arrow() exportThe call raises MissingDependencyError
cwa[geo]Matching warning areas against coordinatesWarning queries by coordinates raise MissingDependencyError; queries by county or township still work
cwa[fast]Distances computed with PyProj, tens of times faster for many pointsGeographicLib is used instead, with the same results
cwa[all]All of the above

API key

Apply for an API key on the CWA Open Data platform. The SDK reads it from the CWA_API_KEY environment variable:

# macOS, Linux
export CWA_API_KEY="api-key"
# Windows PowerShell
$env:CWA_API_KEY = "api-key"

If the key comes from a config file or a secrets manager and an environment variable is inconvenient, pass it when you create a client. Module functions such as cwa.weather.get() read only the environment variable, so use a client in that case:

from cwa import CWA

with CWA(api_key="api-key") as client:
    weather = client.weather.get("臺北市信義區")

See Clients and caching for what else a client does.

First query

cwa.weather.get() returns a township’s forecast, current conditions and active warnings in one call:

import cwa

weather = cwa.weather.get("臺北市信義區")

if weather.current:
    print(weather.current.station.name, weather.current.temperature)
if weather.tomorrow:
    print(weather.tomorrow.temperature.max, weather.tomorrow.precipitation.max_probability)
for alert in weather.alerts:
    print(alert.title)
FieldContainsWhen it is None
currentThe latest reading from a nearby stationNo station within 10 km has a reading from the last two hours
today, tomorrowThe forecasts for today and tomorrowThe forecast does not cover that day
alertsWarnings affecting this township; loop over it with forNever; it is empty when there are no warnings

Check fields that can be None with if before using them, as above. See Weather snapshots for every field.

Next steps

ToRead
Use short names, constants or coordinatesLocations
Get a week of forecasts, or the forecast at a given timeForecasts
Read nearby station observationsObservations and stations
Filter warnings and check whether they affect a placeWarnings
Query earthquakes and typhoonsEarthquakes, Typhoons, Tsunamis
Query marine, astronomy, health and climate dataMarine, Astronomy, Health weather, Climate
Query many places at once and export tablesBatch queries and export
Learn the rules for time, units and missing valuesData notes