Load Open Government, Singapore data to DuckDB
Build a Open Government, Singapore to DuckDB pipeline with your coding agent. One prompt scaffolds it with the dltHub AI harness, plus the Open Government, Singapore API base URL, auth, endpoints, and incremental loading.
Data.gov.sg provides a portal to access official Singapore government datasets and real-time APIs for development. Everything needed to build a working Open Government, Singapore → DuckDB pipeline is on this page: the API's base URL, authentication, endpoints, pagination and incremental field — plus a prompt that hands the whole job to your coding agent.
Build your Open Government, Singapore to DuckDB pipeline
Paste this prompt into Claude, Codex, or Cursor. The agent does the rest.
PromptRunuvx dlthub-init@latestto build a pipeline from Open Government, Singapore to DuckDB and run it on dltHub
That scaffolds a dltHub workspace and installs the dltHub AI harness — the project rules, the secrets-management skill, and the dlt MCP server your agent needs to work safely. From there it reads the Open Government, Singapore API, proposes the endpoints to load, then writes, runs and validates the pipeline while you review rather than type. Credentials are inspected through MCP tools, so your agent never reads secrets.toml itself. How the LLM-native workflow works →
Prefer to write it yourself? Every fact the agent uses is below.
Open Government, Singapore API at a glance
| Base URL | https://api-production.data.gov.sg/v2/public/api |
| Example endpoint | GET v2/public/api/datasets |
| Records found at | data.datasets |
| Authentication | API keys are recommended for production workflows and are passed as a header — sent in the x-api-key header |
| Pagination | Page-number |
| Incremental field | page |
| Record id | _id |
| API reference | https://guide.data.gov.sg/developer-guide/api-overview/how-to-use-your-api-key |
These values come from the Open Government, Singapore API reference — the authoritative source if anything here looks out of date.
How do I authenticate with the Open Government, Singapore API?
Requests must include an 'x-api-key' header containing your API key.
1. Get your credentials
- Navigate to https://data.gov.sg/ and click on the login icon in the top right corner. 2. If you do not have an account, click 'Sign Up' and register using your email address. 3. Verify your identity via the OTP sent to your email. 4. Once logged in, navigate to your user profile or dashboard. 5. Locate and click on the 'API Keys' or 'Create API Key' section. 6. Follow the prompts to request either a developer or production API key, providing details on your use case, and click 'Create' to generate your key. Store this key immediately as it cannot be viewed again.
2. Add them to .dlt/secrets.toml
[sources.open_government_singapore_source] data_gov_sg_api_key = "your_api_key_here"
dlt reads this file automatically at runtime. With the harness, the setup-secrets skill prompts you for the values and never handles the raw credential in chat. For production, see setting up credentials with dlt.
What Open Government, Singapore data can I load into DuckDB?
These are the Open Government, Singapore endpoints dlt can load into DuckDB:
| Resource | Endpoint | Method | Data selector | Description |
|---|---|---|---|---|
| datasets | /v2/public/api/datasets | GET | data.datasets | List all available datasets (page-based) |
| dataset_search | /api/action/datastore_search | GET | result.records | Search rows in a dataset (offset-based) |
| pm25_realtime | /v2/real-time/api/pm25 | GET | Get PM 2.5 real-time data | |
| initiate_download | /v1/public/api/datasets/{datasetId}/initiate-download | GET | Initiate download for a full dataset | |
| poll_download | /v1/public/api/datasets/{datasetId}/poll-download | GET | Poll status of a dataset download |
How do I load only new Open Government, Singapore records?
Open Government, Singapore exposes page on v2/public/api/datasets, so dlt can request only the records that changed since the last run. Set it as the cursor_path and dlt tracks the high-water mark for you between runs.
{"name": "datasets", "endpoint": { "path": "v2/public/api/datasets", "data_selector": "data.datasets", "incremental": {"cursor_path": "page", "initial_value": "2024-01-01T00:00:00Z"}, }}
On the first run dlt loads everything from initial_value; on every run after that it requests only what changed and appends with write_disposition="merge" if you set a primary key. See incremental loading.
What does the generated Open Government, Singapore pipeline look like?
A standard dlt REST API pipeline — the same code you would write by hand, loading datasets and collections from the Open Government, Singapore API into DuckDB:
import dlt from dlt.sources.rest_api import RESTAPIConfig, rest_api_resources @dlt.source def open_government_singapore_source(api_key=dlt.secrets.value): config: RESTAPIConfig = { "client": { "base_url": "https://api-production.data.gov.sg/v2/public/api", "auth": {"type": "api_key", "api_key": api_key, "name": "x-api-key", "location": "header"}, }, "resources": [ {"name": "datasets", "endpoint": {"path": "v2/public/api/datasets", "data_selector": "data.datasets"}}, {"name": "dataset_search", "endpoint": {"path": "api/action/datastore_search", "data_selector": "result.records"}} ], } yield from rest_api_resources(config) def load_open_government_singapore_to_duckdb() -> None: pipeline = dlt.pipeline( pipeline_name="open_government_singapore_pipeline", destination="duckdb", dataset_name="open_government_singapore_data", ) load_info = pipeline.run(open_government_singapore_source()) print(load_info) if __name__ == "__main__": load_open_government_singapore_to_duckdb()
Run it with python open_government_singapore_pipeline.py. The agent iterates on this until it loads cleanly — you review and approve, rather than write it from scratch.
How do I query Open Government, Singapore data in DuckDB?
dlt creates one table per resource. Query the loaded data with Python or SQL — or ask your agent to, through the MCP server's execute_sql_query tool.
Python (pandas DataFrame):
import dlt data = dlt.pipeline("open_government_singapore_pipeline").dataset() df = data.dataset_search.df() print(df.head())
SQL:
SELECT * FROM open_government_singapore_data.dataset_search LIMIT 10;
See querying your data with dataset and exploring it in marimo notebooks.
How do I deploy the Open Government, Singapore to DuckDB pipeline in production?
The pipeline runs locally, which is ideal for prototyping and one-off analysis. When you need it on a schedule, monitored on every load, and shared with your team, deploy the same dlt code on the dltHub platform — no infrastructure to maintain. The prompt above already ends with "run it on dltHub", so your agent can take it there directly.
- Deploy & schedule — run the pipeline as a managed job with automatic retries.
- Monitor — observable job queues, alerting, and load metrics for every run.
- Transform — promote raw Open Government, Singapore loads into governed, documented models.
- Visualize & share — explore data in notebooks and publish live dashboards instead of static screenshots.
What other destinations can I load Open Government, Singapore data to?
dlt loads into any of these — only the destination argument changes:
| Destination | Example value |
|---|---|
| PostgreSQL | "postgres" |
| BigQuery | "bigquery" |
| Snowflake | "snowflake" |
| Redshift | "redshift" |
| Databricks | "databricks" |
| Filesystem (S3, GCS, Azure) | "filesystem" |
Set dlt.pipeline(destination="snowflake") and add credentials in .dlt/secrets.toml. On the dltHub platform the same pipeline runs against a managed Iceberg lakehouse. See the full destinations list.
Next steps
Was this page helpful?
Community Hub
Need more dlt context for Open Government, Singapore to DuckDB?
Request dlt skills, commands, AGENT.md files, and AI-native context.