Skip to contents

Assistive helper that builds a corpus by querying the OpenAlex works API (https://api.openalex.org/works?search=...). This is a network call to a live service whose results change over time, so it is not part of the package's deterministic core. Each work is mapped to {id, doi, title, year, keywords, references}, with the DOI as OpenAlex gives it (keywords falls back to the first five concepts when no keywords are present). A work whose title is null has no title, since the corpus schema types a title as a string. Only the fields the record uses are requested.

Usage

tf_fetch_corpus(
  query,
  per_page = 25,
  mailto = NULL,
  api_key = Sys.getenv("OPENALEX_API_KEY", ""),
  max_records = NULL
)

Arguments

query

Free-text search query, a single non-empty string.

per_page

Number of works to request in each page (default 25). It may be 1 to 200, but OpenAlex supports pages of up to 100 and has deprecated larger ones.

mailto

Optional contact email, sent when it is a non-empty string. OpenAlex now ignores it, since API keys replaced the polite pool it once selected.

api_key

An OpenAlex API key, by default the OPENALEX_API_KEY environment variable. It is sent only in an Authorization: Bearer header, never in the URL, the corpus or an error message. "" or NULL sends no key.

max_records

Number of works to collect, paging as needed. NULL (the default) means per_page, which is one request.

Value

A corpus object (named list) with schema_version, id, source and records.

Details

OpenAlex returns the works that match a search in pages of per_page, ranked by relevance, and a search usually matches far more works than one page holds. A max_records above per_page pages on until that many works are collected or the results run out. By default, max_records equals per_page, so one request is made. When the pages needed lie within the first 10,000 results, they are requested by number, which OpenAlex serves without a key. A larger max_records pages through OpenAlex's cursor instead, and OpenAlex serves a cursor past the first page only with an API key: without one, it refuses the second page with HTTP 401. Each page is one request and costs USD 0.001. OpenAlex allows USD 0.10 a day without a key, about 100 pages, and USD 1 with a free key. When OpenAlex refuses a request, as it does with HTTP 429 once the budget is spent and with HTTP 401 when a later cursor page is asked for without a key, the function stops with the status and, when the suggested curl package is installed, OpenAlex's own message. The request is not retried. A response without a results list, or with a work that is not an object, stops. Keyword and concept entries without a name given as text, and references that are not text, are left out. When a page after the first fails, the works fetched before it are not returned, and the message ends with the number of them, as in (after 200 records were fetched). Every request is abandoned after 30 seconds without progress, or, without the suggested curl package, after 30 seconds in all.

The arguments are checked before any request. query must be a single string with at least one character left after trimming, per_page a whole number from 1 to 200 and max_records a positive whole number, and api_key, unless NULL, must be a single string that holds only visible ASCII characters once the spaces, tabs and line breaks around it are trimmed.

The corpus records where, when and how it was fetched in source. It gives the service, endpoint and query and the UTC time of retrieval (retrieved), then the number of works that matched (total_count), the number kept (n_records), the page size and the order (sort). The date matters because the keywords change. Since late September 2026, OpenAlex has written each work's keywords with a language model that reads its title, abstract and venue. It merges and splits that vocabulary over time. Works without keywords fall back to their concepts, a deprecated vocabulary of its own. tf_litmap() compares keywords as exact strings, so a concept matches a keyword only where the two vocabularies write a term alike, which the concept 'Panic disorder' and the keyword 'panic disorder' do not. Save a fetched corpus and work from the saved file.

Examples

if (FALSE) { # \dontrun{
# Four pages of 100 lie within the first 10,000 results, so no key is
# needed. With OPENALEX_API_KEY set, for example in .Renviron, the key is
# sent in a request header.
corpus <- tf_fetch_corpus("panic disorder interoception",
                          per_page = 100, max_records = 400)
corpus$source
# null = "null" writes a missing DOI or count as null, where jsonlite would
# write an empty object.
path <- tempfile(fileext = ".json")
jsonlite::write_json(corpus, path, auto_unbox = TRUE, null = "null")
corpus <- tf_read_corpus(path)
} # }