/n8n-tutorials

How to fix “invalid API key” error from OpenAI in n8n?

Learn how to fix the “invalid API key” OpenAI error in n8n with simple steps to restore workflow automation and ensure smooth API connections.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to fix “invalid API key” error from OpenAI in n8n?

The “invalid API key” error from OpenAI in n8n is almost always caused by a wrong or outdated key in your n8n Credentials, the wrong Authentication method selected, or sending the key in the wrong header when using an HTTP Request node. The fastest fix is: open your OpenAI credential in n8n, paste a fresh API key from platform.openai.com (or the new API Dashboard if your org uses it), make sure the credential type is set to Header Auth with key name Authorization and value format Bearer YOUR\_KEY, save it, and then re-run the workflow.

 

Why the Error Happens

 

n8n simply forwards whatever API key you gave it. If the key is wrong, expired, revoked, or formatted incorrectly, OpenAI immediately responds with “invalid API key”. This is not n8n logic failing — it’s OpenAI rejecting the request.

Common root causes:

  • Using an old OpenAI key that has been auto‑revoked (OpenAI now regularly rotates keys if they appear leaked).
  • Pasting the key with an extra space or newline.
  • Using the wrong credential type inside n8n (for example “Header Auth” vs “OAuth2”).
  • Using the HTTP Request node without sending the header as Authorization: Bearer YOUR\_KEY.
  • Using the wrong organization/project key if your OpenAI account has multiple projects.

 

How to Fix It (Step-by-Step)

 

These steps assume you’re using the real OpenAI integration in n8n or a manual HTTP Request node.

  • Get a brand new key from OpenAI. Go to platform.openai.com → API Keys → create new key. Copy it once; OpenAI will not show it again.
  • Open n8n → Credentials → OpenAI (or whatever name you used). If you used an HTTP Request credential, open that one instead.
  • Make sure the auth type is correct. For OpenAI’s API, n8n typically uses “Header Auth” or the built‑in “OpenAI API” credential depending on your version. If using Header Auth, the header must be:
{
  "Authorization": "Bearer YOUR_OPENAI_KEY"
}
  • Remove any spaces, tabs, or line breaks before or after the key.
  • Save the credential and ensure your workflow nodes reference that credential (n8n doesn’t auto‑update a node if you were using a different credential instance).
  • Test using the “Execute Node” button on the node that calls OpenAI. If the credential is valid, the node should return a JSON response instead of the 401 error.

 

If You’re Using an HTTP Request Node (very common in production)

 

Make sure your configuration looks like this:

// Method
POST

// URL (example for Chat Completions)
https://api.openai.com/v1/chat/completions

// Auth
None  // You handle the header manually

// Headers
Authorization: Bearer {{$credentials.openAIApiKey}}
Content-Type: application/json

// Body (JSON)
{
  "model": "gpt-4o",
  "messages": [
    { "role": "user", "content": "Hello!" }
  ]
}

If the “Authorization” header is missing or the Bearer format is wrong, OpenAI rejects it immediately.

 

Production Tips (Important)

 

  • Don’t store raw API keys in plain text in nodes. Always use n8n Credentials so they stay encrypted.
  • Test with a minimal request. If your workflow is complex, isolate the OpenAI node and test only that node to avoid confusion.
  • If you run n8n in Docker, restart the container after rotating credentials if the instance behaves oddly. Rare, but happens with long‑running processes.
  • Double‑check your OpenAI org/project ID if your company uses multiple projects. Wrong project = invalid key.

 

If you follow the steps above — fresh key, correct header, correct credentials — the “invalid API key” error disappears 100% of the time in a production n8n environment.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022