403 returned, the api_key doesn't work

Hi, I was trying to use MPDataRetrieval to retrieve the data with my API key, but it returns 403. I’ve tried both from https://legacy.materialsproject.org/dashboar, and https://next-gen.materialsproject.org/api, but both don’t work.

Then, I tried to request “'https://www.materialsproject.org/rest/v2/'tasks”, but 404 is returned. May anybody help me?

Please see our API docs for guidance about how to use the mp-api client to retrieve data from MP: Using the API - Materials Project Documentation
Questions about matminer’s MPDataRetrieval class should be posted in the matminer forum.

Thank you for your prompt reply. Can I move the post or not? Besides, I have read the documentation, but I can not solve it. How can I deal with it?

I just request the data with my keys, but 404 returned, so it should be the key problem?

Please try the latest version of the mp-api client first with the API key from your next-gen dashboard. If it errors out, post a code snippet (dont include your api key!) and the error message.

Thank you for your advice. I have tried the following code with the key from “https://next-gen.materialsproject.org/api”:

from mp_api.client import MPRester
mpr = MPRester("my api_key")
mpr.summary.search(material_ids=["mp-149", "mp-13", "mp-22526"])

But it still doesn’t work. It returns

f"REST query returned with error status code {response.status_code} "
mp_api.core.client.MPRestError: REST query returned with error status code 403 on URL https://api.materialsproject.org/summary/?material_ids=mp-149&material_ids=mp-13&material_ids=mp-22526&all_fields=True&limit=1000 with message:
Response {"error": "You are using deprecated API endpoints. Please read our documentation (https://docs.materialsproject.org) and upgrade to the latest version of the mp-api client (https://pypi.org/project/mp-api/).", "version": "blocked"}

I have upgraded my pymatgen and mp_api using pip install --upgrade pymatgen mp_api.

Besides, I have also tried MPDataRetrieval with the following code:

from matminer.data_retrieval.retrieve_MP import MPDataRetrieval
mpdr = MPDataRetrieval(api_key)
query_criteria = {'e_above_hull': {'$lte': 0.08}, 'nelements': {'$gte': 3, '$lte': 4}, 'nsites': {'$lte': 40}}
query_properties = ['material_id', 'formation_energy_per_atom', 'band_gap', 'pretty_formula', 'e_above_hull', 'elements', 'cif', 'spacegroup.number']
dataframe = mpdr.get_dataframe(criteria = query_criteria, properties = query_properties, )

It returns:

raise MPRestError(f"REST query returned with error status code {response.status_code}")
pymatgen.ext.matproj.MPRestError: REST query returned with error status code 403

During handling of the above exception, another exception occurred:

pymatgen.ext.matproj.MPRestError: REST query returned with error status code 403. Content: b'{"valid_response": false, "error": "Invalid request method.", "version": {"db": "2020_09_08", "pymatgen": "2022.0.8", "rest": "2.0"}, "created_at": "2023-10-13T09:42:35.368001"}'

Thank you very much for your help!

Thanks!

So does the first snippet work after upgrading mp-api? If not, please post the error message.

Note that in the second snippet, MPDataRetrieval seems to be using the legacy API which means you’ll have to use the legacy API key for it and you’ll also obtain outdated data. The continue using legacy the API endpoint would have to be changed to legacy.materialsproject.org explicitly. The better solution would be to update MPDataRetrieval to use the mp-api client directly. Tagging @ardunn here to help with matminer.

Yes, the first snippet work after the upgrade. My pymatgen is 2022.0.17, and mp-api is 0.18.0.

Besides, I have also tried the legacy API key for the second snippet, the same error message was returned. How may I solve it because I may want to try outdated data first to align with the printed paper? May you also give me some suggestions for the MPDataRetrieval to use the mp-api client directly? @ardunn

Thank you all very much for your kind helps!

ok great. Both your pymatgen and mp-api versions are very old though. Try upgrading to mp-api==0.37.5 and pymatgen==2023.10.11

Thank you very much for your advice. However, I am trying tensorflow==1.15 with python==3.7 because of the original project of the printed paper. Therefore, I can not upgrade to mp-api==0.37.5 and pymatgen==2023.10.11. Are there any solution such as the legacy API?

By the way, it works after upgrading to mp-api==0.37.5 and pymatgen==2023.10.11, but since the project is tensorflow1, I wonder if there is any other solution? Thanks

Ok, glad to hear. python==3.7 is past its end of life unfortunately (see Status of Python versions) and thus not supported in MP libraries anymore. You might have to try find a tensorflow version that supports a recent version of python but doesn’t include any code changes that could materially change the results for you. We unfortunately can’t help with that.

Okay. Thank you very much for your help.

Maybe the easiest solution here is just to upgrade matminer to the newest mp-api…

1 Like

matminer is no longer supported for the newest mp-api, right? I think the MPDataRetrieval doesn’t work now. Thank you for your reply.

@zhlfmyzhh yes that’s what @ardunn meant. Please follow this issue in the matminer repo for progress updates.

Thread closed due to inactivity, please open a new thread to address related issues.