mdmb
mdmb

Reputation: 5283

Check if the API is reachable

What is the best way to tell if the API is online? I know I can check if the user has the Internet connection using navigator.onLine but how about the API?

Should I send a request to some simple endpoint and see if the data goes back to me?

Upvotes: 2

Views: 15785

Answers (2)

cslrnr
cslrnr

Reputation: 747

Try this under try catch block and log error or do continue rest of the code based on that

try {
   //api request 
} catch (e)
   // log error
   // Set flag
}

Based on the flag, do you next action item

Upvotes: 0

Sean
Sean

Reputation: 1464

I would just simple do a simple "ping" type test, if your API was https://api.example.com/auth/user I would just do a simple GET request on https://api.example.com if this returns the expected result you can assume the API is online. This isn't the best test because there still could be a problem with the API but the concept of it being on line is still checked.

If the service you are using has a status page 9/10 the API will be on there. You could use this page to your advantage by scraping the page and checking the status of the API. Say your using the Bitbucket API you could GET this page status.bitbucket.org and then check that the API state is OPERATIONAL.

Upvotes: 4

Related Questions