Can't use the API in commandline

I tried following the OpenAI documentation to make a request and used the code they provided, however, after adding my API key and copying the code into the command prompt, I got a slew of errors:

C:\Users\adam>curl https//api.openai.com/v1/chat/completions
{
“error”: {
“message”: “You didn’t provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you’re accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https//platform.openai. [can’t include links] com/account/api-keys.”,
“type”: “invalid_request_error”,
“param”: null,
“code”: null
}
}
curl: (3) URL rejected: Bad hostname

C:\Users\adam> -H “Content-Type: application/json”
‘-H’ is not recognized as an internal or external command,
operable program or batch file.

C:\Users\adam> -H “Authorization: Bearer sk-secret sauce”
‘-H’ is not recognized as an internal or external command,
operable program or batch file.

C:\Users\adam> -d '{
‘-d’ is not recognized as an internal or external command,
operable program or batch file.

C:\Users\adam> “model”: “gpt-3.5-turbo”,
‘“model”:’ is not recognized as an internal or external command,
operable program or batch file.

C:\Users\adam> “messages”: [{“role”: “user”, “content”: “Say this is a test!”}],
‘“messages”:’ is not recognized as an internal or external command,
operable program or batch file.

C:\Users\adam> “temperature”: 0.7
‘“temperature”:’ is not recognized as an internal or external command,
operable program or batch file.

You’re not sending the needed into like API key, etc…

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Who won the world series in 2020?"
      },
      {
        "role": "assistant",
        "content": "The Los Angeles Dodgers won the World Series in 2020."
      },
      {
        "role": "user",
        "content": "Where was it played?"
      }
    ]
  }'

You can find more in the Chat Completion docs at OpenAI…

For cURL, you need everything on one line…

Hope this helps!

But how can I not be sending the needed info when I’m using the code provided by the OpenAI documentation? I replace $OPENAI_API_KEY with my sk API key, but otherwise I leave the code as-is.

The document you provided uses the same recommended code I used in my request. It doesn’t explain the issue I’m having.

Could you or someone else please just tell me in clear terms what is going wrong and how to fix it?

Sounds like you’re copying and pasting into the Command Prompt window and it’s adding extra line breaks.

When pasting a multiline command like the cURL command into the Windows Command Prompt, it’s essential to ensure that the entire command is entered as a single line or properly handled for multiline input. The errors you’re encountering are because the Command Prompt is interpreting each line as a separate command.

Here’s a step-by-step guide to correctly paste and execute your cURL command:

Prepare the Command: Before pasting it into the Command Prompt, you should edit the command in a text editor (like Notepad). Make sure the entire cURL command is on one line or properly formatted for Windows.

This includes:

  • Replacing $OPENAI_API_KEY with your actual OpenAI API key.
  • Ensuring there are no line breaks within the command.
  • Escaping any double quotes within the JSON body.

Command Format: A correctly formatted command for Windows might look something like this (replace YOUR_API_KEY with your actual key):

curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer YOUR_API_KEY" -d "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"}, {\"role\": \"assistant\", \"content\": \"The Los Angeles Dodgers won the World Series in 2020.\"}, {\"role\": \"user\", \"content\": \"Where was it played?\"}]}"

  1. Copy and Paste the Command: Once you have the command properly formatted in a text editor, copy the entire command.
  2. Execute in Command Prompt: Open Command Prompt, right-click to paste the entire command, and then press Enter to execute it.

By following these steps, the cURL command should execute without the issues you’re experiencing. Ensure that your internet connection is stable and that the API key is correctly entered to avoid authorization errors.

1 Like

That worked perfectly. This is honestly the best community forum experience I’ve ever had in 30 years online. Thank you for being clear and to the point.

1 Like

No problem.

Hope you stick around. We’ve got a great community growing here.