Load Open Policy Agent data to DuckDB
Build a Open Policy Agent to DuckDB pipeline with your coding agent. One prompt scaffolds it with the dltHub AI harness, plus the Open Policy Agent API base URL, auth, endpoints, and incremental loading.
Open Policy Agent (OPA) is a general-purpose policy engine that exposes a REST API to manage policies, query decisions, and read/write data. Everything needed to build a working Open Policy Agent → 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 Policy Agent 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 Policy Agent 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 Policy Agent 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 Policy Agent API at a glance
| Base URL | http://<OPA_HOST>:8181 |
| Example endpoint | GET v1/bundles |
| Records found at | result |
| Authentication | OPA supports token-based authentication via the Authorization header using Bearer tokens when enabled — sent in the Authorization header, prefixed Bearer |
| Pagination | Not paginated |
| Incremental field | cursor |
| API reference | https://openpolicyagent.org/docs/rest-api |
These values come from the Open Policy Agent API reference — the authoritative source if anything here looks out of date.
How do I authenticate with the Open Policy Agent API?
When OPA is configured with the --authentication=token flag, clients must include the 'Authorization' header with a value of 'Bearer '.
1. Get your credentials
Open Policy Agent (OPA) does not provide a graphical dashboard for managing API credentials. Authentication is handled by starting the OPA daemon with specific flags. To use token-based authentication: 1. Start OPA with the --authentication=token flag. 2. Define an authorization policy (in Rego) that validates the bearer token provided in requests. 3. When communicating with the OPA REST API, include the token in the HTTP Authorization header as 'Authorization: Bearer '. For TLS-based authentication, start OPA with --authentication=tls and provide the necessary certificate files (--tls-cert-file, --tls-private-key-file, and --tls-ca-cert-file).
2. Add them to .dlt/secrets.toml
[sources.open_policy_agent_source] opa_auth_token = "your_secret_bearer_token_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 Policy Agent data can I load into DuckDB?
These are the Open Policy Agent endpoints dlt can load into DuckDB:
| Resource | Endpoint | Method | Data selector | Description |
|---|---|---|---|---|
| policies | /v1/policies | GET | result | List all policies |
| bundles | /v1/bundles | GET | result | List all bundles with pagination support |
| document | /v1/data/{path:.+} | GET | result | Get a document |
| query | /v1/query | GET | result | Execute a simple query |
| config | /config | GET | Get active configuration |
How do I load only new Open Policy Agent records?
Open Policy Agent exposes cursor on v1/bundles, 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": "bundles", "endpoint": { "path": "v1/bundles", "data_selector": "result", "incremental": {"cursor_path": "cursor", "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 Policy Agent pipeline look like?
A standard dlt REST API pipeline — the same code you would write by hand, loading /v1/data and /v1/policies from the Open Policy Agent API into DuckDB:
import dlt from dlt.sources.rest_api import RESTAPIConfig, rest_api_resources @dlt.source def open_policy_agent_source(auth_token=dlt.secrets.value): config: RESTAPIConfig = { "client": { "base_url": "http://<OPA_HOST>:8181", "auth": {"type": "bearer", "token": auth_token}, }, "resources": [ {"name": "bundles", "endpoint": {"path": "v1/bundles", "data_selector": "result"}}, {"name": "policies", "endpoint": {"path": "v1/policies", "data_selector": "result"}} ], } yield from rest_api_resources(config) def load_open_policy_agent_to_duckdb() -> None: pipeline = dlt.pipeline( pipeline_name="open_policy_agent_pipeline", destination="duckdb", dataset_name="open_policy_agent_data", ) load_info = pipeline.run(open_policy_agent_source()) print(load_info) if __name__ == "__main__": load_open_policy_agent_to_duckdb()
Run it with python open_policy_agent_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 Policy Agent 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_policy_agent_pipeline").dataset() df = data.bundles.df() print(df.head())
SQL:
SELECT * FROM open_policy_agent_data.bundles LIMIT 10;
See querying your data with dataset and exploring it in marimo notebooks.
How do I deploy the Open Policy Agent 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 Policy Agent 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 Policy Agent 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 Policy Agent to DuckDB?
Request dlt skills, commands, AGENT.md files, and AI-native context.