How to get server location. No API Key Required

This post was created to demonstrate how easy it is to grab the server location, as the only other one I have seen is by registering through a free website and using an APIKey.

My method is much more condensed and simpler, and doesn’t utilize an API key.

local suc, getIp = pcall(function()
	return game.HttpService:GetAsync("https://ipconfig.io/json")
end)

local serverIP

if getIp then
	local ipData = game.HttpService:JSONDecode(getIp)
	serverIP = ipData["ip"]
end

-- now that we have the serverip, we can parse it and get the rest of the data
local Success, ServerData = pcall(function()
	return game.HttpService:GetAsync("https://ipapi.co/"..serverIP.."/json/") 
end)


if ServerData then
	ServerData = game.HttpService:JSONDecode(ServerData)
	print(ServerData["region"]..", "..ServerData["country_code"])
end

It’s a two step process involving getting the server’s IP address and then using a pinpoint api to grab the server region. Other websites I’ve seen do not give the exact server location, sometimes specifying that an Australian server is actually an American server, for this case I decided to use https://ipapi.co/.

If you have any suggestions as to how this can be made better, please comment them below.

5 Likes

This isn’t a tutorial as a tutorial should be long and detailed. And you aren’t really explaining what you are doing in your code. It would be better to make this a #resources:community-resources. Anyways, good post. I like how you did this.

(If your wondering how to make this long then here is some refrence:

Section 1: Getting the data

local suc, getIp = pcall(function()
	return game.HttpService:GetAsync("https://ipconfig.io/json")
end)
  • Here we use a pcall function for the sole purpose of handeling errors and so our entire script doesn’t break.
  • Then we use HTTPService and the URL https://ipconfig.io/json to get the server’s ip.

Section 2: Getting the location

The other code snippet and it’s details.

Bla bla bla.

2 Likes

Thanks for the feedback, I’ll most likely restructure the post, and include the final product at the end so people can just copy and paste it.

1 Like

What is the point of using two APIs, one already gives you an abundance of info.

1 Like

Great question. The point of using two APIs is that the first one is to get the actual IP address. The first API doesn’t give the correct server location, which is why a second API is utilized to pinpoint the exact server location. You may ask why we need the server IP in the first place, and the reason for that is https://ipapi.co requires an IP in the first place to be used.