Getting started overview

Integrating with IP2Location primarily involves either querying their Web Service API or utilizing downloadable databases for local lookups. This guide focuses on the Web Service API for immediate integration. The initial setup requires creating an account, obtaining an API key, and then constructing a request to query IP geolocation data. IP2Location provides SDKs for several programming languages, which can simplify the process of making API calls and parsing responses. For developers prioritizing data residency or performance, the database options offer an alternative to API-based lookups.

Before proceeding, consider whether the IP2Location pricing model for the Web Service (query-based) or Databases (one-time purchase with annual updates) aligns with your project requirements. The free LITE databases and limited web service queries offer an entry point for evaluation.

Quick Reference: IP2Location Web Service Setup

Step What to Do Where
1. Create Account Register on the IP2Location website. IP2Location Developer Site
2. Get API Key Locate your API key in the member area. IP2Location Member Area
3. Choose Integration Method Select Web Service API or Database. IP2Location Developer Docs
4. Make First Request Use a provided SDK or cURL to query an IP. IP2Location Web Service Documentation
5. Handle Response Parse the JSON or XML data returned. IP2Location API Reference

Create an account and get keys

To access the IP2Location Web Service, an account is required to generate an API key. This key is essential for authenticating your requests and tracking usage against your subscription plan.

  1. Navigate to the IP2Location Website: Go to the IP2Location homepage.
  2. Register for an Account: Look for a "Sign Up" or "Register" link. You will typically need to provide an email address and create a password.
  3. Verify Email (if required): After registration, IP2Location may send a verification email. Follow the instructions in the email to activate your account.
  4. Log In to Member Area: Once registered and verified, log in to the IP2Location member area.
  5. Locate Your API Key: Within the member area dashboard, there will be a section dedicated to "Web Service" or "API Key." Your unique API key will be displayed here. Copy this key, as it will be used in all your API requests. Treat your API key as sensitive information, similar to a password. It should not be exposed in client-side code or publicly accessible repositories. For server-side applications, use environment variables to store and access API keys securely, as recommended by security best practices for API key management.

Your first request

After obtaining your API key, you can make your first request to the IP2Location Web Service. This example uses a common IP address (8.8.8.8, Google's public DNS) to demonstrate the lookup process. IP2Location supports both JSON and XML response formats. For simplicity, this guide focuses on JSON.

The base URL for the Web Service API is https://api.ip2location.com/v2/.

Using cURL (Command Line)

cURL is a command-line tool and library for transferring data with URLs, commonly used for testing API endpoints.

curl "https://api.ip2location.com/v2/?ip=8.8.8.8&key=YOUR_API_KEY&package=WS1&addon=continent,country,region,city,latitude,longitude&format=json"

Replace YOUR_API_KEY with your actual API key obtained from the IP2Location member area. The package parameter specifies the data package (e.g., WS1 for basic geolocation), and addon specifies the fields to retrieve. Refer to the IP2Location Web Service documentation for a full list of available packages and addons.

Using Python

Python is a popular language for scripting API interactions. IP2Location provides an official Python SDK, but a basic request can also be made using the requests library.

import requests

api_key = "YOUR_API_KEY"  # Replace with your actual API key
ip_address = "8.8.8.8"

url = f"https://api.ip2location.com/v2/?ip={ip_address}&key={api_key}&package=WS1&addon=continent,country,region,city,latitude,longitude&format=json"

try:
    response = requests.get(url)
    response.raise_for_status()  # Raise an exception for HTTP errors
    data = response.json()
    print(data)
except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")

This Python script sends a GET request to the IP2Location API and prints the JSON response. Ensure you have the requests library installed (pip install requests).

Expected Response Structure (JSON Example)

A successful response for the IP 8.8.8.8 with the specified addons might look similar to this:

{
  "ip": "8.8.8.8",
  "country_code": "US",
  "country_name": "United States",
  "region_name": "California",
  "city_name": "Mountain View",
  "latitude": 37.40599,
  "longitude": -122.078514,
  "continent_code": "NA",
  "continent_name": "North America",
  "zip_code": "94043",
  "time_zone": "-07:00"
  // ... other fields based on package and addons
}

The exact fields returned depend on your chosen package and addon parameters. Always consult the IP2Location API reference for the most up-to-date response field definitions.

Common next steps

After successfully making your first API call, consider these next steps to further integrate IP2Location into your application:

  • Explore Additional Data Fields: Review the IP2Location Web Service API documentation to understand the full range of data fields available (e.g., domain, ISP, usage type, proxy information) and how different packages (WS1, WS3, WS5, etc.) affect the data returned.
  • Integrate SDKs: Utilize one of the official IP2Location SDKs (PHP, Java, Node.js, Ruby, Go, C#, Perl) to streamline API requests and response parsing in your preferred programming language.
  • Implement Error Handling: Develop robust error handling for API responses, including managing rate limits, invalid API keys, and other potential issues. The API typically returns HTTP status codes and error messages in the JSON response for debugging.
  • Database Integration (Optional): If your application requires high-volume lookups or offline capabilities, consider downloading and integrating one of the IP2Location Databases directly into your system. This often involves using a provided database library for your language.
  • Monitor Usage: Regularly check your API usage within the IP2Location member area to ensure you stay within your subscription limits and to anticipate potential upgrades.

Troubleshooting the first call

If your first API call does not return the expected results, consider the following common issues:

  • Invalid API Key: Double-check that you have copied your API key correctly and that it is active. An incorrect key will typically result in an authentication error.
  • Incorrect URL or Parameters: Verify the base URL, IP address, package, and addon parameters against the IP2Location Web Service documentation. Missing or misspelled parameters can lead to errors or incomplete responses.
  • Rate Limiting: If you are making many requests in a short period, you might hit rate limits, especially on free or trial accounts. The API will usually return a specific error code for this. Implement delays or caching to manage request volume.
  • Network Connectivity: Ensure your development environment has stable internet access and is not blocked by a firewall from reaching api.ip2location.com.
  • JSON/XML Parsing Errors: If the response looks malformed or your application cannot parse it, verify the format parameter (json or xml) matches what your code expects and that the response is valid according to the chosen format. Tools like browser developer tools or online JSON validators can help inspect raw API responses.
  • Subscription Package Limits: Some data fields are only available with higher-tier packages. If a field is missing, confirm your current subscription package supports it.