No logo available for Department of agriculture to DuckDB connector icon

Load Department of agriculture data to DuckDB

Build a Department of agriculture to DuckDB pipeline with your coding agent. One prompt scaffolds it with the dltHub AI harness, plus the Department of agriculture API base URL, auth, endpoints, and incremental loading.

SourceDepartment of agricultureAPI Guide | USDA FoodData CentralDestinationDuckDBIn-process analytical database. The default local destination for dlt pipelines.

The USDA FoodData Central API provides programmatic access to nutrient and food-related data from the Department of Agriculture. Everything needed to build a working Department of agriculture → 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 Department of agriculture to DuckDB pipeline

Paste this prompt into Claude, Codex, or Cursor. The agent does the rest.

Prompt
Run uvx dlthub-init@latest to build a pipeline from Department of agriculture 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 Department of agriculture 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.


Department of agriculture API at a glance

Base URLhttps://api.nal.usda.gov/fdc/v1
Example endpointGET v1/foods/search
Records found atfoods
Authenticationall requests require an API key for authentication — sent in the Authorization header, prefixed Basic
PaginationPage-number page size via pageSize
Incremental fieldpageNumber
API referencehttps://mymarketnews.ams.usda.gov/mymarketnews-api/authentication

These values come from the Department of agriculture API reference — the authoritative source if anything here looks out of date.


How do I authenticate with the Department of agriculture API?

Authentication is typically performed via an API key. For many USDA services like FoodData Central and ERS APIs, the key is passed as a query parameter named 'api_key', though some services like My Market News use basic authentication with the API key as the username.

1. Get your credentials

To obtain an API key for USDA services, visit the official api.data.gov signup page at https://api.data.gov/signup/. You will need to provide your first name, last name, and a valid email address. Once submitted, you will receive an API key that provides universal access to U.S. government open data APIs, including those managed by USDA agencies like the Economic Research Service (ERS) and FoodData Central (FDC).

2. Add them to .dlt/secrets.toml

[sources.department_of_agriculture_source] 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 Department of agriculture data can I load into DuckDB?

These are the Department of agriculture endpoints dlt can load into DuckDB:

ResourceEndpointMethodData selectorDescription
food_search/v1/foods/searchGETfoodsSearch for food items with pagination
food_details/v1/foodsGETFetch multiple food items by FDC ID
food_list/v1/foods/listGETGet a paged list of foods
publication_list/api/v1/publication/findAllGETReturns a paginated list of all publications
publication_by_agency/api/v1/publication/findByAgency/{agencyAcronym}GETSearch a paginated list of publications by agency
mpr_reports/services/v1.1/reportsGETresultsReturns list of market news reports

How do I load only new Department of agriculture records?

Department of agriculture exposes pageNumber on v1/foods/search, 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": "food_search", "endpoint": { "path": "v1/foods/search", "data_selector": "foods", "incremental": {"cursor_path": "pageNumber", "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 Department of agriculture pipeline look like?

A standard dlt REST API pipeline — the same code you would write by hand, loading /foods/search and /food/{fdcId} from the Department of agriculture API into DuckDB:

import dlt from dlt.sources.rest_api import RESTAPIConfig, rest_api_resources @dlt.source def department_of_agriculture_source(api_key=dlt.secrets.value): config: RESTAPIConfig = { "client": { "base_url": "https://api.nal.usda.gov/fdc/v1", "auth": {"type": "http_basic", "username": "REPLACE_ME", "password": api_key}, }, "resources": [ {"name": "food_search", "endpoint": {"path": "v1/foods/search", "data_selector": "foods"}}, {"name": "mpr_reports", "endpoint": {"path": "services/v1.1/reports", "data_selector": "results"}} ], } yield from rest_api_resources(config) def load_department_of_agriculture_to_duckdb() -> None: pipeline = dlt.pipeline( pipeline_name="department_of_agriculture_pipeline", destination="duckdb", dataset_name="department_of_agriculture_data", ) load_info = pipeline.run(department_of_agriculture_source()) print(load_info) if __name__ == "__main__": load_department_of_agriculture_to_duckdb()

Run it with python department_of_agriculture_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 Department of agriculture 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("department_of_agriculture_pipeline").dataset() df = data.foods.df() print(df.head())

SQL:

SELECT * FROM department_of_agriculture_data.foods LIMIT 10;

See querying your data with dataset and exploring it in marimo notebooks.


How do I deploy the Department of agriculture 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 Department of agriculture loads into governed, documented models.
  • Visualize & share — explore data in notebooks and publish live dashboards instead of static screenshots.

Book a demo →


What other destinations can I load Department of agriculture data to?

dlt loads into any of these — only the destination argument changes:

DestinationExample 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 Department of agriculture to DuckDB?

Request dlt skills, commands, AGENT.md files, and AI-native context.