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)
Enter fullscreen mode Exit fullscreen mode

There are now thousands of APIs to do almost anything possible. But APIs are unpredictable. They work most of the time, but it happens, one day or another, that for an unanticipated reason, 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:

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

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:

Bearer Dashboard showing API monitoring view

Screenshot of my Dashboard with an overview of third-party APIs usage

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 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:

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)
Enter fullscreen mode Exit fullscreen mode

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:

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

const axios = require('axios')
axios.get('https://swapi.co/api/planets').then(console.log)
Enter fullscreen mode Exit fullscreen mode

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 on our website.

PS: Credit to Annie Spratt & Unsplash for the featured image.

Learn more about Bearer.sh (hero image)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player