Invalid API Key (401) when connecting to index

I am attempting to connect to an existing index in Pinecone. The connection appears to work when storing the documents like so:

from langchain_community.vectorstores import Pinecone as LangchainPinecone
index_name = os.environ.get('PINECONE_RECIPE_INDEX') 
docsearch = LangchainPinecone.from_documents(my_documents, embeddings, index_name=index_name)
retriever = docsearch.as_retriever(search_type="mmr")

However, when I attempt to connect to this index again, I get an Invalid API Key error (401). I have double checked that my API key is correct.

Here is the code that is giving me the error:

pc = Pinecone(api_key=os.environ.get('PINECONE_API_ENV'))
index_name = os.environ.get('PINECONE_INDEX') 
# the line below is where the error occurs
index = pc.Index(index_name)
retriever = index.as_retriever(search_type="mmr")

Any advice would be helpful. Thanks.

Hi @david3. Can you share the full stacktrace of the error? Also, how are you invoking the script itself? If this is in a terminal can you confirm that

echo $PINECONE_API_KEY

returns the API key you see in the console? (Please don’t post the output of that here, we don’t want anyone else to have a copy of your API key.)

Hi Cory, I resolved the issue… was accessing the wrong Pinecone key. Thanks.