[Error-403 Forbidden] I entered the API key correctly, but I got a 403 error

This is the error displayed when running the code.

(403)
Reason: Forbidden
HTTP response headers: HTTPHeaderDict({'Date': 'Fri, 08 Nov 2024 22:07:38 GMT', 'Content-Type': 'text/plain', 'Content-Length': '9', 'Connection': 'keep-alive', 'x-pinecone-auth-rejected-reason': 'Wrong API key', 'www-authenticate': 'Wrong API key', 'server': 'envoy'})
HTTP response body: Forbidden


I reissued the API key and tried again, but the same error persists. The API key seems valid, so I’m uncertain why this error keeps occurring.

Additionally, I upgraded to the Standard plan to see if that would help, but the issue still wasn’t resolved.

As an alternative solution, I also added sleep(30) immediately after creating the Pinecone index, but this didn’t resolve the error either. This approach doesn’t appear to address the underlying issue.

API_KEY = "~" 
pinecone = Pinecone(api_key=API_KEY)

index_name = "stock-research"
if index_name not in pinecone.list_indexes().names():
    pinecone.create_index(
        name=index_name,
        dimension=768,
        metric='cosine',
        spec=ServerlessSpec(cloud='aws', region='us-east-1') 
    )
    time.sleep(30) 

index_description = pinecone.describe_index(index_name)
host = index_description.host

index = Index(index_name, host=host)


This function, upsert_data_from_db, reads data from an SQLite database and uploads it to a Pinecone index.

def upsert_data_from_db(db_path):
    # Connect to the database
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    # Select data
    cursor.execute("SELECT title, stock_name, broker, date, goal_price, recommendation, sub_title, body FROM research")
    rows = cursor.fetchall()

    # Upload each row to Pinecone
    for row in rows:
        title, stock_name, broker, date, goal_price, recommendation, sub_title, body = row
        try:
            # Generate vector embedding from the body
            embedding = get_embedding(body)

            # Upload data to Pinecone
            index.upsert([
                (
                    title,  # Vector ID
                    embedding,  # Embedding vector
                    {  # Metadata
                        "stock_name": stock_name,
                        "broker": broker,
                        "date": date,
                        "goal_price": goal_price,
                        "recommendation": recommendation,
                        "sub_title": sub_title,
                        "body": body
                    }
                )
            ])
            print(f"'{title}' data successfully uploaded to Pinecone.")

        except Exception as e:
            print(f"Error occurred while uploading data: {e}")


When running the code, I can confirm that the index is successfully created on the Pinecone site. However, the data has not been uploaded.


Is there something I’m missing? Please let me know if you need any more information.

Hi @01037269288a there are some potential issues standing out in your code, versus the documentation

1.) The initialization should be done using the gRPC client 1:

python
from pinecone.grpc import PineconeGRPC as Pinecone

pc = Pinecone(api_key="YOUR_API_KEY")
For index creation and connection, you should wait for the index to be ready before proceeding 2:
python
while not pc.describe_index(index_name).status['ready']:
    time.sleep(1)

# connect to index
index = pc.Index(index_name)
time.sleep(1)
For upserts, the vectors parameter should be formatted as a list of tuples containing (id, values, metadata) 3:
python
index.upsert(
    vectors=records,
    namespace="example-namespace"
)

2.) The 403 Forbidden error with “Wrong API key” suggests an authentication issue. Please make sure:

  • You’re using the correct API key from your Pinecone console 1
  • The API key is properly initialized with the client