Open In App

Working with APIs in JavaScript

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
12 Likes
Like
Report

An API is simply a medium to fetch or send data between interfaces. Let’s say you want to make an application that provides the user with some real-time data fetched from the server or maybe even allows you to modify or add data to some other endpoint. This is made possible by the API or the Application Programming Interface.

We will use a simple public API that requires no authentication and allows you to fetch some data by querying the API with GET requests.

https://randomuser.me/ is a website that provides dummy data for random users that we can easily work with. We can get the response by requesting https://randomuser.me/api/. The JSON response that we receive is in the following format.

{
    "results": [
        {
            "gender": "female",
            "name": {
                "title": "Miss",
                "first": "Nina",
                "last": "Simmmons"
            },
            "location": {
                "street": {
                    "number": 970,
                    "name": "Eason Rd"
                },
                "city": "Fullerton",
                "state": "Wyoming",
                "country": "United States",
                "postcode": 57089,
                "coordinates": {
                    "latitude": "83.1807",
                    "longitude": "104.7170"
                },
                "timezone": {
                    "offset": "+8:00",
                    "description": 
        "Beijing, Perth, Singapore, Hong Kong"
                }
            },
            "email": "nina.simmmons@example.com",
            "login": {
                "uuid": "bd0d135f-84df-4102-aa4f-5baaa41baf5c",
                "username": "yellowfrog722",
                "password": "dawg",
                "salt": "q28gdiyN",
                "md5": "291987daea22bb91775226574925b271",
                "sha1": "a0463a26ea5c2ff4f3ad498fd01c5994926e5021",
                "sha256": 
"6583eb74ca08bfac50b3b29aa52c9f02ea5d9d017fef0e5a5a6fae4f5225f928"
            },
            "dob": {
                "date": "1980-11-01T23:10:05.403Z",
                "age": 40
            },
            "registered": {
                "date": "2013-04-02T02:26:52.904Z",
                "age": 7
            },
            "phone": "(216)-693-7015",
            "cell": "(501)-534-9413",
            "id": {
                "name": "SSN",
                "value": "847-09-2973"
            },
            "picture": {
                "large": 
"https://randomuser.me/api/portraits/women/60.jpg",
                "medium": 
"https://randomuser.me/api/portraits/med/women/60.jpg",
                "thumbnail": 
"https://randomuser.me/api/portraits/thumb/women/60.jpg"
            },
            "nat": "US"
        }
    ],
    "info": {
        "seed": "82a8d8d4a996ba17",
        "results": 1,
        "page": 1,
        "version": "1.3"
    }
}

Next, you need to have an HTML file for the frontend where you want to display the retrieved data. 

We can use either the “div” tag (block-level) or the “span” tag (inline-level), which will act as a placeholder for the information. Using the “id” attribute, we can get the required “div/span” container where we want to place the information.

HTML
CSS JavaScript

The script tag will contain the code that will make the API request and handle the response. This needs to be placed within the body tag or as a separate file.

We use the async/await function that basically ensures that the data is displayed even after the page is loaded.

You can use the console.log(…) method to check if the user is retrieving the correct piece of information. The output for the same can be seen by opening the console window in your web browser (Right Click -> Inspect -> Console or Ctrl+Shift+J in Chrome/Edge).

Output:

Random user data

Want to explore the APIs more and dive deeper into them, Refer to Public APIs which have a vast collection of publicly available APIs to fuel your API exploration journey.

To test an API for the type of response it gives Postman is an amazing application that will fulfill all your needs. You can use Postman API development article to get an insight into, how to use it. Another alternative is Postman APIs which will help you do the same task on the browser itself.



Next Article
Article Tags :

Similar Reads