Skip to content

OpenAlex

The OpenAlex client (bibliofabric.openalex) is a Python client for the OpenAlex API. It types all nine native entity endpoints (works, authors, sources, institutions, topics, keywords, publishers, funders, awards) plus the /locations per-copy index and /links citation-graph endpoint with Pydantic models and serializes OpenAlex filter syntax for you. It ships an async client and a generated sync twin with the same API.

Install

uv add bibliofabric

The OpenAlex client ships in bibliofabric 0.9.0 and newer. Requires Python 3.12 or newer.

Authentication

OpenAlex works without a key, but unkeyed traffic shares a $0.10/day per-IP budget and cannot use cursor paging past the first page. A free key raises the budget to $1/day. Request one at openalex.org/settings/api, then export it:

export OPENALEX_API_KEY="your-key"

By default the key travels as the api_key query parameter. Set OPENALEX_AUTH_SCHEME=bearer to send it as an Authorization: Bearer header instead. See errors, retries, and rate limits for budgets and pacing.

Quick start

import asyncio
from bibliofabric.openalex import OpenAlexClient


async def main():
    async with OpenAlexClient() as client:
        work = await client.works.get("W2741809807")
        print(work.title, work.publication_year)

        page = await client.works.search(
            {"is_oa": True, "publication_year": 2024},
            query="photosynthesis",
            select=["id", "title"],
        )
        print(page.total, len(page.results))

        async for author in client.authors.iterate(query="chemistry", limit=10):
            print(author.display_name)


asyncio.run(main())

Every resource client exposes get(), search(), iterate(), collect(), first(), count(), sample(), group_by(), and iterate_group_by(). The works client adds OpenAlex-specific helpers: batch_get_by_doi(), batch_get_by_openalex_id(), citing(), referenced(), oa_pdf_urls(), search_scoped(), and corpus=/include_parts=/include_pulled=/rerank= controls. The client itself offers free autocomplete() and rate_limit() endpoints, plus last_rate_limit_headers after responses.

Sync usage

OpenAlexSyncClient is generated from the async source and behaves the same: same parameters, errors, retries, paging, and rate limits. The resource twins are WorksSyncClient, AuthorsSyncClient, and so on, one *SyncClient class per endpoint. The sync client uses plain httpx.Client and never starts or touches an event loop, so it works in scripts, in Jupyter, in threads, and when called from inside a running event loop.

from bibliofabric.openalex import OpenAlexSyncClient

with OpenAlexSyncClient() as client:
    work = client.works.get("W2741809807")
    print(work.title, work.publication_year)

    page = client.works.search(
        {"is_oa": True}, query="photosynthesis", select=["id", "title"]
    )
    print(page.total, len(page.results))

    for author in client.authors.iterate(query="chemistry", limit=5):
        print(author.display_name)

Use the async client in asyncio applications and for many concurrent requests. Use the sync client everywhere else. Both read OPENALEX_API_KEY and the other OPENALEX_* settings the same way.

Settings

OpenAlexSettings loads from environment variables prefixed with OPENALEX_: OPENALEX_API_KEY, OPENALEX_AUTH_SCHEME, OPENALEX_MAX_REQUESTS_PER_SECOND, OPENALEX_MAX_CONCURRENCY, OPENALEX_RATE_LIMITS (JSON, default {"semantic": 1.0}), OPENALEX_TIMEOUT, OPENALEX_MAX_RETRIES, OPENALEX_MAX_RETRY_WAIT, OPENALEX_USER_AGENT.

Examples

Runnable scripts live in packages/bibliofabric/examples/openalex: a quickstart (get, count, search, bounded iterate), the same calls with the sync client, typed filters with cursor iteration, and batch DOI lookups with the citation-graph helpers. Each script states its run command in its docstring; details are in examples/openalex/README.md.

Where to go next