Skip to contents

Search the PubChem PUG REST API by compound name, SMILES, InChI, InChIKey, or molecular formula and return matching PubChem Compound IDs (CIDs).

SMILES and InChI inputs are automatically sent via POST to avoid URL encoding issues with special characters (`/`, `#`, `+`).

PubChem may return multiple CIDs for a single name query (e.g., different stereoisomers). If more than 10 CIDs are returned, a message is displayed with the count.

For structure-based searches (SMILES, InChI), the query is sent as a POST form body rather than in the URL path. This is required because `httr2::req_url_path_append()` does not percent-encode path segments, and characters like `/` and `#` in SMILES notation would corrupt the URL.

Native credential and configuration errors stop the call; other PubChem Faults retain their warning and empty-result behavior.

Usage

pubchem_search(
  query,
  type = c("name", "smiles", "inchi", "inchikey", "formula"),
  cache = TRUE
)

Arguments

query

Character string. The search term (compound name, SMILES, InChI, InChIKey, or molecular formula).

type

Character string. The type of identifier in `query`. One of `"name"` (default), `"smiles"`, `"inchi"`, `"inchikey"`, or `"formula"`.

cache

Logical. If `TRUE` (default), results are cached in the session environment so repeated searches for the same query and type are instant.

Value

A tibble with a single column `cid` containing integer PubChem CIDs. Returns a zero-row tibble if no matches are found.

See also

[pubchem_properties()] to fetch properties for CIDs, [pubchem_synonyms()] to fetch synonym lists, [ct_chemical_detail_search_by_dtxsid()] for CompTox Dashboard chemical search.

Examples

if (FALSE) { # \dontrun{
pubchem_search(query = "aspirin")
pubchem_search(query = "CC(=O)O", type = "smiles")
} # }