HomeExplor All APIsRandom User API

Random User API

The Random User API provides access to random user data, ideal for testing applications that require user profiles.

Endpoint

https://freeapihub.onrender.com/api/v1/randomusers

Get User Data by ID

To retrieve user data by ID, you can use the following endpoint:

https://freeapihub.onrender.com/api/v1/randomusers/:userId

Replace : userId with the actual user ID you want to fetch.

Installation

To use the Random User API, you’ll need to install Axios. You can do this using either npm or pnpm:

Note: Run these commands in your project directory to install Axios.

Using npm

npm install axios

Using pnpm

pnpm add axios

Example Usage

Here’s how to fetch random user data using Axios:

import axios from "axios";
 
axios
  .get("https://freeapihub.onrender.com/api/v1/randomusers")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error("Error fetching data:", error);
  });

Here’s how to fetch random user data by userId using Axios:

import axios from "axios";
 
// Fetching user data by ID
const userId = "1a2b3c4d"; // Replace with actual user ID
 
axios
  .get(`https://freeapihub.onrender.com/api/v1/randomusers/${userId}`)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error("Error fetching user data by ID:", error);
  });

Response Format

Below is a sample JSON response when requesting random user data or user data by id:

{
    "success": true,
    "data": [
        {
            "id": "cm2ivfrdc0002fo4ch5zi0car",
            "gender": "male",
            "firstName": "John",
            "lastName": "Doe",
            "email": "john.doe@example.com",
            "username": "johndoe123",
            "password": "password123",
            "phone": "+1234567890",
            "age": 34,
            "streetNumber": 123,
            "streetName": "Main St",
            "city": "Anytown",
            "state": "California",
            "country": "USA",
            "postcode": 90210,
            "picture": "https://randomuser.me/api/portraits/men/1.jpg",
            "nationality": "American",
            "createdAt": "2024-10-21T10:26:09.061Z",
            "updatedAt": "2024-10-21T10:26:09.061Z"
        }
    ],
    "message": "All users retrieved successfully"
}

Error Handling

When using the Random User API, it’s essential to handle potential errors effectively. Below are common error codes you may encounter:

Error CodeDescription
404Resource not found
429Too many requests (rate limit)

Example Error Handling

To manage potential errors gracefully, you can use a try...catch block in your Axios requests. Here’s an example of how to implement error handling when fetching user data:

import axios from "axios";
 
async function fetchUserData() {
  try {
    const response = await axios.get("https://freeapihub.onrender.com/api/v1/randomusers");
    console.log(response.data);
  } catch (error) {
    console.error(
      "Error fetching random user data:",
      error.response?.data?.message || "An unexpected error occurred."
    );
  }
}
 
fetchUserData();

Usage Tips

  • Utilize the Random User API to generate user data for testing or development purposes.
  • Experiment with different API calls to see the variety of data returned, helping you understand the structure and attributes available.
  • For a deeper understanding of how to effectively use APIs, refer to the API Usage Guide.

Happy coding with the Random User API! 🎉