Skip to content

Quick start

This tutorial takes you from an empty project to your first Crossref records. The Crossref API needs no key, and the three steps repeat for every client in the package, so what you learn here transfers to OpenAlex, ROR, Europe PMC, and the other thirteen.

Install the package

uv add bibliofabric

The package needs Python 3.12 or newer. It ships every client, so you never install a client on its own. Install and set up shows the pip command and the dependencies.

Fetch one record

Save this script as quickstart.py and run it with uv run python quickstart.py.

import asyncio

from bibliofabric.crossref import CrossrefClient


async def main() -> None:
    async with CrossrefClient() as client:
        work = await client.works.get("10.1038/nature12373")
        print(work.primary_title)
        print(work.doi, work.is_referenced_by_count)


asyncio.run(main())

The script prints:

Nanometre-scale thermometry in a living cell
10.1038/nature12373 1879

Three things happened. CrossrefClient() created the client and read its settings from the environment. client.works is the resource for Crossref works, and get() fetched one work by DOI. The call returned a Work model with typed attributes, so your editor completes primary_title and is_referenced_by_count, and a value that the API left out is None instead of a missing key.

get() also accepts the DOI as a URL. client.works.get("https://doi.org/10.1038/nature12373") returns the same work.

Search and page through the matches

A search returns one page at a time. iterate() walks through the pages for you and stops at limit. Replace the body of main() with a loop, and add WorksFilters to the import:

import asyncio

from bibliofabric.crossref import CrossrefClient, WorksFilters


async def main() -> None:
    async with CrossrefClient() as client:
        async for hit in client.works.iterate(
            WorksFilters(type="journal-article"), query="proteomics", limit=3
        ):
            print(hit.doi, hit.is_referenced_by_count, hit.primary_title)


asyncio.run(main())

The loop prints three journal articles that match "proteomics":

10.1016/s0167-7799(01)01863-7 0 Proteomics in context
10.1016/s0378-4347(00)00192-4 164 Strategy for qualitative and quantitative analysis in proteomics based on signature peptides
10.1016/s0079-6107(00)00010-9 71 Structural proteomics: prospects for high throughput sample preparation

WorksFilters(type="journal-article") is a typed filter. Each field is a Crossref filter, and the client serializes the values into the query string. Pass only query= when you want a plain search.

The same call has three shortcuts. collect(query="proteomics", limit=100) returns the matches as a list, first(query="proteomics") returns the best match or None, and count(query="proteomics") returns the number of matches as an integer. Page through results shows all four with their options.

Use the sync client when you don't use asyncio

Every client has a sync twin with the same calls and no await. The name adds Sync, so CrossrefClient becomes CrossrefSyncClient. The sync client works in scripts, notebooks, and threads.

from bibliofabric.crossref import CrossrefSyncClient, WorksFilters

with CrossrefSyncClient() as client:
    work = client.works.get("10.1038/nature12373")
    print(work.primary_title)

    for hit in client.works.iterate(
        WorksFilters(type="journal-article"), query="proteomics", limit=3
    ):
        print(hit.doi, hit.primary_title)

The script prints:

Nanometre-scale thermometry in a living cell
10.1016/s0167-7799(01)01863-7 Proteomics in context
10.1016/s0378-4347(00)00192-4 Strategy for qualitative and quantitative analysis in proteomics based on signature peptides
10.1016/s0079-6107(00)00010-9 Structural proteomics: prospects for high throughput sample preparation

Handle errors

Calls raise typed exceptions. A missing record raises NotFoundError, a rate limit raises RateLimitError, and an expired key raises AuthError. Each exception carries the request and the response, so that you can log what the API answered.

import asyncio

from bibliofabric import NotFoundError
from bibliofabric.crossref import CrossrefClient


async def main() -> None:
    async with CrossrefClient() as client:
        try:
            await client.works.get("10.1000/does-not-exist")
        except NotFoundError as error:
            print("not found:", error.status_code, error.request.url)


asyncio.run(main())

The script prints:

not found: 404 https://api.crossref.org/v1/works/10.1000%2Fdoes-not-exist?mailto=s.mok%40utwente.nl

mailto carries the contact address from CROSSREF_MAILTO, so your URL shows your own address. Crossref puts requests with a contact address in a faster pool, so set it once in your environment and every Crossref call benefits. Settings and credentials lists the variable for each client.

Where to go next

  • Pick a client for the source you need, then read its quick start.
  • What every client gives you explains paging, filters, settings, identifiers, and error handling once for all clients.
  • Example scripts lists a runnable script for every client, with the output each one prints.