Hi everyone,
I'm trying to send a POST request to the OpenAI API from a MetaTrader 5 script using WebRequest , but I'm getting this response:
"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.com/account/api-keys."
The request seems to go through (WebRequest returns 200), but the API says the key is missing. I've triple-checked the key and the header format.
Has anyone successfully connected to OpenAI's API from MT5? Is there anything specific about how MT5 handles headers that could be causing this?
Any help would be greatly appreciated!
string Headers = "Content-Type: application/json\r\n" + "Authorization: Bearer " + APIKey + "\r\n";
Your header looks correct. I've been using deep seek api the past week and the header format is the same in mql5 and it's been working.
Hi Connor,
Thanks for replying. Indeed, I am also using the header with:
string Headers = "Content-Type: application/json\r\n" + "Authorization: Bearer " + APIKey + "\r\n";
But I still get the message:
"You didn't provide an API key..."
I'm trying to send a POST request to the OpenAI API from a MetaTrader 5 script using WebRequest , but I'm getting this response:
"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.com/account/api-keys."
The request seems to go through (WebRequest returns 200), but the API says the key is missing. I've triple-checked the key and the header format.
Has anyone successfully connected to OpenAI's API from MT5? Is there anything specific about how MT5 handles headers that could be causing this?
Your WebRequest call is incorrect. The function provides 2 variants:
int WebRequest(const string method, const string url, const string cookie, const string referer, int timeout, const char &data[], int size, char &result[], string &response)
int WebRequest(const string method, const string url, const string headers, int timeout, const char &data[], char &result[], string &response)
You can choose either one of them, but you mixed their prototypes in the call, hence no headers have been passed to the service - the string was interpreted as cookies.
Your WebRequest call is incorrect. The function provides 2 variants:
You can choose either one of them, but you mixed their prototypes in the call, hence no headers have been passed to the service - the string was interpreted as cookies.
Hi again,
Thanks @Stanislav for the clarification on WebRequest overloads — that was indeed part of the problem in the original post. I corrected the function call to use the proper overload that includes the custom headers, like this:
int status = WebRequest( "POST", endpoint, httpHeaders, // now correctly passed as headers, not cookies timeout, payload, responseBuffer, responseHeaders );
Now the request reaches the OpenAI API, but I'm facing a new issue. I consistently receive a 400 Bad Request response, with the following message:
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON...)"
Example of my code (simplified):
string requestBody = "{\"model\":\"gpt-3.5-turbo\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello!\"}],\"max_tokens\":50}"; uchar payload[]; int len = StringToCharArray(requestBody, payload, 0, CP_UTF8); if(len > 0 && payload[len - 1] == 0) len--; string headers = "Content-Type: application/json; charset=utf-8\r\n" + "Authorization: Bearer " + apiKey + "\r\n"; int status = WebRequest( "POST", "https://api.openai.com/v1/chat/completions", headers, 10000, payload, responseBuffer, responseHeaders );
Despite all these precautions, OpenAI keeps returning the same error, indicating that the JSON was not correctly received.
Has anyone managed to successfully send JSON to OpenAI from MQL5 using WebRequest ? I’d appreciate a working example or any insight into what MT5 might be doing to the payload under the hood.
Thanks again for any suggestions!
Thanks @Stanislav for the clarification on WebRequest overloads — that was indeed part of the problem in the original post. I corrected the function call to use the proper overload that includes the custom headers, like this:
Now the request reaches the OpenAI API, but I'm facing a new issue. I consistently receive a 400 Bad Request response, with the following message:
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON...)"
Example of my code (simplified):
Despite all these precautions, OpenAI keeps returning the same error, indicating that the JSON was not correctly received.
Has anyone managed to successfully send JSON to OpenAI from MQL5 using WebRequest ? I’d appreciate a working example or any insight into what MT5 might be doing to the payload under the hood.
You're doing it wrong again, this time with StringToCharArray function - input agruments are incorrect, and decrementing len variable is useless. Look at HTTPRequest::POST method (from the algotrading book) as an example:
class HTTPRequest { ... int POST(const string address, const string payload, uchar &result[], string &response, const string custom_headers = NULL) { uchar bytes[]; const int n = StringToCharArray(payload, bytes, 0, -1, CP_UTF8); ArrayResize(bytes, n - 1); // remove terminal zero return request("POST", address, custom_headers, bytes, result, response); } ... };PS. Please, don't use ChatGPT for programming!

- www.mql5.com
You're doing it wrong again, this time with StringToCharArray function - input agruments are incorrect, and decrementing len variable is useless. Look at HTTPRequest::POST method (from the algotrading book) as an example:
PS. Please, don't use ChatGPT for programming!Thank you very much, fixed!
After many attempts and errors using WebRequest() to connect to the OpenAI API from an MQL5 script, I finally managed to solve the problems. I share here what I was doing wrong and how to fix it, as it may help others.
Problem 1: Incorrect use of WebRequest overloading:
int status = WebRequest( "POST", url, headers, "", timeout, payload, len, responseBuffer, cookies );
I was generating the message body like this:
char payload[]; int len = StringToCharArray(json, payload, 0, StringLen(json), CP_UTF8); ArrayResize(payload, len + 1); payload[len] = '\0';

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone,
I'm trying to send a POST request to the OpenAI API from a MetaTrader 5 script using WebRequest , but I'm getting this response:
"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.com/account/api-keys."
The request seems to go through (WebRequest returns 200), but the API says the key is missing. I've triple-checked the key and the header format.
Has anyone successfully connected to OpenAI's API from MT5? Is there anything specific about how MT5 handles headers that could be causing this?
Any help would be greatly appreciated!