How to handle API downtime with 2 lines of code

Corentin - Jan 17 '20 - - Dev Community

In the last years, calling a third-party API has been made very straightforward. As an example, here's all that it takes to show in your app a list of planets from the famous Star Wars movies:

const axios = require("axios")

axios
  .get("https://swapi.co/api/planets")
  .then(console.log)
```



There are now thousands of APIs to do almost anything possible. But APIs are unpredictable. They work most of the time, but [it happens](https://status.box.com/incidents/45rmsv4glz45), [one day or another](https://status.sendgrid.com/incidents/0s9gnnbgv9z2), [that for an unanticipated reason](https://developers.facebook.com/status/issues/2287293591515186/), the request fails.

Debugging these errors on production is pretty tricky. You need to have a good logging habit or rely on third-party services (like Bugsnag or Sentry). This is great, but you don't really focus on API traffic here.

**What if you could make your app API-resilient?** No matters what happens on Stripe, Twilio, or any other services, your app (and your business) will remain above the fray.

At Bearer, this is what we're working on. The first version of our Agent monitors your external API calls, without impacting your network or your app performance. It does it with 2 lines of code (in Node.js).

Let's have a look:



```js
// That's all it takes to monitor external API calls
const Bearer = require('@bearer/node-agent')
Bearer.init({ secretKey: '...' })
```



Adding these two LOC to your app gives you a full overview of outbound API requests that your application is performing.

This helps you to debug all requests made from your app, in real-time:

<figure>
[![Bearer Dashboard showing API monitoring view](https://thepracticaldev.s3.amazonaws.com/i/ypd9v8771uwhhyj1aa0k.png)](https://app.bearer.sh)
<figcaption>Screenshot of my Dashboard with an overview of third-party APIs usage</figcaption>
</figure>

But the Bearer Agent does more. It also protects your app in an active way.

Let's say that requests to [the Star Wars API](https://swapi.co/) are frequently failing. This makes your app buggy, but you know that it's just some network issue with that API. The first step, to fix that issue, is to add retry logic to your app.

Here's how you could do that with [Axios](https://github.com/axios/axios):



```js
const axios = require('axios')

function getPlanets(count_requests) {
  // Max number of retries
  const max_retry = 2

  // Counter on how many requests has been performed yet
  // (will be 0, 1 or 2)
  count_requests = Number(count_requests) || 0

  // If the counter of requests is above the limit
  // of retries, throw an error.
  if (count_requests > max_retry) {
    throw Error(`Unable to make the request (total retry: ${count_requests})`)
  }

  // Make the request and return it
  return axios.get('https://swapi.co/api/planets').catch(() => {
    // If an error happens, retry the request.
    return getPlanets(count_requests + 1)
  })
}

// Make the request
getPlanets().then(console.log)
```



It's starting to look a little bit more complex...

The Bearer Agent has built-in mechanisms to retry requests automatically. So, let's see how retry would look like with Bearer enabled on your app:



```js
const Bearer = require('bearer')
Bearer.init({ secretKey: '...' })

const axios = require('axios')
axios.get('https://swapi.co/api/planets').then(console.log)
```



Looks better, no? That's because the retry logic is handled right into the Bearer Agent. But, retry is just one example of the features that the Agent brings. Our team is also adding fallback, caching, a circuit breaker and more.

If I piqued your curiosity, learn more about [API resilience & monitoring](https://www.bearer.sh) on our website.

_PS: Credit to [Annie Spratt](https://unsplash.com/@anniespratt) & [Unsplash](https://unsplash.com/photos/XMpXzzWrJ6g) for the featured image._

[![Learn more about Bearer.sh (hero image)](https://dev-to-uploads.s3.amazonaws.com/i/5vn0mz3l200z437wubax.jpg)](https://www.bearer.sh/blog?utm_source=dev.to&utm_medium=hero&utm_campaign=handle-api-downtime)
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .