Skip to main content

Python client for the MaterialsGalaxy API

Project description

MaterialsGalaxy SDK

Python client for the MaterialsGalaxy API.

Install

pip install materialsgalaxy-api

For local development from this repository:

cd sdk
uv sync --dev

Authentication

Pass an API key directly:

from materialsgalaxy import MGClient

with MGClient(api_key="sk-...", base_url="https://materialsgalaxy.iphy.ac.cn/api/v1") as mg:
    structure = mg.materials.get_structure("mg-1")

Or set MATERIALSGALAXY_API_KEY:

export MATERIALSGALAXY_API_KEY="sk-..."

Search Materials

search() returns a PagedSummary model. Use fields to request only the fields you need.

from materialsgalaxy import MGClient

with MGClient(base_url="https://materialsgalaxy.iphy.ac.cn/api/v1") as mg:
    page = mg.materials.search(
        elements=["Si", "O"],
        fields=["mg_id", "reduced_formula", "properties.band_gap"],
        page_size=10,
    )

print(page.total)
print(page.data[0].mg_id)
print(page.data[0].properties)

Detail Endpoints

with MGClient(api_key="sk-...") as mg:
    summary = mg.materials.get_summary("mg-1")
    structure = mg.materials.get_structure("mg-1")
    growth = mg.materials.get_single_crystal_growth("mg-1")
    electronic = mg.materials.get_electronic_structure("mg-1", soc=False)
    topology = mg.materials.get_topology("mg-1", soc=False)
    phonons = mg.materials.get_phonons("mg-1")
    ferroelectric = mg.materials.get_ferroelectric("mg-1")
    nonlinear = mg.materials.get_nonlinear_optical("mg-1")

print(structure.symmetry.symbol if structure else None)
print(topology.topo_class if topology else None)

Search Then Fetch Details

with MGClient(api_key="sk-...") as mg:
    page = mg.materials.search(
        formula="SiO2",
        fields=["mg_id", "reduced_formula"],
        page_size=5,
    )
    structures = mg.materials.get_structures([item.mg_id for item in page.data if item.mg_id])

for structure in structures:
    print(structure.mg_id, structure.lattice.volume if structure else None)

Use iter_search() to page through all matches:

with MGClient(api_key="sk-...") as mg:
    for material in mg.materials.iter_search(
        elements=["Si", "O"],
        fields=["mg_id", "reduced_formula"],
        page_size=100,
    ):
        print(material.mg_id, material.reduced_formula)

Returned Models

The SDK returns Pydantic models that mirror the public API contract:

  • PagedSummary
  • Summary
  • CrystalStructure
  • SingleCrystalGrowth
  • ElectronicStructure
  • TopologicalProperties
  • TopologicalPhonon
  • Ferroelectric
  • NonlinearOpticalProperties

Summary fields are optional because fields=[...] can request partial summary payloads.

Errors

from materialsgalaxy import MGClient
from materialsgalaxy.exceptions import MaterialsGalaxyAuthenticationError, MaterialsGalaxyHTTPError

try:
    with MGClient(api_key="sk-...") as mg:
        mg.materials.get_structure("mg-1")
except MaterialsGalaxyAuthenticationError:
    print("Invalid or revoked API key")
except MaterialsGalaxyHTTPError as exc:
    print(exc.status_code, exc.detail)

Project details


Download files

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

Source Distribution

materialsgalaxy_api-0.1.1.tar.gz (30.6 kB view details)

Uploaded Source

Built Distribution

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

materialsgalaxy_api-0.1.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file materialsgalaxy_api-0.1.1.tar.gz.

File metadata

  • Download URL: materialsgalaxy_api-0.1.1.tar.gz
  • Upload date:
  • Size: 30.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CentOS Linux","version":"7","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for materialsgalaxy_api-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ef264d7b44635f220072f5de2e48af9135924597f012dc0a32c26d43f5182256
MD5 1dac916b61f89a0d9e7077a51143dc18
BLAKE2b-256 28a1c7b4a11cfb681961a23432505eb23bc6a9a19ae416cb46ff243d90dcdf5e

See more details on using hashes here.

File details

Details for the file materialsgalaxy_api-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: materialsgalaxy_api-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"CentOS Linux","version":"7","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for materialsgalaxy_api-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 00f43f10a73f3625ef5eeb19f5ca5a9bda847ed58d134393a99c34e5b305b166
MD5 b84c73e03de9349bd744fe9e4d7dc1ff
BLAKE2b-256 f0d32a136a2db9fa22ba1b4698a23942697d78eebb3fc5ae257cc76c66aec87e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page