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)

List available summary data sources before filtering by source:

with MGClient() as mg:
    sources = mg.materials.list_data_sources()

for source in sources:
    print(source.source, source.count)

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
  • DataSource
  • 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.2.tar.gz (30.9 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.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: materialsgalaxy_api-0.1.2.tar.gz
  • Upload date:
  • Size: 30.9 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.2.tar.gz
Algorithm Hash digest
SHA256 435b49a895bba611d06a2d8d1ae0637b267737efb5a12c912d1e384cf539102e
MD5 f17bb7e193cac35cb6bf458dc86afb9d
BLAKE2b-256 3cc61904af39939b13d11403851be4fdf8fc7ed48f7a6531d3c2eed56b2be0b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: materialsgalaxy_api-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 105f6d5cd644c7a75e691044c23a6579cce91ad7a1d04ef3c4bd5e0624cfa50a
MD5 3626d02250e324d1ee7bd0957d20fbb2
BLAKE2b-256 fb530148fdb3f9033a12bb25efa0b1d6f8fa7fdb77afb704a699df5875c1ce6d

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