Authentication¶
Check your account with a PAT¶
Create a PAT in User Settings → Tokens, then set NOMAD_PAT in your
process environment. This example calls GET /users/me
and returns an Account. Create a PAT in NOMAD
| Action | Required scope |
|---|---|
| Read your account in the examples below | 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)
See the me() return value and Account fields and methods for what you can do with the returned profile.
See the client setup example for reusable client options.
Sign in with your browser¶
REST endpoints used · OIDC discovery, authorization, and token exchange · GET /users/me
identity_provider="helmholtz-aai" opens Helmholtz ID. Choose your institution there. Omit the alias to use Central's standard sign-in page. NOMAD sign-in guide
from nomad_python_api import NomadClient, Scope
from nomad_python_api.identity import BrowserCredential
# Let the first account request open the browser.
credential = BrowserCredential(
issuer="https://nomad-lab.eu/fairdi/keycloak/auth/realms/fairdi_nomad_prod",
client_id="nomad_public",
discover=True,
identity_provider="helmholtz-aai",
)
with NomadClient(credential=credential, enforced_scopes={Scope.USERS_READ}) as client:
account = client.identity.me()
Get the API URL and Keycloak settings from your Oasis administrator. This example uses explicit Keycloak URLs and signs in before calling the API. For your own Keycloak, a proxy or private CA, brokered SSO, or an external SSO provider used directly, see Oasis identity setups. Oasis Keycloak setup
from nomad_python_api import NomadClient
from nomad_python_api.identity import BrowserCredential
endpoint = "https://oasis.example.edu/nomad-oasis/api/v1"
issuer = "https://login.example.edu/realms/materials"
# Use the explicit Keycloak endpoints supplied by your Oasis.
credential = BrowserCredential(
endpoint=endpoint,
issuer=issuer,
client_id="materials-python",
authorization_endpoint=f"{issuer}/protocol/openid-connect/auth",
token_endpoint=f"{issuer}/protocol/openid-connect/token",
identity_provider="university-sso",
)
credential.authenticate()
with NomadClient(credential=credential) as client:
account = client.identity.me()
Run browser login where Python and the browser share a loopback interface, including a local Jupyter kernel. By default, the SDK lets the operating system pick a free callback port and handles token renewal as requests need it. Use the advanced browser settings if your Oasis administrator gives you a fixed callback URL. The authorization request asks for openid. Add scope names with the advanced requested_scopes option only when your Oasis identity setup needs them. See Advanced: request scopes at sign-in.
Sign in with a username and password¶
REST endpoints used · POST /auth/token · GET /users/me
Replace the example username and set NOMAD_PASSWORD through a secure prompt or secret manager. The target deployment must enable Keycloak Direct Access Grants. Keycloak password grant · OAuth security guidance
import os
from nomad_python_api import NomadClient
from nomad_python_api.identity import PasswordCredential
# Replace the example username with your own.
username = "user1"
credential = PasswordCredential(username=username, password=os.environ["NOMAD_PASSWORD"])
with NomadClient(credential=credential) as client:
account = client.identity.me()
Pass the Oasis API URL and your username to the credential. The password comes from the environment.
import os
from nomad_python_api import NomadClient
from nomad_python_api.identity import PasswordCredential
# Send the password exchange to the selected Oasis.
credential = PasswordCredential(
endpoint="https://oasis.example.edu/nomad-oasis/api/v1",
username="user1",
password=os.environ["NOMAD_PASSWORD"],
)
credential.authenticate()
with NomadClient(credential=credential) as client:
account = client.identity.me()
See PasswordCredential for renewal and private CA settings. The token carries your full user scopes. Restrict the client with enforced_scopes, or run jobs with a scoped PAT. The advanced requested_scopes option does not restrict tokens on standard NOMAD. Details
Use an existing Keycloak access token¶
Set NOMAD_KEYCLOAK_ACCESS_TOKEN to the access token supplied by your sign-in application. Replace the credential when that application issues a fresh token.
import os
from nomad_python_api import NomadClient
from nomad_python_api.identity import BearerTokenCredential
# Use the access token obtained by your sign-in application.
credential = BearerTokenCredential(os.environ["NOMAD_KEYCLOAK_ACCESS_TOKEN"])
with NomadClient(credential=credential) as client:
account = client.identity.me()
Connect to an Oasis with a PAT¶
Set OASIS_PAT to a PAT issued by your Oasis. Replace endpoint with its API URL. Set ca_bundle to the path of its PEM bundle for a private CA, or to None for a publicly trusted CA. See the Oasis developer guide for custom REST contracts.
import os
from nomad_python_api import NomadClient
from nomad_python_api.identity import BearerTokenCredential
endpoint = "https://oasis.example.org/nomad-oasis/api/v1"
ca_bundle = "oasis-ca.pem" # Use None when the API uses a publicly trusted CA.
# Use a token issued by the same Oasis as the configured API URL.
credential = BearerTokenCredential(os.environ["OASIS_PAT"], endpoint=endpoint)
client = NomadClient(
credential=credential,
ca_bundle=ca_bundle,
)
try:
account = client.identity.me()
finally:
client.close()
In Jupyter, install the SDK in the active kernel's environment. In pyiron, create the client inside the worker using a job-provided secret.
Scope and access settings¶
See Access and scopes for enforced_scopes, requested_scopes, and granted_scopes, permission checks, and troubleshooting.