openFDA Search and Query
Prerequisites
uv: Read the uv skill and follow its Setup instructions to ensure
uv is installed and on PATH.
User Notification: If LICENSE_NOTIFICATION.txt does not already exist in
this skill directory then (1) prominently notify the user to check the terms
at https://open.fda.gov/apis/ and https://open.fda.gov/license, then (2)
create the file recording the notification text and timestamp.
.env file: Make sure the .env file exists in your home directory.
Create one if it does not exist.
FDA_API_KEY (optional but recommended): Raises the daily request limit
from 1,000 to 120,000. The skill works without it, but an agent can easily
exhaust the keyless limit in a single session. The user can register for a
free key at https://open.fda.gov/apis/authentication/. If the variable is
missing from .env, do NOT ask the user to paste it into the chat (this
would leak the key into the agent's context). Instead, give the user this
command — substituting ENV_FILE with the resolved literal path to the
.env file:
printf "Enter openFDA API key (typing hidden): " && read -s key && echo && echo "FDA_API_KEY=$key" >> "ENV_FILE" && echo "Saved."
The scripts load credentials automatically via dotenv. NEVER read,
print, or inspect the .env file or its variables (e.g. no cat, grep,
echo, printenv, or os.environ.get on keys). Credentials must stay
out of the agent's context.
Core Rules
Use the Wrapper: ALWAYS execute the provided helper scripts to query the
database rather than accessing the database directly. The scripts
automatically enforce the required rate limit gracefully.
Rate Limiting: Respect openFDA rate limits. Without API key: 240
requests/min, 1,000 requests/day per IP. With API key: 240 requests/min,
120,000 requests/day per key. Always set an API key before running
multi-query workflows.
Warning: An automated agent can easily exhaust the 1,000-request daily
limit in a single research session. Always set an API key before running
multi-query workflows.
Instruct the user to register for a free key at
https://open.fda.gov/apis/authentication/ and follow the prerequisite
instructions above to add FDA_API_KEY to the .env file. The script will
emit a warning to stderr if no API key is detected.
Always Use --output: All subcommands require --output <file> to
write results to a file. This prevents large output becoming overwhelming.
Use jq or code to read the output file.
Notification: If this skill is used, ensure this is mentioned in the
output.
Utility Script
Single script for all operations:
uv run scripts/openfda_query.py {search,count,download} --output <file> [options]
1. Search
Search any of the 28 endpoints and save JSON results to a file.
uv run scripts/openfda_query.py search \
--category drug --endpoint event \
--search "patient.drug.medicinalproduct:aspirin" \
--limit 5 --output /tmp/fda_results.json
Stdout prints a compact summary:
{"status": "success", "output": "/tmp/fda_results.json", "results_in_file": 5, "total_matching": 601477}
Options:
--output: Output file for full JSON results (required).
--category: API category — drug, device, food, tobacco, other,
animalandveterinary, cosmetic, transparency.
--endpoint: Endpoint within the category (e.g., event, label, 510k).
See references/api_endpoints.md for full
list.
--search: Query string (e.g.,
patient.drug.medicinalproduct:aspirin+AND+serious:1).
--sort: Sort field and order (e.g., receivedate:desc).
--limit: Max results (default 10, max 1000).
--skip: Pagination offset (default 0).
--api_key: API key (also reads FDA_API_KEY env var).
2. Count
Count unique values of a field within matching results.
uv run scripts/openfda_query.py count \
--category drug --endpoint event \
--search "patient.drug.medicinalproduct:aspirin" \
--count_field "patient.reaction.reactionmeddrapt.exact" \
--summary 10 --output /tmp/aspirin_reactions.json
Stdout prints a summary with the top 5 terms. Full data is in the output file.
Additional options:
--count_field: Field to count (append .exact for whole-phrase counting).
--summary N: Return only the top N most frequent terms. Use this to avoid
flooding the context with hundreds of infrequent terms.
3. Download
Download multiple pages of results to a file.
uv run scripts/openfda_query.py download \
--category drug --endpoint event \
--search "patient.drug.medicinalproduct:aspirin" \
--limit 100 --max_pages 5 \
--output /tmp/aspirin_events.json
Additional options:
--max_pages: Maximum pages to fetch (default 10).
--all_results: Automatically paginate to fetch all matching results.
Safety cap of 25,000 records maximum per download to prevent runaway
downloads and prevent excessive API usage.
Tip: Common drugs can have excessive reports. Use a date range (e.g.,
receivedate:[20250101+TO+20250131]) to limit the volume of download.
Entity Resolution: Using .exact for Precision
When searching for specific product names, drug names, or categorical terms,
always use the .exact suffix on the field to get exact-match results. Without
it, the API tokenizes multi-word values and returns noisy partial matches.
# Precise: matches only "ADVIL"
uv run scripts/openfda_query.py search --category drug --endpoint label \
--search 'openfda.brand_name.exact:"ADVIL"' \
--limit 5 --output /tmp/advil_label.json
Note: Many brand names in the FDA database include variant suffixes (e.g.,
"TYLENOL Extra Strength" rather than just "TYLENOL"). If an .exact search
returns 0 results, try without .exact to see the available brand name
variants, then re-query with the full exact name.
The .exact suffix is also required when using --count_field to aggregate
whole phrases instead of individual words.
MedDRA Term Resolution
openFDA adverse event data uses MedDRA (Medical Dictionary for Regulatory
Activities) terms for reactions. The API reports Preferred Terms (PTs) but
does not provide the MedDRA hierarchy (System Organ Class, High Level Terms,
etc.).
Note: MedDRA is a proprietary ontology and is not indexed in the
EMBL-EBI OLS. To approximate MedDRA hierarchy lookups, use the Human
Phenotype Ontology (HP) or NCI Thesaurus (NCIT) as proxy ontologies —
they cross-reference MedDRA IDs and provide parent/ancestor relationships.
# Step 1: Get top reactions from openFDA
uv run scripts/openfda_query.py count \
--category drug --endpoint event \
--search "patient.drug.medicinalproduct:metformin" \
--count_field "patient.reaction.reactionmeddrapt.exact" \
--summary 5 --output /tmp/metformin_reactions.json
# Step 2: Look up the top reaction term using a biomedical ontology service
# skill (e.g. embl-ebi-ols skill).
# MedDRA is not available in OLS; use the Human Phenotype Ontology (HP) or
# NCI Thesaurus (NCIT) as a proxy to find the hierarchical classification of
# the reaction term.
Available Endpoints (28 total)
Category to endpoint mapping:
drug: event, label, ndc, enforcement, drugsfda, shortages
device: 510k, classification, enforcement, event, pma, recall,
registrationlisting, udi, covid19serology
food: enforcement, event
tobacco: problem, researchpreventionads, researchdigitalads,
researchsmokefree
other: historicaldocument, nsde, substance, unii
animalandveterinary: event
cosmetic: event
transparency: crl
Reference
- Query syntax and all endpoints: See
references/api_endpoints.md for field names,
search syntax, date ranges, and boolean operators.
Recipes
Common query patterns for drugs, devices, foods, tobacco, cosmetics, animal and
veterinary products, substances, transparency data, adverse events, recalls,
labeling, approvals, shortages, 510(k) clearances, NDC lookups, any FDA safety
or regulatory data query, and more. See
references/recipes.md for the full recipes.
Workflow
- Search for records using
search with --output. Read the output file.
- Use
count with --summary 10 --output to summarize field distributions.
- Use
download (with --all_results for exhaustive pulls) to fetch larger
datasets.
- Read and analyze the output file using standard tools.
- For MedDRA term hierarchy questions, use a biomedical ontology service skill
(e.g. EMBL-EBI OLS skill with the HP or NCIT ontology) to look up the term.
1---2name: openfda-database3description: Query, search, and download data from the openFDA API for drugs, devices, foods, tobacco, cosmetics, animal and veterinary products, substances, and transparency data. Use for FDA adverse events, recalls, labeling, approvals, shortages, 510(k) clearances, NDC lookups, and any FDA safety or regulatory data query across all 28 API endpoints.4---56# openFDA Search and Query78## Prerequisites9101. **`uv`**: Read the `uv` skill and follow its Setup instructions to ensure11 `uv` is installed and on PATH.122. **User Notification**: If LICENSE_NOTIFICATION.txt does not already exist in13 this skill directory then (1) prominently notify the user to check the terms14 at https://open.fda.gov/apis/ and https://open.fda.gov/license, then (2)15 create the file recording the notification text and timestamp.163. **`.env` file**: Make sure the `.env` file exists in your home directory.17 Create one if it does not exist.184. **`FDA_API_KEY`** (optional but recommended): Raises the daily request limit19 from 1,000 to 120,000. The skill works without it, but an agent can easily20 exhaust the keyless limit in a single session. The user can register for a21 free key at https://open.fda.gov/apis/authentication/. If the variable is22 missing from `.env`, do NOT ask the user to paste it into the chat (this23 would leak the key into the agent's context). Instead, give the user this24 command — **substituting `ENV_FILE` with the resolved literal path to the25 `.env` file**:2627 ```bash28 printf "Enter openFDA API key (typing hidden): " && read -s key && echo && echo "FDA_API_KEY=$key" >> "ENV_FILE" && echo "Saved."29 ```3031 The scripts load credentials automatically via `dotenv`. **NEVER** read,32 print, or inspect the `.env` file or its variables (e.g. no `cat`, `grep`,33 `echo`, `printenv`, or `os.environ.get` on keys). Credentials must stay34 out of the agent's context.3536## Core Rules3738- **Use the Wrapper**: ALWAYS execute the provided helper scripts to query the39 database rather than accessing the database directly. The scripts40 automatically enforce the required rate limit gracefully.4142- **Rate Limiting**: Respect openFDA rate limits. Without API key: 24043 requests/min, 1,000 requests/day per IP. With API key: 240 requests/min,44 120,000 requests/day per key. Always set an API key before running45 multi-query workflows.4647> **Warning**: An automated agent can easily exhaust the 1,000-request daily48> limit in a single research session. Always set an API key before running49> multi-query workflows.5051> Instruct the user to register for a free key at52> https://open.fda.gov/apis/authentication/ and follow the prerequisite53> instructions above to add `FDA_API_KEY` to the `.env` file. The script will54> emit a warning to stderr if no API key is detected.5556- **Always Use `--output`**: All subcommands require `--output <file>` to57 write results to a file. This prevents large output becoming overwhelming.58 Use jq or code to read the output file.5960- **Notification**: If this skill is used, ensure this is mentioned in the61 output.6263## Utility Script6465**Single script for all operations:**6667```bash68uv run scripts/openfda_query.py {search,count,download} --output <file> [options]69```7071### 1. Search7273Search any of the 28 endpoints and save JSON results to a file.7475```bash76uv run scripts/openfda_query.py search \77 --category drug --endpoint event \78 --search "patient.drug.medicinalproduct:aspirin" \79 --limit 5 --output /tmp/fda_results.json80```8182Stdout prints a compact summary:8384```json85{"status": "success", "output": "/tmp/fda_results.json", "results_in_file": 5, "total_matching": 601477}86```8788*Options:*8990- `--output`: Output file for full JSON results (required).91- `--category`: API category — `drug`, `device`, `food`, `tobacco`, `other`,92 `animalandveterinary`, `cosmetic`, `transparency`.93- `--endpoint`: Endpoint within the category (e.g., `event`, `label`, `510k`).94 See [references/api_endpoints.md](references/api_endpoints.md) for full95 list.96- `--search`: Query string (e.g.,97 `patient.drug.medicinalproduct:aspirin+AND+serious:1`).98- `--sort`: Sort field and order (e.g., `receivedate:desc`).99- `--limit`: Max results (default 10, max 1000).100- `--skip`: Pagination offset (default 0).101- `--api_key`: API key (also reads `FDA_API_KEY` env var).102103### 2. Count104105Count unique values of a field within matching results.106107```bash108uv run scripts/openfda_query.py count \109 --category drug --endpoint event \110 --search "patient.drug.medicinalproduct:aspirin" \111 --count_field "patient.reaction.reactionmeddrapt.exact" \112 --summary 10 --output /tmp/aspirin_reactions.json113```114115Stdout prints a summary with the top 5 terms. Full data is in the output file.116117*Additional options:*118119- `--count_field`: Field to count (append `.exact` for whole-phrase counting).120- `--summary N`: Return only the top N most frequent terms. Use this to avoid121 flooding the context with hundreds of infrequent terms.122123### 3. Download124125Download multiple pages of results to a file.126127```bash128uv run scripts/openfda_query.py download \129 --category drug --endpoint event \130 --search "patient.drug.medicinalproduct:aspirin" \131 --limit 100 --max_pages 5 \132 --output /tmp/aspirin_events.json133```134135*Additional options:*136137- `--max_pages`: Maximum pages to fetch (default 10).138- `--all_results`: Automatically paginate to fetch all matching results.139 Safety cap of 25,000 records maximum per download to prevent runaway140 downloads and prevent excessive API usage.141142 > **Tip**: Common drugs can have excessive reports. Use a date range (e.g.,143 > `receivedate:[20250101+TO+20250131]`) to limit the volume of download.144145## Entity Resolution: Using .exact for Precision146147When searching for specific product names, drug names, or categorical terms,148always use the `.exact` suffix on the field to get exact-match results. Without149it, the API tokenizes multi-word values and returns noisy partial matches.150151```bash152# Precise: matches only "ADVIL"153uv run scripts/openfda_query.py search --category drug --endpoint label \154 --search 'openfda.brand_name.exact:"ADVIL"' \155 --limit 5 --output /tmp/advil_label.json156```157158> **Note**: Many brand names in the FDA database include variant suffixes (e.g.,159> "TYLENOL Extra Strength" rather than just "TYLENOL"). If an `.exact` search160> returns 0 results, try without `.exact` to see the available brand name161> variants, then re-query with the full exact name.162163The `.exact` suffix is also required when using `--count_field` to aggregate164whole phrases instead of individual words.165166## MedDRA Term Resolution167168openFDA adverse event data uses MedDRA (Medical Dictionary for Regulatory169Activities) terms for reactions. The API reports **Preferred Terms (PTs)** but170does not provide the MedDRA hierarchy (System Organ Class, High Level Terms,171etc.).172173> **Note**: MedDRA is a proprietary ontology and is **not indexed** in the174> EMBL-EBI OLS. To approximate MedDRA hierarchy lookups, use the **Human175> Phenotype Ontology (HP)** or **NCI Thesaurus (NCIT)** as proxy ontologies —176> they cross-reference MedDRA IDs and provide parent/ancestor relationships.177178```bash179# Step 1: Get top reactions from openFDA180uv run scripts/openfda_query.py count \181 --category drug --endpoint event \182 --search "patient.drug.medicinalproduct:metformin" \183 --count_field "patient.reaction.reactionmeddrapt.exact" \184 --summary 5 --output /tmp/metformin_reactions.json185186# Step 2: Look up the top reaction term using a biomedical ontology service187# skill (e.g. embl-ebi-ols skill).188# MedDRA is not available in OLS; use the Human Phenotype Ontology (HP) or189# NCI Thesaurus (NCIT) as a proxy to find the hierarchical classification of190# the reaction term.191```192193## Available Endpoints (28 total)194195Category to endpoint mapping:196197- `drug`: event, label, ndc, enforcement, drugsfda, shortages198- `device`: 510k, classification, enforcement, event, pma, recall,199 registrationlisting, udi, covid19serology200- `food`: enforcement, event201- `tobacco`: problem, researchpreventionads, researchdigitalads,202 researchsmokefree203- `other`: historicaldocument, nsde, substance, unii204- `animalandveterinary`: event205- `cosmetic`: event206- `transparency`: crl207208## Reference209210- **Query syntax and all endpoints**: See211 [references/api_endpoints.md](references/api_endpoints.md) for field names,212 search syntax, date ranges, and boolean operators.213214## Recipes215216Common query patterns for drugs, devices, foods, tobacco, cosmetics, animal and217veterinary products, substances, transparency data, adverse events, recalls,218labeling, approvals, shortages, 510(k) clearances, NDC lookups, any FDA safety219or regulatory data query, and more. See220[references/recipes.md](references/recipes.md) for the full recipes.221222## Workflow2232241. Search for records using `search` with `--output`. Read the output file.2252. Use `count` with `--summary 10 --output` to summarize field distributions.2263. Use `download` (with `--all_results` for exhaustive pulls) to fetch larger227 datasets.2284. Read and analyze the output file using standard tools.2295. For MedDRA term hierarchy questions, use a biomedical ontology service skill230 (e.g. EMBL-EBI OLS skill with the HP or NCIT ontology) to look up the term.