Skip to content

Set up a client

Summary

Create a credential, then pass it to NomadClient. The credential selects the NOMAD API endpoint. Leave the endpoint at its default for NOMAD Central, or set the complete HTTPS API v1 URL for your Oasis.

Use with NomadClient(credential=credential) as client: to close the client at the end of the session. The client reference covers constructor options and ownership. For connection pools and worker examples, see Connections and HPC.

Open a client and check your account

Set NOMAD_PAT to a token issued by NOMAD Central. This example checks which account the token belongs to and closes the client when the lookup ends.

Action Required scope
Construct or close a client None
Read your account with identity.me() users:read
import os

from nomad_python_api import NomadClient, Scope
from nomad_python_api.identity import BearerTokenCredential

credential = BearerTokenCredential(os.environ["NOMAD_PAT"])

with NomadClient(
    credential=credential,
    enforced_scopes={Scope.USERS_READ},
) as client:
    # Confirm which account the token belongs to.
    account = client.identity.me()
    print(account.user_id)

Advanced client options for workers

Use a Python dictionary when a script builds the same client options for multiple workers or runs. Include the job's credential and unpack the dictionary with **options. The keys match the NomadClient constructor.

import os

from nomad_python_api import NomadClient, Scope
from nomad_python_api.identity import BearerTokenCredential

options = {
    "credential": BearerTokenCredential(os.environ["NOMAD_PAT"]),
    "enforced_scopes": {Scope.USERS_READ},
}

with NomadClient(**options) as client:
    # Inspect operation metadata locally before making the API call.
    info = client.identity.get_operation_info("me")
    print(info.description)
    print([scope.value for scope in info.required_scopes])
    print(client.enforced_scopes)
    account = client.identity.me()

For process-based work, construct the client inside each worker.