Faraday 401 Unauthorized Ruby

I am a fresh ruby programmer and I am trying to use OpenAI API for a school project. I keep receiving this error when trying to run my ruby code. I used curl to see if it was something to do with api key and curl worked fine I am not sure what my next steps should be. Here is my code:

require 'openai'

client = OpenAI::Client.new(logerrors: true, api_key: "API_KEY")

response = client.chat(
  parameters: {
    model: "gpt-4o",
    messages: [{role: "user", content: "Hello!"}]
  }
)


puts response.dig("choices", 0, "message", "content")

I apologize if this is not a lot of information but any help is appreciated.

1 Like

Welcome to the OpenAI community @reyescaleb320

Here’s what a 401 means when thrown by OpenAI API:

Code Overview
401 - Invalid Authentication Cause: Invalid Authentication
Solution: Ensure the correct API key and requesting organization are being used.
401 - Incorrect API key provided Cause: The requesting API key is not correct.
Solution: Ensure the API key used is correct, clear your browser cache, or generate a new one.
401 - You must be a member of an organization to use the API Cause: Your account is not part of an organization.
Solution: Contact us to get added to a new organization or ask your organization manager to invite you.

Use the correct methods for the independently-developed library.

Read here, and you can see configuring a client with “access token”. With the author also using their own odd environment variables.

1 Like

I’ve implemented the initializer and changed the env from api_key to access_token. I’m still getting the same error. I even tried to just hardcode the api key into the code and it still is giving me a unauthorized error.

The library doesn’t seem to get from any env variable automatically.
Documentation reports using an an openai.rb initializer file with its more permanent setting.

You can find this in the link, and use OPENAI_API_KEY instead (like anything else would).

…
config.access_token = ENV.fetch(“OPENAI_ACCESS_TOKEN”)

Correct. I am using the openai.rb file with this inside:

require ‘dotenv/load’

OpenAI.configure do |config|
config.access_token = ENV.fetch(“OPENAI_API_KEY”)
config.log_errors = true
end

In my run_openai.rb I have confirmed that it does point to the API key

The best I can think of is to hack into the library and capture the API request right before it is sent, for diagnosis.

The OpenAI key has recently grown in length, so the library (and other utilities) may not account for that unannounced and unreasoned change.

How would I go about doing that?

If you don’t know how to work with the library’s Ruby code and identify where it creates a request as a variable, to then log it, and are uncertain if you are even using this library right, perhaps the “community” that might know is some Ruby developer forum, or the “issues” on github for it. I could probably install Ruby, make it work or not, debug, but that is called “consulting”, not “fun times with forum pals useful to all”.

Maybe it is better to be a “fresh” Python or node.js programmer, as there you have official libraries that are maintained with examples.