MPRester.query NOT working

This returns the following for weeks now.

MPRestError: REST query returned with error status code 403

Any help will be appreciated.

What query are you trying to run?

Thanks for your response. Examples of the queries are the following:

  1. get_bandstructure_by_material_id

  2. mpr.query({“pretty_formula”:“SiO2”}, [“material_id”, “structure”])

  3. mpr.query({“pretty_formula”:“SiO2”}, [“material_id”, “formation_energy_per_atom”])

Are you making sure you’re using the MPRester object with your most recent API key? That’s the most common reason MPRester stops working.

Thanks again. It works with the following:

mpr.supported_properties

mpr.get_materials_ids

mpr._make_request(’/battery/all_ids’)

So, I don’t think it has to do with the API key

I should meant, I’m using the keys from mpr.supported_properties.

Examples of which are listed above.

Are those not the most recent keys?

All your queries work fine for me. When I have the wrong API key I get the following error:

REST query returned with error status code 403. Content: b'{"valid_response": false, "error": "API_KEY is not a valid key.", "version": {"db": "2020_09_08", "pymatgen": "2020.8.13", "rest": "2.0"}, "created_at": "2020-11-17T20:28:27.362237"}'

Can you post your full error message? Also, which version of pymatgen are you using?

The pymatgen version is 2020.11.11

I just generated another API key and used and it’s still giving the same error.

I appreciate your time.

I will be delighted to have this resolved.

You have no API key in your MPRester as the error says.
Try something like this:
with MPRester("INSERT_API_KEY_HERE") as mpr:

Great!

Thanks a lot for your time.

with MPRester(“INSERT_API_KEY_HERE”) as mpr:

has solved the problem.

I also meet this problem: “REST query returned with error status code 403”
How to solve it?
Thanks.

@SamWang We’ll need more information to be able to help you. Can you post the full error message and an example code snippet to reproduce the error? Make sure to NOT include your API key in your response. Thanks!

yes. as follows:
284 raise MPRestError(data[“error”])
→ 286 raise MPRestError(f"REST query returned with error status code {response.status_code}")
288 except Exception as ex:

MPRestError: REST query returned with error status code 403

During handling of the above exception, another exception occurred:

MPRestError Traceback (most recent call last)284 raise MPRestError(data[“error”])
→ 286 raise MPRestError(f"REST query returned with error status code {response.status_code}")
288 except Exception as ex:

MPRestError: REST query returned with error status code 403

During handling of the above exception, another exception occurred:

MPRestError Traceback (most recent call last)
Cell In[4], line 7
3 #from mp_api.client import MPRester # This is new version instead of older
4 ## Sorry. I dont know what is the reason of the two version all occurs error.
5 ## After update API Keys, still occurs error.
6 mpr = MPRester()
----> 7 struct = mpr.get_structure_by_material_id(“******”) 707 “”"
708 Get a Structure corresponding to a material_id.
709
(…)
717 Structure object.
718 “”"
719 prop = “final_structure” if final else “initial_structure”
→ 720 data = self.get_data(material_id, prop=prop)
721 if not data:
722 try:
288 except Exception as ex:
289 msg = f"{ex}. Content: {response.content}" if hasattr(response, “content”) else str(ex)
→ 290 raise MPRestError(msg)
MPRestError: REST query returned with error status code 403. Content: b’{“valid_response”: false, “error”: “API_KEY is not a valid key.”, “version”: {“db”: “2020_09_08”, “pymatgen”: “2022.0.8”, “rest”: “2.0”}, “created_at”: “2023-08-14T18:20:22.535904”}’

Please make sure to read our documentation at Using the API - Materials Project Documentation

The error message shows that you’re connecting to the legacy API which means you’re not actually using MPRester from the mp-api package. If you want to connect to legacy with data from 2020, make sure to upgrade to the latest pymatgen and use the legacy API key from Login. Our legacy website and API will soon be retired, though. To connect to our new API with the latest data, make sure to use the MPRester from the mp-api package with a new API key from Materials Project - Dashboard

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