Request failed with status code 400

I think I had the same issue. Does your API key end with the letter ‘u’, perhaps?

My environment variable (process.env.OPENAI_API_KEY) was not parsed correctly, or so I thought. Turns out that during copy-pasting the api key a hidden character was added to the string, probably caused by an unicode encoding issue somewhere. Yes, that’s a thing.

This hidden character leads to an error 400 because the request contains invalid characters, but only when it gets read from the .env file.

I fixed it by generating a new key.

I was using VS Code + WSL + Windows Terminal.

1 Like