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.0.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.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: materialsgalaxy_api-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 c28509e9d232c0a0c81e4ee41af72a7ff6248a34d06b6c90ac467b18dcd60c54
MD5 3c7bbd13724cdac8c368613054086495
BLAKE2b-256 60f239e8c6dd664dbde76aac8a2e2eb675a33d8ffe28185be2f18949f510bda9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: materialsgalaxy_api-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6dbc462e468b27f63d5725fe58102ef49176772e8431c4fc461f788263392bb1
MD5 d5099a9476237b8ddbcaed36bf92bad2
BLAKE2b-256 23d521c7abe4123eb367447755f3362b196dac51f5c7207331865e7b269853cb

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