Chainlink Functions Error in SendRequest to an API

63 Views Asked by At

I'm trying a basic integration of AI into Smart Contracts using Chainlink Functions and I got an error after sending the request, resulting in empty response, even though I've been charged for the API call.

Error during functions execution: Error: {"error":true,"message":"AbortError: The signal has been aborted"}

I'm making a text-to-image call. I thought it could be a timeout execution issue but I do not get how to address it anyway. Any help?

Thanks

This is the source script used to send the request:

const _prompt = args[0];

const postData = {
  model:"dall-e-3",
  prompt: _prompt,
  size:"1024x1024",
  n:1,
};

const openAIResponse = await Functions.makeHttpRequest({
  url: "https://api.openai.com/v1/images/generations",
  method: "POST",
  headers: {
    Authorization: `Bearer ${secrets.apiKey}`,
    "Content-Type": "application/json",
  },
  data: postData
});

if (openAIResponse.error) {
  throw new Error(JSON.stringify(openAIResponse));
}

const result = openAIResponse.data.data[0].url;

console.log(result);
return Functions.encodeString(result);
2

There are 2 best solutions below

2
On

"The signal has been aborted" is a timeout error on the Functions side. The default value for the makeHttpRequest is 3000 ms.

At the following docs URL you can see how to manually increase it. In this case, that would be:

const openAIResponse = await Functions.makeHttpRequest({
  url: "https://api.openai.com/v1/images/generations",
  method: "POST",
  headers: {
    Authorization: `Bearer ${secrets.apiKey}`,
    "Content-Type": "application/json",
  },
  data: postData,
  timeout: 9000
});

Keep in mind that there are general service limits when using Functions, you can check them out in the official documentation as well. At the moment the duration of a single HTTP request must not exceed 9 seconds.

0
On

Chainlink community node operator (oracle) here.

Using Chainlink Direct Requests (also sometimes known as 'Any API') instead of Functions, you can get around this 9-second HTTP request timeout limitation, as Direct Requests are more flexible in terms of their service limitations (request timeouts, payload size, etc.).

The obvious downside of using a Direct Request oracle is that your request will only be served by a single oracle, which introduces decentralized risk (you'll need to use a reputable oracle company, who hosts your requests on a redundant, scalable, distributed infrastructure), but if you're able to find an oracle company that you trust, this may not be an issue for you.

The recommended way to find a Chainlink oracle is to join Chainlink Official Discord server and post your requirement to the #operator-requests channel. Many reputable oracles will also provide code examples of how to do this (at LinkWell Nodes you can find it on our website), and can easily make your POST request for you with whatever timeout you like.

Hopefully that helps to at least point you in the right direction - let me know if you have any questions.