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.

10 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.

3 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.

1 Like

Dude, this has saved me hours of research and trail of error. This is by the far the simplest and easiest solution I have seen on roblox without having to deal with API keys.

I have converted this code into a nice lil module script that can be easily ported around to all my games!

Thank you!

2 Likes

This is a great way of achieving what is needed. But be wary as it can only handle so many requests on that endpoint.

60k concurrent players example all sending a request to this endpoint would be expensive and most likely cause some bugs / rate limits.

Other developers like @ffrostfall has had to actually create a reliable external server to achieve the same. Which obviously means they endure personal costs to get a feature Roblox could quite possibly implement easily as we connect to their servers anyway.

Roblox should integrate some feature that allows us to get the exact region of the user, as some locales using the localisation method can mix up, thinking some EU countries are US, etc.

1 Like

My module actually only fetches the data once and then stores the table in a cache, and then whenever a user requests the data, it is just grabbed from the cache. Would this work out well?

Would this still be a concern for 1 fetch per server?

(Maybe for something like 1k to 5k players)

If you’re doing it one fetch per server, you should be alright, considering it may not hit the limit. But depending on other factors…

It varies due to what their respective rate limit would be, like x number of requests in x number of time - because new servers could be created frequently depending on player count, how many players can go in each server and many other factors.

You could read the documentation and see if you could almost implement a threshold which matches their rate limit. So it’d somehow track this.

On the other hand, caching the request once per server is a very good idea.

Probably would be ideal for 1k concurrent players.

1 Like