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]":
| Extra | Adds | Without it |
|---|---|---|
cwa[polars] | to_polars() export | The call raises MissingDependencyError |
cwa[pandas] | to_pandas() export | The call raises MissingDependencyError |
cwa[arrow] | to_arrow() export | The call raises MissingDependencyError |
cwa[geo] | Matching warning areas against coordinates | Warning queries by coordinates raise MissingDependencyError; queries by county or township still work |
cwa[fast] | Distances computed with PyProj, tens of times faster for many points | GeographicLib is used instead, with the same results |
cwa[all] | All of the above |
API key
Apply for an API key on 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
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) | Field | Contains | When it is None |
|---|---|---|
current | The latest reading from a nearby station | No station within 10 km has a reading from the last two hours |
today, tomorrow | The forecasts for today and tomorrow | The forecast does not cover that day |
alerts | Warnings affecting this township; loop over it with for | Never; it is empty when there are no warnings |
Check fields that can be None with if before using them, as above. See
Next steps
| To | Read |
|---|---|
| Use short names, constants or coordinates | |
| Get a week of forecasts, or the forecast at a given time | |
| Read nearby station observations | |
| Filter warnings and check whether they affect a place | |
| Query earthquakes and typhoons | |
| Query marine, astronomy, health and climate data | |
| Query many places at once and export tables | |
| Learn the rules for time, units and missing values |