Python mp-api REST error 401 "Invalid authentication credentials" shows up for all else than the summary document

Hi,

I’m trying to use mp-api to retrive data (specifically, DOS) from materials project database via mpids but have problems; only “summary” document seems to be working. I tested with various mpids to no avail.
I tested both get_dos_from_material_id method, and get_dos_from_task_id method.
Actually, things like thermo doc from the tutorial did not work either.

My simple testcode is as below.
+++++++++++++++++++++++++++++++++
from mp_api import MPRester
with MPRester(mpkey) as mpr:
results = mpr.summary.get_data_by_id(‘mp-1’)
print(results.dos)
###until here no errors, but the next two lines produce 401 error
esdos_doc = mpr.electronic_structure_dos.get_dos_from_material_id(“mp-1”)
esdos_doc = mpr.electronic_structure_dos.get_dos_from_task_id(“mp-1686392”) ###this is the task id that showed up in summary for dos calculation
+++++++++++++++++++++++++++++++++

Apologies if this is some stupid mistake of mine.
I am using anaconda environment python 3.8.13 on a ubuntu workstation. I installed mp_api version 0.21.6 with pip.
The full error message is as below.

+++++++++++++++++++++++++++++++++
Traceback (most recent call last):
File “aa.py”, line 21, in
esdos_doc = mpr.electronic_structure_dos.get_dos_from_material_id(‘mp-1’)
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/routes/electronic_structure.py”, line 408, in get_dos_from_material_id
dos_data = es_rester.get_data_by_id(
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/core/client.py”, line 757, in get_data_by_id
new_document_id = mpr.get_materials_id_from_task_id(document_id)
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/client.py”, line 256, in get_materials_id_from_task_id
docs = self.materials.search(task_ids=[task_id], fields=[“material_id”])
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/core/client.py”, line 810, in search
return self._get_all_documents(
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/core/client.py”, line 859, in _get_all_documents
results = self._query_resource(
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/core/client.py”, line 294, in _query_resource
data = self._submit_requests(
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/core/client.py”, line 401, in _submit_requests
initial_data_tuples = self._multi_thread(
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/core/client.py”, line 612, in _multi_thread
data, subtotal = future.result()
File “/home/sjay/anaconda3/envs/latestpymatgen/lib/python3.8/concurrent/futures/_base.py”, line 437, in result
return self.__get_result()
File “/home/sjay/anaconda3/envs/latestpymatgen/lib/python3.8/concurrent/futures/_base.py”, line 389, in __get_result
raise self._exception
File “/home/sjay/anaconda3/envs/latestpymatgen/lib/python3.8/concurrent/futures/thread.py”, line 57, in run
result = self.fn(*self.args, **self.kwargs)
File “/home/sjay/.local/lib/python3.8/site-packages/mp_api/core/client.py”, line 680, in _submit_request_and_process
raise MPRestError(
mp_api.core.client.MPRestError: REST query returned with error status code 401 on URL https://api.materialsproject.org/materials/?task_ids=mp-1&limit=1000&fields=material_id with message:
Response {
“message”:“Invalid authentication credentials”

The problem seems to have been associated with my api key.

The one that I’ve been using was taken from dashboard of www.materialsproject.org.
I made a new ID at Materials Project - API, and used
the new key generated there which solved everything.

So it seems the new mp-api library is not comatiable with the old api-key. Is this correct?

Thank you all in advanced.

Hi @sjk,

That is correct. If you would like to use the new client in mp-api, you will have to get your updated API key from Materials Project - API.

– Jason

Hello Jason,

I seem to be having a similar error where it is saying my api_key is invalid how do I update the api_key?