Skip to content

Dataverse

The Dataverse client (bibliofabric.dataverse) is a Python client for the Dataverse native API and search API. One client serves every Dataverse installation, because they all expose the same routes: the default points at the Harvard Dataverse, and you pass another installation with base_url=. It covers the search API, collections, datasets (with versions, files, metadata, and exports), file downloads, metadata block definitions, server info, and the metrics routes. Every call returns a typed Pydantic model, and the client keeps a default pace under 2 requests per second. It ships an async client and a generated sync twin with the same API. Install follows the setup guide.

Credentials

Public reads need no key. Private data and the write routes need an API token, which the client sends as an X-Dataverse-key header: set the DATAVERSE_API_KEY environment variable or pass DataverseClient(api_key=...); an explicit argument wins over the environment value, and api_key="" forces anonymous access. mailto= sets the contact address in the User-Agent. See settings for the DATAVERSE_ variables.

The write routes (create dataset, upload file, publish, dataverse management) are out of scope; this client reads only. Each dataset version carries its own license: new datasets default to CC0 1.0 on most installations, and the license field of a version names it.

Quick start

import asyncio

from bibliofabric.dataverse import DataverseClient


async def main():
    async with DataverseClient() as client:
        page = await client.search.search(
            query="*",
            type="dataset",
            sort="date:desc",
            page_size=3,
            show_entity_ids=True,
        )
        print(page.total, [r.name for r in page.results])

        # search results carry the numeric id only with show_entity_ids;
        # the persistent id (doi:...) works on every dataset route.
        dataset = await client.datasets.get(page.results[0].entity_id)
        print(dataset.persistent_id, dataset.latest_version.version)

        version = await client.datasets.version(dataset.id, ":latest")
        print(version.metadata_blocks["citation"].fields[0].type_name)


asyncio.run(main())

search() returns a Page of SearchResult; datasets.get() accepts the numeric id or a doi: persistent id; version() takes one of :latest, :draft, :latest-published, or a major.minor pair. The script prints, on the Harvard Dataverse:

308675 ['Replication data for: ...', ...]
doi:10.7910/DVN/27221 1.0
title

Sync quick start

DataverseSyncClient is generated from the async source and takes the same calls without async:

from bibliofabric.dataverse import DataverseSyncClient

with DataverseSyncClient(base_url="https://demo.dataverse.org/api/v1/") as client:
    version = client.info.version()
    metric = client.metrics.get("datasets")
    print(version.version, metric.count)

When to use which client is covered in async or sync.

What the client offers

Attribute Returns Common methods Client-specific methods
client.search SearchResult search, iterate, collect, first, count; get is disabled sort reads "date", "name", "score", and "field:asc\|desc" spellings; subtree, fq, and metadata_fields accept one value or a list
client.dataverses Dataverse get; search and friends are disabled contents() lists the direct children, facets() and facet_details() read the facet configuration
client.datasets Dataset get; search and friends are disabled version() fetches one version, versions() lists them, files() lists the files of one version, metadata() returns the typed blocks, export() returns one exporter's answer as text
client.files FileMetadata get; search and friends are disabled none
client.access none None of the common methods datafile() and dataset() stream downloads to a file path
client.info ServerVersion, ServerInfo None of the common methods export_formats(), metadatablocks(), metadatablock(name)
client.metrics MetricResponse None of the common methods get(name, monthly=, month=, days=, parent_alias=, data_location=, country=)

Every identifier argument accepts the numeric id or the doi: persistent id (files accept a file doi); collection routes accept the alias. Paging on the search route uses start and per_page with a cap of 1,000 per page; the versions and files listings page with limit and offset. The details are in paging and search.

Examples

Runnable scripts live in packages/bibliofabric/examples/dataverse:

  • quickstart.py searches datasets, fetches one, and reads its typed metadata.
  • files.py lists the files of a dataset and downloads the smallest one into a temporary directory.

Next