#API ERROR 400

11 messages · Page 1 of 1 (latest)

undone cedar
#

hi, i have an error when trying to generate an image with a prompt using the API for my flutter mobile app :
here is my code :


class AIHandler {
  final openAI = OpenAI.instance.build(
    token: 'HERE IS MY API KEY',
    baseOption: HttpSetup(receiveTimeout: 20000),
    isLogger: true,
  );

  Future<ImageUrl?> generateImage(String prompt) async {
    try {
      final request = GenerateImage(
        prompt,
        1,
        size: '1024x1024',
      );
      final response = await openAI.generateImage(request);
      final imageDataList = response?.data;
      final imageData = imageDataList?.last;
      return imageData?.url;
    } catch (e) {
      print(e);
    }
    return null;
  }

  void dispose() {
    openAI.close();
  }
}

typedef ImageUrl = String;```

here is the error :
```I/flutter (16498): string request
I/flutter (16498): request body :{prompt: Boy with a guitar, n: 1, size: 1024x1024, response_format: url, user: }
I/flutter (16498): status code :400
I/flutter (16498): message :Http status error [400]```

can someone help me ?
cold violet
#

Verify your API key: Make sure the API key you're using is valid and has the necessary permissions to access the API.

Double-check the API documentation: Make sure the parameters you're sending in your request match the required format and types specified in the API documentation. For OpenAI's image generation API, you can refer to the official documentation here: https://platform.openai.com/docs/guides/images
Try a different prompt: Test with a different prompt to see if the issue is specific to the current prompt you're using.
Test using a REST client: Test the API request using a REST client like Postman to ensure your request structure is correct. If you can successfully generate an image using a REST client, it will help you narrow down the issue to the Flutter SDK or your code.
Check the SDK documentation or source code: Since you're using a custom Flutter SDK, it's possible that there's an issue or limitation within the SDK itself. Check the SDK's documentation or source code to see if there are any known issues or specific requirements for using it with the OpenAI API.

undone cedar
formal hazel
#

Hi @undone cedar
Upon receiving an error you still want to inspect the response body, there might be more specific information about the error.

An error 400 means that your request is likely invalid for one reason or another.

undone cedar
#

how ? I don’t have any infos about this error

#

and my prompt seems correct in my code

#

all is up to date

undone cedar
#

👀

kindred olive
#

same i have issue with my api and i don't know how to solve it

formal hazel
#

Does your http response contain a body?

undone cedar
#

No