Published on November 01, 2022

Get User Current Region With Geolocation API

Melvin Liu

By Melvin Liu

Software Design | Engineering | Architecture

2 min read

share this post


A few weeks ago, one of my clients required me to implement geolocation detection on their site. They want to show different content and store user form submission to a different API endpoint depending on their location (ex: if a user is currently in the Singapore region, the site will show Singapore content only & POST the user form submission data to the Singapore endpoint). In this article I'll show different cases of handling this kind of feature.


Officially modern browsers already exposed their own native official geolocation feature (read this, BUT ,since it is quite a hassle to set it up in the project, I found a great alternative which is that only require an API call



ℹ️ If you open the home URL it will show 502 Bad Gateway, insted open will result in this :

  "latitude": "coordinate",
  "longitude": "coordinate",
  "IPv4":"IP Address",


Since I will be using Javascript with axios, here is the function that I wrote to GET the endpoint result

// getLocation.js
import axios from "axios";

export const getLocation = async () => {
  const API_URL = ``;
  const response = await axios.get(API_URL);


To check whether the function/API works, you can utilize your favorite VPN 😏. You can manipulate it into a standalone library or include it in the component/customize it as a react hook (maybe useLocation hooks or something)

If you have any question feel free to ask in the comment section below ⬇️