0

I'm trying to call an external API in Firebase Functions but i always get a timeout. What can be the issue causing this?

Here is my code

exports.getCountryData = functions.https.onRequest((request, response) => {

const https = require('https');
const options = {
  hostname: "api-football-v1.p.rapidapi.com/v3",
  path: '/fixtures?next=5',
  headers: {
    "x-rapidapi-host": "api-football-v1.p.rapidapi.com/v3",
    "x-rapidapi-key": "my-api-key"
  }
};
  var req = https.get(options, (resp) => {
    let data = '';
    resp.on('data', (chunk) => { data += chunk; });
    resp.on('end', () => {
        var result = JSON.parse(data);
        console.log("Api fetched successfully");

        console.log(result);
   
        response.send({ fulfillmentText: result});
      });
    }).on("error", (err) => { console.log("Error: " + err.message); });
});
3
  • 2
    loading api-football-v1.p.rapidapi.com/v3 in a new browser tab takes forever. that's the issue Commented Jul 2, 2022 at 11:05
  • 3
    Thanks. You're right. I changed the host from api-football-v1.p.rapidapi.com/v3 to v3.football.api-sports.io and it's now working fine Commented Jul 2, 2022 at 11:44
  • Hi @tate_xy Please post the answer so that community members visiting the question, should know what solved your issue Commented Jul 4, 2022 at 7:52

2 Answers 2

1

An event-driven function may fail to successfully complete due to errors thrown in the function code itself. Some of the reasons this might happen are as follows:

  • The function contains a bug and the runtime throws an exception.
  • The function cannot reach a service endpoint, or times out while trying to reach the endpoint.
  • The function intentionally throws an exception (for example, when a parameter fails validation).
  • When functions written in Node.js return a rejected promise or pass a non-null value to a callback.

In any of the above cases, the function stops executing by default and the event is discarded. If you want to retry the function when an error occurs, you can change the default retry policy by setting the "retry on failure" property. This causes the event to be retried repeatedly for up to multiple days until the function successfully completes.

In this question, the service endpointi ‘api-football-v1.p.rapidapi.com/v3’ itself took so much time to load ( not reachable ), that was the issue. Changing the API endpoint to v3.football.api-sports.io and then calling the external API in Firebase Functions solved the issue for our user @tate_xy

Sign up to request clarification or add additional context in comments.

Comments

1

It turns out using their Rapid Api url (api-football-v1.p.rapidapi.com/v3) was was resulting in a timeout. Using a direct Api url (v3.football.api-sports.io) with their domain name in it did the trick for me.

Here is my working code.

exports.getCountryData = functions.https.onRequest((request, response) => {

const https = require('https');
const options = {
  hostname: "v3.football.api-sports.io",
  path: '/fixtures?next=5',
  headers: {
    "x-rapidapi-host": "v3.football.api-sports.io",
    "x-apisports-key": "my-api-key"
  }
};
  var req = https.get(options, (resp) => {
    let data = '';
    resp.on('data', (chunk) => { data += chunk; });
    resp.on('end', () => {
        var result = JSON.parse(data);
        console.log("Api fetched successfully");

        console.log(result);
   
        response.send({ fulfillmentText: result});
      });
    }).on("error", (err) => { console.log("Error: " + err.message); });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.