{"id":9415,"date":"2016-10-10T23:28:29","date_gmt":"2016-10-10T21:28:29","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=9415"},"modified":"2026-01-12T09:07:55","modified_gmt":"2026-01-12T09:07:55","slug":"238-way-to-react-to-api-failure","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/tutorials\/238-way-to-react-to-api-failure\/","title":{"rendered":"Way to react to API failure"},"content":{"rendered":"<p><b><a href=\"https:\/\/en.wikipedia.org\/wiki\/Application_programming_interface\">APIs<\/a><\/b> can be described as a tap delivering data instead of water, as it provides a continuous stream of information. With APIs, developers send queries to a server and get access to data or even plug web applications to an up-to-date data-source.<\/p>\n<p>In my field, International Development, <b><a href=\"http:\/\/data.worldbank.org\/\">The World Bank<\/a><\/b> is the most important source of data to study economic and social development. <b><a href=\"https:\/\/datahelpdesk.worldbank.org\/knowledgebase\/topics\/125589-developer-information\">World Bank&#8217;s API<\/a><\/b> allows developers to take advantage of open data and automation; so that everyone can create up-to-date applications, as applications can be plugged directly to the data servers, and have access to live data.<\/p>\n<p>But here is my issue, I am torn between a full automatization and a constant monitoring of an update. If I go with an automatization, there is a risk of a crash when major updates occur, such as indicators names revision, series discontinuity, etc. And if I don\u2019t use automation, that means I have to constantly monitor any data update. As a developer and maintainer of a dataviz portal on international development for the <b><a href=\"http:\/\/data.afd.fr\">French Development Agency<\/a><\/b>; I have to provide users with a continuous service.<\/p>\n<p>To provide that service and enjoy the holidays gained out of automation, I need a backup plan.<\/p>\n<h2>CONTINUOUS SERVICE: LOCAL BACKUP AND URL SWITCH<\/h2>\n<p>Hospitals have local generators that switch on in a case of a power grid crash. To allow a continuous dataviz service a switching mechanism and a local generator are required. But how this concept could be used in my case? If the API request fails, the chart will\u00a0use a local\u00a0data. The local generator will be our local JSON file, and the switch will be probably a script in javascript.<\/p>\n<p>Let\u2019s do it.<\/p>\n<h2>1ST STEP: CREATE A LOCAL JSON FILE<\/h2>\n<p>Click on the following <b><a href=\"http:\/\/api.worldbank.org\/countries\/CIV; LMC;SSA;WLD;WLD\/indicators\/EG.USE.COMM.FO.ZS?per_page=80&amp;MRV=20&amp;format=json\">link<\/a><\/b> to download the JSON file.<\/p>\n<h2>2ND STEP: A SWITCHING MECHANISM<b><a href=\"http:\/\/www.stats4dev.com\/doc\/Blog++API+and+providing+continuous+service.html#2nd-step:-a-switching-mechanism\"><br \/>\n<\/a><\/b><\/h2>\n<p>The following javascript send the initial request to the API with the method getJSON. If it succeeds; \u00a0the chart will use the requested URL data. Otherwise, local file will be selected:<\/p>\n<pre>\/\/ Request to the API\r\nvar url_API =\"http:\/\/api.worldbank.org\/countries\/CIV;\r\nLMC;SSA;WLD\/indicators\/EG.USE.COMM.FO.ZS?\r\nper_page=80&amp;MRV=20&amp;format=jsonP&amp;prefix=?\"\r\n\/\/ Local folder where the file created in step 1 is stored\r\nvar url_local =\"data\/CIV_EG_USE_COMM_FO_ZS.json\"\r\n\r\n\/\/ variable that will take the value 1 if the call is successful\r\nAPI_success =0\r\n\r\n\/\/------- Try 1st url, if succeed, API_success=1\r\n$.getJSON(url_API, function(d) {\r\nAPI_success =1; \/\/ if failure, API_succes remains=0\r\nurl_success=url_API;\r\ndocument.getElementById('bulb_success').style.color =\"#6E9FC5\";\r\n});\r\n\r\n\/\/--------- if getJSON fails give a new URL ------------\r\nsetTimeout(function() {\r\nif (API_success ==0) { \/\/ i.e. if failure\r\nurl_success = url_local\r\ndocument.getElementById('bulb_success').style.color =\"red\";\r\n};\r\n}, 200);\r\n<\/pre>\n<h2>LAST STEP: CREATE THE DATAVIZ WITH HIGHCHARTS<\/h2>\n<p>To create the interactive charts, I use Highcharts library; as It is very easy to implement and can be <b><a href=\"blog\/218-css-styling-javascript-charts\">customized with CSS<\/a><\/b>. In this example, I use <b><a href=\"http:\/\/jupyter.org\/\">IPython notebook<\/a><\/b> to write the HTML file hosting the chart (click <b><a href=\"https:\/\/s3.amazonaws.com\/highsoftpictures\/_blog\/WayToReactToAPIFailure.py\">here<\/a><\/b>)<\/p>\n<p><iframe height=\"570\" style=\"width: 100%;\" scrolling=\"no\" title=\"Way to react to API failure\" src=\"https:\/\/codepen.io\/mushigh\/embed\/jOrNpRr?height=265&#038;theme-id=light&#038;default-tab=result\" frameborder=\"no\" loading=\"lazy\" allowtransparency=\"true\" allowfullscreen=\"true\"><br \/>\n  See the Pen <a href='https:\/\/codepen.io\/mushigh\/pen\/jOrNpRr'>Way to react to API failure<\/a> by mustapha mekhatria<br \/>\n  (<a href='https:\/\/codepen.io\/mushigh'>@mushigh<\/a>) on <a href='https:\/\/codepen.io'>CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<p>To check the API connexion status, I am using a <b><a href=\"http:\/\/fontawesome.io\/icons\/\">font awesome icon<\/a><\/b>, the icon turns blue if the chart reaches the API and turns red if the chart gets the data from the local file. This way, I will know if there&#8217;s any issue and eventually update the script.\u00a0As a demonstration, let&#8217;s try API request that doesn\u2019t succeed.<\/p>\n<pre>var url_API = \"http:\/\/FAKE_API_request\/countries\/CIV;\r\nLMC;SSA;WLD\/indicators\/EG.USE.COMM.FO.ZS?\r\nper_page=80&amp;MRV=20&amp;format=jsonP&amp;prefix=?\"<\/pre>\n<p>The following chart uses this URL:<\/p>\n<pre>HTML('&lt;iframe src=\"Dataviz_with_FAKE_API_request.html\" scrolling=\"no\" \r\nframeborder=\"0\" width=100%\" height=\"475px\"&gt;&lt;\/iframe&gt;')<\/pre>\n<p><img decoding=\"async\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/s3.amazonaws.com\/highsoftpictures\/_blog\/Way+to+react+to+API+failure_font_awesome_icon.jpg\" alt=\"\" width=\"50%\" height=\"50%\" border=\"0\" \/><\/p>\n<p>As shown in the above chart, the icon turns red, following a failed API request, indicating that the chart is fetching data from the local file. The data is not automatically updated, but at least it is not broken, and I can work to adapt the script, and manually update the local data file if necessary. Now, I \u00a0have time to maintain the service and I can enjoy my holidays.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is your strategy in case of an API failure? Are you pro automatization or a constant monitoring process?<\/p>\n","protected":false},"author":37,"featured_media":10702,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[210],"tags":[1094,1031],"coauthors":[724],"class_list":["post-9415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-highcharts-core","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/9415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=9415"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/9415\/revisions"}],"predecessor-version":[{"id":29076,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/9415\/revisions\/29076"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/10702"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=9415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=9415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=9415"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=9415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}