Curl example gives 400 error with new API key

I am trying out the API, and using the curl example, from the docs

curl https://api.openai.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "dall-e-3",
    "prompt": "a white siamese cat",
    "n": 1,
    "size": "1024x1024"
  }'

I’m using a brand new API token, and getting the following, pretty non-descript, error

{
  "error": {
    "message": "Error in request. Please check your input.",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

Where are you trying to run it?

Line continuations and escaping for Windows:

curl https://api.openai.com/v1/images/generations ^
  -H "Content-Type: application/json" ^
  -H "Authorization: Bearer %OPENAI_API_KEY%" ^
  -d "{
    \"model\": \"dall-e-3\",
    \"prompt\": \"A cute baby sea otter\",
    \"n\": 1,
    \"size\": \"1024x1024\"
  }"

Or better: have some of the P in API - programming, in Python. A bit better dall-e-3 demo.

import base64
import requests
import os
import time; starttime = time.time()
''' DALL-E API for image creation - URL json object returned: 
{"created": 1744839275, "data": [{"url": "https://..."}, {"url": "https://..."}]}
'''

prompt = """
Photo Image: A monkey eating pasta with red sauce in a human restaurant.
""".replace('\n', ' ').strip()

headers = {"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}"}

request = {
    "prompt": prompt,        # DALL-E 3: max 4000 characters // DALL-E 2: max 1000
    "model": "dall-e-3",     # Defaults to dall-e-2 // optional
    "quality": "standard",   # or "hd" // no dall-e-2
    "size": "1024x1024",     # 1024x1024, 1792x1024, or 1024x1792 // 1792: no dall-e-2
    "response_format": "url",  # or b64_json for data
    "style": "vivid",        # or "natural", which is poor  // no dall-e-2
    "user": "my_id1234",     # your customer tracker sent to OpenAI
}
response = requests.post(
    "https://api.openai.com/v1/images/generations",
    headers=headers,
    json=request,
)
try:
    response.raise_for_status()
    data = response.json()
    # Print each URL line-by-line for n>1 models
    print("\nURLs:")
    for item in data.get('data', []):
        url = item.get('url')
        if url:
            print(url)
except requests.HTTPError as err:
    print(f"HTTP error: {err}")
    print(response.text)
print(str(time.time()-starttime) + " seconds")
input("\nCopy the url - links aren't saved!\nLinks good for an hour.")

looks like its due to having no credit on my account. would be helpful if the error message was of some help :roll_eyes: