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).

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.

Details

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.

See also

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

Examples

if (FALSE) { # \dontrun{
# Search by name
pubchem_search("aspirin")

# Search by SMILES (uses POST automatically)
pubchem_search("CC(=O)OC1=CC=CC=C1C(=O)O", type = "smiles")

# Search by InChIKey
pubchem_search("BSYNRYMUTXBXSQ-UHFFFAOYSA-N", type = "inchikey")

# Search by molecular formula
pubchem_search("C9H8O4", type = "formula")
} # }