Skip to content

VIAF

The VIAF client (bibliofabric.viaf) looks up Virtual International Authority File clusters by id, LCCN, and source-local id, searches them with CQL, and queries the AutoSuggest index. It talks to the routes of the OCLC VIAF API under https://viaf.org/viaf, parses the content-negotiated XML into Pydantic models, and paces requests at two per second over two connections. Install bibliofabric first if you have not (setup). An async client and a generated sync twin share the same API.

Credentials

The API needs no credentials. The client reads only pacing settings from the VIAF_ environment prefix; see settings. VIAF publishes the dataset under the ODC Attribution License; the VIAF data page names the preferred attribution form.

Quick start

import asyncio

from bibliofabric.viaf import ViafClient


async def main():
    async with ViafClient() as client:
        cluster = await client.clusters.get("102333412")
        print(cluster.viaf_id, cluster.name_type, cluster.headings[0])

        page = await client.clusters.search(
            query='local.personalNames all "tolkien"', page_size=5
        )
        print(f"{page.total} matches")
        for record in page.results:
            print(record.viaf_id, record.headings[:1])

        for hit in await client.suggest.clusters("austen"):
            print(hit.term, hit.viaf_id, hit.source_ids.get("lc"))


asyncio.run(main())

The script fetches one cluster by id, runs one CQL search, and asks the AutoSuggest index for name forms that start with "austen". Run it and it prints:

102333412 Personal 1775-12-16 1817-07-18
Austen, Jane, 1775-1817.
93 clusters for 'local.personalNames all "tolkien"'
7468167807446118130007 ['Tolkien, John Ronald Reuel, 1892-1973']
6555176021966901500007 ['Finrod Felagund (postać fikcyjna)']
449 brief work clusters for hamlet
Austen, Jane, 1775-1817 102333412 n79032879
Austen Henry Layard, 1817-1894 9943394 n50039774
Austen Chamberlain, 1863-1937 66482160 n85298947

Search totals and AutoSuggest hits come from the live index and move over time, so your numbers may differ.

Sync quick start

ViafSyncClient has the same methods without await:

from bibliofabric.viaf import ViafSyncClient


def main():
    with ViafSyncClient() as client:
        cluster = client.clusters.get("102333412")
        print(cluster.headings[0])

        total = client.clusters.count(query='local.corporateNames all "oclc"')
        print(f"{total} corporate clusters mention oclc")


main()

What the client offers

ViafClient exposes three resources.

Attribute Record model Methods
client.clusters ViafCluster get, by_lccn, by_source_id, search, iterate, collect, first, exists, count, explain
client.brief_clusters ViafBriefCluster search, iterate, collect, first, count
client.suggest ViafSuggestion clusters

client.clusters.get() accepts bare numbers (102333412), padded numbers (00000000000102333412), and https://viaf.org/viaf/... URLs; ids go through normalize_viaf(). by_lccn() takes an LCCN such as n79032879, and by_source_id() pairs a source code with its local id, for example ("WKP", "Q36322") for the Wikipedia id.

Searching

Search runs SRU over GET /viaf/search. Build the CQL query yourself or with the cql() helper:

from bibliofabric.viaf import cql

query = cql("local.personalNames", "all", "tolkien")
page = await client.clusters.search(query=query, page=2, page_size=10)

The full cluster schema answers at most 10 clusters per page; the BriefVIAF schema answers at most 250. client.clusters defaults to a page_size of 10 and rejects larger values; client.brief_clusters defaults to 100 and rejects values above 250. sort accepts only "holdingscount", the one sort the server supports. explain() returns the server facts: index names, the default maximumRecords, and the record count of the index.

See upstream quirks for the server behaviours the client works around, and the API reference for the full surface.