How to Use FreeIPAPI in Your Laravel Project (with Caching)

If you’ve ever needed IP geolocation data in your Laravel project, you know how valuable it is for personalization, fraud prevention, and analytics. FreeIPAPI is a fast and reliable free IP geolocation API that provides accurate IP-based data such as the country, city, ASN, timezone, currency, and more — all at zero cost.

In this guide, we’ll walk you through how to integrate FreeIPAPI into your Laravel application and how to optimize it with caching so you don’t send unnecessary API requests.

Why Use FreeIPAPI for IP Geolocation?

FreeIPAPI makes it easy to retrieve location data from an IP address. With a simple GET request like this:

https://free.freeipapi.com/api/json/{ip}

You’ll receive a detailed JSON response that includes useful fields such as:

  • countryName and countryCode
  • cityName and regionName
  • latitude and longitude
  • timeZones and currencies
  • asn and ISP details
  • Proxy detection

For example, querying an IP from Düsseldorf, Germany returns:

{
  "ipVersion": 6,
  "ipAddress": "2a02:8071:1212:aa0:d914:e794:457d:1212",
  "latitude": 51.2217,
  "longitude": 6.77616,
  "countryName": "Germany",
  "countryCode": "DE",
  "capital": "Berlin",
  "phoneCodes": [49],
  "timeZones": ["Europe/Berlin", "Europe/Busingen"],
  "zipCode": "40123",
  "cityName": "Düsseldorf",
  "regionName": "North Rhine-Westphalia",
  "continent": "Europe",
  "currencies": ["EUR"],
  "languages": ["de"],
  "asn": "3209",
  "asnOrganization": "Vodafone GmbH",
  "isProxy": false
}

That’s a lot of geo-data for just one simple API call!

Why Cache IP Lookups?

Calling an external API on every request can cause performance issues and unnecessary latency. Since IP information doesn’t change frequently, it makes sense to cache it.

In Laravel, caching is super simple thanks to its built-in caching system. By storing IP lookup results for one hour, you’ll:

  • Improve site performance
  • Reduce API calls
  • Speed up geo-based personalization

Installing Laravel (Skip If Already Done)

If you don’t already have a Laravel project, create one using Composer:

composer create-project laravel/laravel freeipapi-demo
cd freeipapi-demo
php artisan serve

Making HTTP Requests with Laravel

Laravel provides a clean API for making HTTP requests using its Http facade. We’ll use this to fetch IP info from FreeIPAPI.

Creating an IP Lookup Service with Cache

Let’s create a service file that handles the request and adds caching.

Create a new file: app/Services/IpLookupService.php

<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class IpLookupService
{
    public function getIpData(string $ip)
    {
        // Cache key
        $cacheKey = "ip_lookup_" . $ip;

        // Cache for 1 hour (3600 seconds)
        return Cache::remember($cacheKey, 3600, function () use ($ip) {
            $url = "https://free.freeipapi.com/api/json/{$ip}";

            $response = Http::get($url);

            if ($response->successful()) {
                return $response->json();
            }

            return null;
        });
    }
}

Using the Service in Your Controller

Now, inject the service into a controller and fetch IP info easily.

Example: app/Http/Controllers/IpController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\IpLookupService;

class IpController extends Controller
{
    protected $ipService;

    public function __construct(IpLookupService $ipService)
    {
        $this->ipService = $ipService;
    }

    public function lookup(Request $request)
    {
        $ip = $request->ip(); // Get the user’s IP

        $ipData = $this->ipService->getIpData($ip);

        return response()->json($ipData);
    }
}

Now, visiting /ip-lookup in your Laravel app will return the user’s geolocation data while applying a 1-hour cache to reduce API calls.

Setting Up a Route

In routes/web.php, add:

use App\Http\Controllers\IpController;

Route::get('/ip-lookup', [IpController::class, 'lookup']);

Example Output

If you run your app and visit /ip-lookup, you might see:

{
  "countryName": "Germany",
  "cityName": "Düsseldorf",
  "latitude": 51.2217,
  "longitude": 6.77616,
  "asn": "3209",
  "asnOrganization": "Vodafone GmbH",
  "isProxy": false
}

Conclusion

Integrating FreeIPAPI into a Laravel app is quick and effective. By caching results for 1 hour, you’ll save resources while still ensuring that your users get fast and accurate IP geolocation data.

Whether you’re building custom dashboards, fraud detection, analytics, or personalized experiences, FreeIPAPI gives you the free IP geolocation API you need — and Laravel makes it easy to implement. 🚀