Materials Project API Client

0.46.0 · active · verified Tue Apr 14

mp-api is the official Python client for the Materials Project API, providing programmatic access to the vast Materials Project database. It features the MPRester class for easy data retrieval and is actively maintained with frequent updates to align with the evolving API and new data methodologies.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `MPRester` client using an API key from an environment variable and retrieve basic summary information for a material. You need a Materials Project API key, obtainable from your profile dashboard, to run this example.

import os
from mp_api.client import MPRester

# Set your Materials Project API key as an environment variable
# Example: export MP_API_KEY="YOUR_API_KEY_HERE"
# You can find your API key on your Materials Project profile dashboard.

api_key = os.environ.get('MP_API_KEY', '')

if not api_key:
    print("Error: MP_API_KEY environment variable not set. Please set it to your Materials Project API key.")
else:
    try:
        with MPRester(api_key) as mpr:
            # Fetch summary data for silicon (material_id: mp-149)
            material_id = "mp-149"
            docs = mpr.materials.summary.search(material_ids=[material_id])
            if docs:
                print(f"Material ID: {docs[0].material_id}")
                print(f"Formula: {docs[0].formula_pretty}")
                print(f"Band gap: {docs[0].band_gap} eV")
            else:
                print(f"No data found for material ID: {material_id}")
    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →