Blog posts tagged with 'axios'

Learning to use APIs with Wikipedia



API stands for “application programming interface”.

When you send a request to an endpoint, its API will interpret your request and then perform the action you have specified.

In this scenario, “endpoint” is the URL which we use to make requests.


I will be using the MediaWiki API in order to demonstrate how we can request information from English Wikipedia:

The endpoint we will send our requests to will be:



How do I specify what information I want?


Let’s say we want to get the first few lines from the Wikipedia page about Amazon Alexa.

In order to tell the API what we want, we need to add some parameters to the endpoint.


We start the string of parameters with a question mark.

Then, if you have multiple parameters, connect them using the “&” symbol.


Here is a snippet of JavaScript code which makes our request slightly easier to read and construct.

const myPageTitle = “Amazon_Alexa”;

const endpoint = ""

const params = “?action=query” // query is one possible action.

+ “&prop=extracts” // “extracts” is the name of an extension used by many wikis.

+ “&exsentences=3” // this lets you get the first 3 sentences from a page.

+ “&exlimit=1”⠀

+ “&titles=” + myPageTitle// this lets you specify which page you want information from.

+ “&explaintext=1” // means “extracts plain text”, which is human-friendly for reading.

+ “&format=json” // the data will be returned in JSON format

+ “&formatversion=2” // the JSON will be easier to navigate using index notation

+ “&origin=*”; // prevents a CORS error

Now we have a complete URL which we can use to make our request:

const alexaWikiUrl = endpoint + params;⠀

console.log(alexaWikiUrl); // output:*

Try visiting the link in your browser!

You will be presented with the information which would be returned if you were to make the request using code.


In order to make API calls using your Alexa skill, you need to install a package which lets you do so.

There are many choices, but I recommend either node fetch or Axios. Node fetch appears to be more commonly used, but Axios is easier for me personally.

Here is an example of what a fetch request might look like using node fetch:

const wikiEndpoint = '';

const wikiParams = "?action=query"

+ "&prop=extracts"

+ "&exsentences=3"

+ "&exlimit=1"

+ "&titles=" + "Amazon_Alexa"

+ "&explaintext=1"

+ "&format=json"

+ "&formatversion=2"

+ "&origin=*";

const myUrl = wikiEndpoint + wikiParams;



async function getData(url){

let res = await fetch(url);

let data = res.json();

return data;



getData(myUrl).then(data => {



If you were to use Axios, it might look something like this:

async function getWikiData(){

const wikiEndpoint = '';

const wikiParams = "?action=query"

+ "&prop=extracts"

+ "&exlimit=1"

+ "&exsentences=3"

+ "&titles=" + "Amazon_Alexa"

+ "&explaintext=1"

+ "&format=json"

+ "&formatversion=2"

+ "&origin=*";

const wikiLink = wikiEndpoint + wikiParams;


var wikiConfig = {

timeout: 6500


async function getJsonResponse(url, config){

const res = await axios.get(url, config);



return getJsonResponse(wikiLink, wikiConfig).then((result) => {

return result;

}).catch((error) => {

return null;



const wikiData = await getWikiData();

const wikiOutput = wikiData.query.pages[0].extract;


In both cases, the first 3 sentences from will be logged.

The code in the node fetch example should work in your browser console.

If you want to test it out, try using this code and experimenting by changing some parameters.

Axios isn’t so easy to test, but the provided example works when implemented correctly, and should be able to serve as a good foundation.

Useful resources