Skip to main content
v2026.11,986 entries · CC-BY 4.0

rentrez: Guide to NCBI E-utilities in R

Learn how to use rentrez to query NCBI databases, download nucleotide sequences, fetch PubMed records, and manage API rate limits directly in an R session.

Written and maintained by CASRAI Editorial Board

Published

On this page

The rentrez package provides a native R programming interface to the National Center for Biotechnology Information (NCBI) Entrez Programming Utilities (E-utilities) API. Developed under the rOpenSci initiative by David J. Winter, rentrez enables researchers, biostatisticians, and bioinformaticians to query NCBI databases, retrieve structured metadata, download nucleotide and protein sequences, and automate large-scale literature and genomic workflows directly within R.

What it is used for in research

Biomedical and life science investigations frequently require automated, programmatic access to reference databases rather than manual web-based searching. The rentrez package abstracts the nine core NCBI E-utilities web service endpoints into idiomatic R functions, allowing researchers to build reproducible pipelines across 38+ NCBI databases, including PubMed, Nuccore (GenBank), Protein, NCBI Gene, NCBI Taxonomy, ClinVar, PMC, and the Sequence Read Archive (SRA).

Key research applications include:

  • Systematic literature searches and scientometrics: Programmatically querying PubMed with Boolean logic, date ranges, and Medical Subject Headings (MeSH), followed by downloading PubMed identifiers (PMIDs), author metadata, abstracts, and publication dates for evidence synthesis. This complements broader academic citation workflows such as the Semantic Scholar API.
  • Batch sequence acquisition: Downloading nucleotide or amino acid sequences in FASTA, GenBank flatfile, or XML format using accession numbers or database identifiers directly into R analysis environments.
  • Cross-database entity mapping: Navigating relational connections across distinct NCBI collections, such as resolving all research articles linked to a specific genetic locus, or locating protein structures associated with a mapped genomic feature.
  • Server-side workflow caching: Utilizing the NCBI Entrez History Server to store massive search results on NCBI remote servers, enabling iterative filtering and chunked downloading without transferring millions of IDs over the network or exhausting local memory.

Install or access (official source only)

The rentrez package is distributed through the Comprehensive R Archive Network (CRAN) as a stable binary package. Installing from CRAN ensures binary compatibility and installs required dependencies, including httr for HTTP requests, jsonlite for parsing JSON, and XML for parsing structured XML documents. For background on configuring package repositories and build dependencies, consult the CASRAI guide to installing R packages.

To install the stable release from CRAN, execute the following command in your R console:

install.packages("rentrez")
library(rentrez)

For development versions containing upstream bug fixes, the package can be installed from the official rOpenSci GitHub repository using the remotes package:

install.packages("remotes")
remotes::install_github("ropensci/rentrez")

Configuring NCBI API Key Authentication

NCBI enforces strict rate-limiting policies on all E-utilities requests:

  • Unregistered requests (no API key): Maximum of 3 requests per second. Requests exceeding this threshold receive HTTP 429 errors.
  • Registered requests (with API key): Maximum of 10 requests per second. Registered accounts also enjoy higher concurrency thresholds.

Obtain a personal API key from your free NCBI account settings page. Register the key in your R session using the dedicated rentrez helper:

# Set API key for the active R session
set_entrez_key("YOUR_NCBI_API_KEY")

# Alternatively, set permanently in ~/.Renviron:
# ENTREZ_KEY="YOUR_NCBI_API_KEY"

Step-by-step task (numbered, with menu names or commands exactly as in the current docs)

This reproducible workflow demonstrates how to search PubMed for recent research papers, store the results on the NCBI History Server, paginate through document summaries, and download linked sequence data from GenBank.

  1. Inspect available databases and search fields:
    Verify connection to the NCBI servers and list supported database keys.

    library(rentrez)
    
    # List all available Entrez databases
    dbs <- entrez_dbs()
    print(head(dbs, 10))
    
    # Inspect indexed search fields for PubMed
    pubmed_fields <- entrez_db_searchable("pubmed")
    head(pubmed_fields)
  2. Execute a search with History Server caching:
    Query PubMed using standard Entrez Boolean syntax. Setting use_history = TRUE instructs NCBI to hold the full set of matching UIDs on its server.

    # Search PubMed for CRISPR clinical trials published in 2024
    search_query <- "CRISPR[Title/Abstract] AND clinical trial[Publication Type] AND 2024[Date - Publication]"
    search_res <- entrez_search(db = "pubmed", term = search_query, use_history = TRUE)
    
    # View total matching records and returned IDs
    cat("Total matching records:", search_res$count, "\n")
    print(search_res$ids)
  3. Extract server-side session credentials:
    Retrieve the WebEnv session string and QueryKey generated by NCBI to reference the stored query in subsequent calls.

    webenv_cookie <- search_res$WebEnv
    query_key <- search_res$QueryKey
  4. Retrieve and parse document summaries in batches:
    Fetch structured metadata summaries using entrez_summary() with pagination parameters retstart and retmax, then parse fields using extract_from_esummary().

    # Fetch summaries for the first 5 records via the History Server
    summaries <- entrez_summary(db = "pubmed", 
                                WebEnv = webenv_cookie, 
                                query_key = query_key, 
                                retstart = 0, 
                                retmax = 5)
    
    # Extract titles and publication dates
    paper_titles <- extract_from_esummary(summaries, "title")
    pub_dates <- extract_from_esummary(summaries, "pubdate")
    
    # Format into a clean summary data frame
    results_df <- data.frame(
      PMID = names(paper_titles),
      Title = unname(paper_titles),
      PubDate = unname(pub_dates),
      stringsAsFactors = FALSE
    )
    print(results_df)
  5. Find cross-referenced biological entities:
    Use entrez_link() to locate nucleotide or protein records connected to specific PubMed articles or taxonomic IDs.

    # Find nucleotide records linked to a specific PubMed ID
    links <- entrez_link(dbfrom = "pubmed", id = search_res$ids[1], db = "nuccore")
    linked_nuccore_ids <- links$links$pubmed_nuccore
    print(linked_nuccore_ids)
  6. Download complete biological sequence records:
    Fetch full FASTA sequences from the nuccore database using entrez_fetch().

    # Download FASTA record for a target accession or UID
    target_id <- "NC_001422.1"
    fasta_record <- entrez_fetch(db = "nuccore", 
                                 id = target_id, 
                                 rettype = "fasta", 
                                 retmode = "text")
    
    # Print the header line and beginning of the sequence
    cat(substr(fasta_record, 1, 250), "\n")

Output and how to read it

Different rentrez functions return distinct R data structures tailored to the underlying E-utilities endpoint:

  • Search Objects (esearch class):
    Returned by entrez_search(), this S3 list contains:

    • $count: An integer representing the total count of records in the database matching your query, regardless of how many IDs were downloaded.
    • $retmax: The number of IDs returned in this specific response (default 20, capped at 10,000 per request).
    • $retstart: The starting offset index for the current batch.
    • $ids: A character vector containing the retrieved unique identifiers (e.g., PMIDs or NCBI accession numbers).
    • $WebEnv and $QueryKey: Alphanumeric server tokens populated when use_history = TRUE, identifying the remote search session.
  • Summary Lists (esummary_list class):
    Returned by entrez_summary(), this contains individual record summaries. Each list element corresponds to one UID and contains parsed fields such as $title, $authors, $source, $pubdate, and $articleids. The helper function extract_from_esummary() flattens selected attributes across all elements into a character vector or named matrix.
  • Raw Fetch Data (character strings):
    Returned by entrez_fetch(), this output contains raw text formatted according to the requested rettype (e.g., "fasta", "gb", "medline") and retmode ("text" or "xml"). For XML responses, the string can be parsed into an XML document tree using XML::xmlParse() or xml2::read_xml() for downstream XPath extraction.

Troubleshooting

When automating batch queries against NCBI web services, network latency, rate restrictions, and malformed query strings can cause errors. The table below outlines common failure modes and verified solutions.

Error Condition Probable Cause Verified Solution
HTTP 429 Too Many Requests Exceeded NCBI rate limit (3 req/sec unauthenticated, 10 req/sec with key). Register an NCBI API key using set_entrez_key("YOUR_KEY"). In iterative batch loops, add explicit delays with Sys.sleep(0.15) and wrap requests in tryCatch() retry blocks.
HTTP 400 Bad Request Malformed search syntax, unclosed brackets, or invalid database parameter. Verify indexed field qualifiers using entrez_db_searchable(db). Check database names against entrez_dbs(). Escape boolean operators (AND, OR, NOT) in uppercase.
Empty ID Vector / Zero Count Overly narrow search parameters or misspelled search terms. Check search_obj$count > 0 before passing search_obj$ids to entrez_summary() or entrez_fetch(). Passing an empty vector causes runtime execution errors.
HTTP 500 / 502 / 504 Timeouts NCBI server overload or requesting excessively large batches in a single HTTP call. Split large downloads into smaller batches of 200 to 500 records using retstart and retmax with the NCBI History Server (WebEnv and query_key).
Truncated or Malformed XML Network interruption before the full payload is transferred. Increase HTTP timeout with httr::config(timeout = 60) inside the config argument of rentrez functions, or write raw content to disk before parsing.

Free alternatives (table)

Several open-source packages and command-line utilities offer complementary approaches to retrieving biological and bibliographic data from NCBI and affiliated public repositories.

Tool Environment Scope & Primary Focus Primary Advantage Trade-offs
rentrez R (CRAN) Broad NCBI ecosystem (38+ databases including PubMed, GenBank, Gene) Native R interface for all 9 E-utilities; seamless NCBI History Server support for large queries. Requires manual string or XML parsing for deeply nested record types.
easyPubMed R (CRAN) PubMed literature and bibliographic records exclusively Automatic extraction of author affiliations, abstracts, and MeSH terms into tidy data frames. Limited to PubMed; cannot query nucleotide, protein, or taxonomic databases.
repmeta R (CRAN) Bibliographic search and metadata curation Tailored for systematic reviews, deduplication, and journal metric extraction. Specialized for literature curation; lacks biological sequence access.
Bioconductor annotate / Biostrings R (Bioconductor) Genomic annotation maps and high-throughput biological sequences Direct integration with Bioconductor S4 classes (GRanges, DNAStringSet). Steeper learning curve; heavier installation dependencies; slower for ad-hoc PubMed queries.
NCBI Entrez Direct (EDirect) Command Line (Unix/Bash) Entire NCBI Entrez suite via shell pipelines Unmatched speed in shell environments using pipeable commands (esearch | efetch | xtract). Not an R package; requires command-line scripting and shell execution to ingest into R.

FAQ

What is the difference between entrez_fetch() and entrez_summary()?

entrez_summary() retrieves pre-formatted document summaries (ESummary records) containing primary metadata such as titles, publication dates, and author lists as structured R objects. entrez_fetch() downloads complete, raw database records in requested formats, such as full GenBank flatfiles, FASTA nucleotide sequences, or raw XML documents.

How do I set an NCBI API key in rentrez?

You can set your NCBI API key for the current R session by running set_entrez_key("YOUR_API_KEY"), which stores the key in the ENTREZ_KEY environment variable. To make this setting permanent across all R sessions, add ENTREZ_KEY="YOUR_API_KEY" to your user-level ~/.Renviron configuration file.

How can I download thousands of records without running out of memory in R?

Set use_history = TRUE inside entrez_search() to instruct NCBI to cache search results on their remote servers. Then, iterate through the stored set using a loop that passes search_obj$WebEnv and search_obj$QueryKey to entrez_fetch() or entrez_summary() with retstart incremented in batches of 200 to 500 records.

Can rentrez download full-text open access articles from PubMed Central?

Yes. You can retrieve full-text XML for open-access articles available in PubMed Central by targeting db = "pmc" with entrez_fetch(), specifying rettype = "xml". Note that articles restricted by publisher copyright barriers cannot be fetched in full text via the open E-utilities API.

Does rentrez support complex Boolean search queries?

Yes. Any search syntax supported by the NCBI Entrez web interface works in entrez_search(). You can combine terms with uppercase Boolean operators (AND, OR, NOT), search specific indexed fields using square brackets (such as [Title], [Author], or [Organism]), and filter by publication date ranges.

Sources (official docs)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about rentrez: Guide to NCBI E-utilities in R

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →