I keep getting error 401 - when trying to load my API key in Python / Powershell

I have generated so many new keys, my account has $10 credit, what I’m i doing wrong? to the point where my account on api is now a mess.

I have three organisations, and 2 projects nothing i tried works

Current Status- i keep getting this error in python/poweshell

  1. Initial Issue: The API key you were using (sk-proj-V_U9-) resulted in a 401 Unauthorized error, indicating it was invalid or revoked.

  2. OpenAI Dashboard: The first screenshot showed no API keys under the “Default project” in the “Yello” organization, confirming that the key you were using was either deleted, revoked, or never existed in that project.

  3. New Key Created: You created a new key ( KEY) in the “Default project” under “Yello.”

  4. Multiple Projects/Organizations: The later screenshots show that you have multiple organizations (“Personal” and “Yello”) and projects within “Yello” (“Default project,” “Yellow ,” and “Yelo”). Additionally, there are older keys in other projects (e.g., sk-…t1AA in “Yelo” and sk-…q5UA in “Yellow Deck”), but they were created on March 20, 2025, and have never been used.

Read back report

  • PowerShell Environment Variable: The API key is correctly set in PowerShell, as confirmed by:

    • $env:OPENAI_API_KEY | Out-String -Width 200 showing the full key.

    • $env:OPENAI_API_KEY.Length reporting 166.

  • Python Environment Variable: Python successfully reads the key from the environment variable:

    • Loaded API Key: [api-key]

    • Length: 166

  • API Call Failure: The OpenAI API rejected the key with a 401 error:

    • {‘error’: {‘message’: ‘Incorrect API key provided: sk-proj-******…******eHsA. You can find your API key at https://platform.openai.com/account/api-keys.’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: ‘invalid_api_key’}}

The 401 error means the API key is either:

  • Invalid (e.g., it was never a valid key).

  • Revoked or deleted in your OpenAI account.

  • Not associated with an account that has the necessary permissions or credits to make API calls.

The key issue is that the original key you were using doesn’t exist according to this error code, in the “Default project” (or any visible project), which explains the 401 error. The new key you created should work, but we need to ensure it’s correctly set up in your environment and that you’re using the right project/organization context. this is my day 8 hours long no luck

Hey, @reiniergeel … welcome to the community!

If you post your API key publicly, as you did here, OpenAI will nullify it.

Did you maybe post the key somewhere publicly first?