Author Photo
Christopher Unum I am a software Developer, I write web development tutorials, I also mentor aspiring web developers. s

how to retrieve data from an api using fetch api in javascript

User Name

User Phone

User Address

User age

If you are warming up to become a front-end developer, then knowing how to extract data from an API is vital. APIs are developed by back-end developers and can be accessed by a HTTP request. 

In this tutorial, we will be connecting to a free, open-source API obtained from that generates random user data using Fetch API.


Basic knowledge of Html, Css and JavaScript (ES 6) is required to fully understand the concepts in this tutorial

What is an API

API stands for Application Programming Interface. An API allows two applications to communicate with each other. 

One example of an API usage in our everyday lives is weather data found on platforms like Google Search or Apple's Weather App. Google don't generate their weather data, they rely on third-party APIs to obtain weather data for search results.

An API provides a Layer of Security, your data is never fully exposed to the server and Also the API developers decide which information are made available, this limits access to only required information rather than full control.

Randomuser API

As mentioned earlier, we will be connecting to the randomuser API to obtain random user data. After we send an HTTP request to the API endpoint, the API will provide us with data containing the user's information which would be obtained as a JSON object. Below is a sample of a JSON object obtained from

    "results": [
        "name": {
        "location": {
          "street""9278 new road",
          "coordinates": {
          "timezone": {
        "login": {
        "dob": {
        "registered": {
        "id": {
        "picture": {
    "info": {


We need to create a profile page that displays the information obtained from the API.  We will be obtaining the user's name, image, phone, address and age from the randomuser API and rendering it in our HTML code shown below.

<div class="profile-out">
  <div class="profile-in">
    <div class="profile">
      <h3 class="user-name">User Name</h3>
      <div class="user-photo">
        <a href=""
          ><img src=""
      <p class="user-phone">User Phone</p>
      <p class="user-address">User Address</p>
      <p class="user-age">User age</p>


Adding styles to our HTML code

.profile-out {
  margin0 auto;
  padding15px 0;
.profile-in {
  margin0 auto;
  padding15px 0;

.profile {
  margin0 auto;
.user-photo {
  margin0 auto;
.user-photo img {
  border3px solid #47dbe0;

Fetch API

Fetch API is a JavaScript API that allows you to make network requests, it is similar to XMLHttpRequest and it is promise-based. Compared to XMLHttpRequest, Fetch API is a simple and clean API that fetches resources from a server.

Using Fetch API is straight forward

  .then((response) => {
    // handle response data
  .catch((err) => {
    // handle errors

The path to access the resources is passed as a parameter in the fetch() method, then the response is accessed using the then() method and finally, we can catch any errors and handle them appropriately.


Now that we understand the Fetch API, we can now use it in our JavaScript code

const displayUserName = ({ title, last, first }) => {
  ).textContent = `${title} ${first} ${last}`;

const displayUserPhoto = ({ large }) => {
    .getElementsByTagName("img")[0].src = large;

const displayUserPhone = ({ phone, cell }) => {
  ).textContent = `${phone} / ${cell}`;

const displayUserAddress = ({ street, city, state }) => {
  const { name, number } = street;
  ).textContent = `${number} ${name}${city}${state}`;

const displayUserAge = ({ age }) => {
  document.querySelector(".user-age").textContent = `${age} years old`;

const displayUserInfo = (data) => {
  if (!data) return;

  //using object destructuring
  const { results } = data;
  const [profile] = results;

  const { name } = profile;

  const { picture } = profile;


  const { location } = profile;

  const { dob } = profile;

const getUserInfo = () => {
  const api = "";

  // make API call here
    .then((response) => response.json())
    .then((data) => {
    .catch((error) => alert("error getting API resources"));

We used a lot of Object Destructuring to obtain data from the response. If you are new to ES 6, you can read about it here. In our JavaScript code, the getUserInfo() method uses Fetch API to obtain data from, then the data is then passed on to the displayUserInfo(data) to render the information on our page. 


We've come to the end of this tutorial. The best way to learn is to practice and practice some more, so find some free APIs and try using Fetch API to extract data from them.