Python SDK for Singapore Government Data APIs
Project description
SGData SDK
A Python client for Singapore's government data APIs. Get typed, ready-to-use data for weather, air quality, and carpark availability — no dict wrangling required.
Features
- Typed responses for all 9 endpoints — full autocomplete, no raw dicts
- Auto-coercion of API quirks (string numbers, string datetimes)
- Convenience methods on responses:
available(),get(),full() - SDK exceptions instead of bare
requestserrors - Pass
datetimeobjects or strings for historical queries - Optional retry with exponential backoff
Installation
pip install sgdata-sdk
Quick Start
from sgdata import SGDataClient
client = SGDataClient()
# Typed responses — no dict drilling
psi = client.get_psi()
print(psi.readings.psi_24h) # dict[Region, int]
print(psi.timestamp) # datetime
weather = client.get_2hour_weather_forecast()
ang_mo_kio = weather.get("Ang Mo Kio")
print(ang_mo_kio.forecast) # "Partly Cloudy"
print(ang_mo_kio.latitude) # 1.375
carparks = client.get_carpark_availability()
print(carparks.timestamp) # datetime
Carpark Availability
The carpark endpoint is where the SDK saves the most work:
carparks = client.get_carpark_availability()
# All carparks with available spaces
for cp in carparks.available():
print(cp.carpark_number, cp.total_available)
# Only carparks with available car lots
from sgdata import LotType
for cp in carparks.available(lot_type=LotType.CAR):
print(cp.carpark_number, cp.car_lots.available_lots)
# Look up a specific carpark
hg1 = carparks.get("HG1")
if hg1:
print(f"{hg1.carpark_number}: {hg1.car_lots.occupancy_rate:.0%} full")
# All full carparks
full = carparks.full()
What You Can Get
Air Quality
get_psi()→PSIResponse— Pollutant Standards Indexget_pm25()→PM25Response— PM2.5 readings
Weather Forecasts
get_2hour_weather_forecast()→WeatherForecastResponseget_24hour_weather_forecast()→WeatherForecastResponseget_4day_weather_forecast()→WeatherForecastResponse
Weather Measurements
get_rainfall()→StationReadingResponseget_relative_humidity()→StationReadingResponseget_air_temperature()→StationReadingResponse
Transport
get_carpark_availability()→CarparkAvailabilityResponse
Historical Data
Pass a datetime, date, or a string:
from datetime import datetime, date
# datetime object
psi = client.get_psi(date_time=datetime(2024, 1, 15, 12))
# date object
psi = client.get_psi(date=date(2024, 1, 15))
# or a string, same as before
psi = client.get_psi(date_time="2024-01-15T12:00:00")
Error Handling
from sgdata import SGDataError, SGDataAPIError, RateLimitError, SGDataTimeoutError
try:
carparks = client.get_carpark_availability()
except RateLimitError:
print("Rate limited — back off and retry")
except SGDataTimeoutError:
print("Request timed out")
except SGDataAPIError as e:
print(f"API error {e.status_code}: {e}")
except SGDataError as e:
print(f"SDK error: {e}")
Context Manager
with SGDataClient() as client:
weather = client.get_2hour_weather_forecast()
temp = client.get_air_temperature()
Retry Support
pip install sgdata-sdk[retry]
# Automatically retries on 429, 5xx, and timeouts (3 attempts, exponential backoff)
client = SGDataClient(retry=True)
Development
git clone https://github.com/KT-afk/sgdata-sdk-python.git
cd sgdata-sdk-python
pip install -e ".[dev]"
pytest # run tests
mypy sgdata # type check
black sgdata tests
License
MIT
Links
- Data source: data.gov.sg
- Issues and PRs welcome!
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 sgdata_sdk-0.2.0.tar.gz.
File metadata
- Download URL: sgdata_sdk-0.2.0.tar.gz
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e727ed187fc8c6097f0c702ab7c2023259b7b3aa6de9a888cf92513b34a000d3
|
|
| MD5 |
1fbd808b7cf5e609db39196d46c5f677
|
|
| BLAKE2b-256 |
f8c4d84ddfe2bbeaea7458f211c799f16cab5e4d21e45fc1f979af8fdf110fee
|
File details
Details for the file sgdata_sdk-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sgdata_sdk-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a876599ce431b9e21bd18ed47628564419f3620447fe0cbe144fdb596ab19a3
|
|
| MD5 |
9792afdd2499381069c78c82cc615ab8
|
|
| BLAKE2b-256 |
30afdd98e020ae680773b41ac3d02aa04c38050301d71e08487bc06dd2597379
|