Skip to main content

Invenio REST API Client

PyPI - Version PyPI - Python Version License

A typed Python client for the InvenioRDM REST API, with particular emphasis on the lifecycle of records and drafts.

Why this project exists

The original Invenio OpenAPI description documented the available operations, but at the time this client was developed it did not provide the response schemas required to generate a useful typed client.

This project therefore follows a schema-first reconstruction workflow:

  1. study the human-readable InvenioRDM REST API documentation;
  2. reconstruct the missing data and response schemas from the documented fields and examples;
  3. add those schemas to the OpenAPI description;
  4. generate Pydantic models and a Python client from the enhanced contract.

Most examples and validation work focus on operations associated with the OpenAPI Records tag and the related draft, file, and version endpoints.

Install

pip install invenio-rest-api-client

Python 3.10 or newer is required.

Configure a client

Create an API token in your InvenioRDM account and expose the instance URL and token as environment variables:

export INVENIO_BASE_URL="https://invenio.example.org"
export INVENIO_TOKEN="replace-with-your-token"

Then create an authenticated client:

import os

from invenio_rest_api_client import AuthenticatedClient

client = AuthenticatedClient(
    base_url=os.environ["INVENIO_BASE_URL"],
    token=os.environ["INVENIO_TOKEN"],
    raise_on_unexpected_status=True,
)

Search and retrieve records

Every endpoint module provides sync, sync_detailed, asyncio, and asyncio_detailed functions.

from invenio_rest_api_client.api.records import (
    get_a_record_by_id,
    search_records,
)

with client:
    results = search_records.sync(
        client=client,
        q='metadata.title:"climate"',
        size="10",
        page="1",
    )

    record = get_a_record_by_id.sync(
        "abcde-12345",
        client=client,
    )

Use the detailed form when status codes, headers, or raw response content matter:

from http import HTTPStatus

from invenio_rest_api_client.api.records import get_a_record_by_id

with client:
    response = get_a_record_by_id.sync_detailed(
        "abcde-12345",
        client=client,
    )

if response.status_code is HTTPStatus.OK:
    record = response.parsed
else:
    raise RuntimeError(
        f"Invenio returned {response.status_code}: "
        f"{response.content.decode(errors='replace')}"
    )

Create and publish a record

The generated request models make the minimum record metadata explicit:

from datetime import date

from invenio_rest_api_client.api.drafts import publish_a_draft_record
from invenio_rest_api_client.api.records import create_a_draft_record
from invenio_rest_api_client.models import (
    Access,
    AccessFiles,
    AccessRecord,
    CreateADraftRecordBody,
    Creator,
    Files,
    Metadata,
    PersonOrOrg,
    PersonOrOrgType,
    ResourceType,
    ResourceTypeId,
)

body = CreateADraftRecordBody(
    access=Access(
        record=AccessRecord.PUBLIC,
        files=AccessFiles.PUBLIC,
    ),
    files=Files(enabled=False),
    metadata=Metadata(
        resource_type=ResourceType(id=ResourceTypeId.DATASET),
        title="Climate observations",
        publication_date=date.today(),
        creators=[
            Creator(
                person_or_org=PersonOrOrg(
                    type=PersonOrOrgType.PERSONAL,
                    given_name="Ada",
                    family_name="Lovelace",
                )
            )
        ],
        publisher="Example Repository",
    ),
)

with client:
    draft = create_a_draft_record.sync(client=client, body=body)
    if draft is None or draft.id is None:
        raise RuntimeError("Invenio did not return the created draft")

    published = publish_a_draft_record.sync(draft.id, client=client)
    if published is None:
        raise RuntimeError("Invenio did not return the published record")

Async usage

The async API mirrors the synchronous API:

from invenio_rest_api_client.api.records import get_a_record_by_id

async with client:
    record = await get_a_record_by_id.asyncio(
        "abcde-12345",
        client=client,
    )

Documentation

The documentation follows the Diátaxis convention:

Development

Run the test and quality suite with:

task quality:pre-commit:run

Build the documentation locally with:

mkdocs build --strict

License

Licensed under the Apache License 2.0.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

invenio_rest_api_client-12.0.3.tar.gz (198.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

invenio_rest_api_client-12.0.3-py3-none-any.whl (210.7 kB view details)

Uploaded Python 3

File details

Details for the file invenio_rest_api_client-12.0.3.tar.gz.

File metadata

  • Download URL: invenio_rest_api_client-12.0.3.tar.gz
  • Upload date:
  • Size: 198.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for invenio_rest_api_client-12.0.3.tar.gz
Algorithm Hash digest
SHA256 545c968bdfdd3fc1f1fa848393fd0bb0eb5eca1fd46e27bf03d7e8ef5d650e81
MD5 76d3fa6e3b9956b6cb65af4375abbe0d
BLAKE2b-256 cd4c0494daa45de1b260d96c09a07deac367c9aca0848c59d2ce1eddbbe4dfc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for invenio_rest_api_client-12.0.3.tar.gz:

Publisher: package.yaml on Terradue/invenio-rest-api-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file invenio_rest_api_client-12.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for invenio_rest_api_client-12.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5ac0941c63e98729200427a982e25bfcec731ed05292462e19d904df03c782c4
MD5 bcb47cac7b6e7d8c7f71d95dc8134936
BLAKE2b-256 72c127941b324dc7323d4f42242d0cf71dd11190a18e42e36533ecf224fb4844

See more details on using hashes here.

Provenance

The following attestation bundles were made for invenio_rest_api_client-12.0.3-py3-none-any.whl:

Publisher: package.yaml on Terradue/invenio-rest-api-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

12.0.4

2 files

This release

12.0.3 This release

2 files

12.0.2

2 files

12.0.1

2 files

12.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page