Case study · API development

Building an IP geolocation API in Go and CodeIgniter 4

An IP geolocation service turns an IP address into a country, region, city, coordinates and time zone. I built one as a Go API on AWS Lambda and a CodeIgniter 4 REST app, both answering lookups from the IP2Location LITE DB11 and DB-IP datasets in MySQL: more than 8 million IPv4 ranges in total.

TypeIP geolocation API
StackGo · CodeIgniter 4 · MySQL
HostingAWS Lambda
DataIP2Location LITE DB11 · DB-IP
The problem

One lookup, several datasets

Free IP geolocation datasets differ in what they cover. IP2Location LITE DB11 includes ZIP codes and time zones, while DB-IP city data gives region, city and coordinates. Each one ships as a CSV file of several hundred megabytes, which no application should have to load or parse itself.

The goal was a single, simple endpoint that could answer a lookup from any of the datasets and return clean JSON, so other applications could use it without knowing how the data is stored.

What I built

Two ways to serve the same lookups

  • Data import: each dataset loaded into its own MySQL table, one row per IP range, with the start and end of each block. IP2Location LITE stores its ranges as integers, which matters for speed (see below).
  • Go API on AWS Lambda: one endpoint, GET /?type=ip2location&ipaddress=49.36.18.209, where type picks the dataset. It validates the address with Go's net/netip package, returns JSON with a success flag, and gives clear 400, 404 and 500 errors. It is built as a static binary for Lambda, behind API Gateway or a function URL.
  • CodeIgniter 4 REST app: routes such as /api/ip2location and /api/dbip, with IPv4 validation, a model per dataset and the same JSON shape as the Go API, so either one can serve the same clients.
Lookups & performance

How an IP range lookup works

An IPv4 address is really a 32-bit number. The lookup converts the address with MySQL's INET_ATON() and finds the range that contains it: WHERE INET_ATON(?) BETWEEN ip_from AND ip_to LIMIT 1.

How the ranges are stored decides how fast that is. When the start and end are stored as integers, as in IP2Location LITE, MySQL compares numbers directly. When a dataset stores them as dotted text, every row has to be converted during the search, which is far slower on millions of rows. Storing every dataset's ranges as indexed integers is the first step to making all lookups equally fast.

For higher traffic, the next steps are clear: cache repeated lookups in memory or Redis, because real traffic repeats the same addresses often; reuse the database connection across warm Lambda invocations; and add IPv6 ranges.

Good to know

Choosing an IP geolocation service

  • Accuracy: country-level results are usually reliable, but city-level results are approximate, especially for mobile networks. Free LITE datasets are less detailed than the commercial editions.
  • Freshness: IP allocations change, so plan to re-import the data regularly.
  • Licences: the free IP2Location LITE and DB-IP Lite datasets require attribution, so credit them wherever results are shown.
  • Self-hosted or third-party: a self-hosted API like this one has no per-request fees or rate limits, and no visitor IP addresses leave your systems.
Tech stack

Tech stack

  • Go
  • AWS Lambda
  • API Gateway
  • CodeIgniter 4
  • PHP 8.2
  • MySQL
  • IP2Location LITE DB11
  • DB-IP

Results

IPv4 ranges across the datasets
8million+
Geolocation datasets behind one API
3
One endpoint, three datasetsSwitch the data source with a single type parameter.
ServerlessA Go binary on AWS Lambda, with no server to manage.
Two runtimes, one formatGo and CodeIgniter 4 return the same JSON, so either can serve clients.
Clear errorsInvalid addresses and misses return clean 400 and 404 responses.

Need an API built or scaled?

I build APIs in PHP and Go, from data import to deployment. Tell me what you need it to do.