Tutorial: How to Use an IP Location Finding API with Kotlin

To use an IP location finding API, you need to send a GET request to the API's endpoint. Here's how you can do it using Kotlin:


import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import java.net.HttpURLConnection
import java.net.URL

data class Response(
    val city: City,
    val continent: Continent,
    val country: Country,
    val location: Location,
    val subdivisions: List,
    val traits: Traits

data class City(
    @SerializedName("names") val names: Map

data class Continent(
    @SerializedName("code") val code: String

data class Country(
    @SerializedName("names") val names: Map

data class Location(
    @SerializedName("latitude") val latitude: Double,
    @SerializedName("longitude") val longitude: Double,
    @SerializedName("time_zone") val timeZone: String,
    @SerializedName("weather_code") val weatherCode: String

data class Subdivision(
    @SerializedName("names") val names: Map

data class Traits(
    @SerializedName("autonomous_system_number") val autonomousSystemNumber: Int,
    @SerializedName("autonomous_system_organization") val autonomousSystemOrganization: String,
    @SerializedName("connection_type") val connectionType: String,
    @SerializedName("isp") val isp: String,
    @SerializedName("user_type") val userType: String

fun main() {
    val ipAddress = "IP_Address" // Replace with the IP Address you want to lookup
    val apiKey = "YOUR_API_KEY" // Replace with your actual API key
    val url = "https://api.findip.net/$ipAddress/?token=$apiKey"

    val connection = URL(url).openConnection() as HttpURLConnection
    connection.requestMethod = "GET"

    val response = connection.inputStream.bufferedReader().use { it.readText() }
    val data = Gson().fromJson(response, Response::class.java)

    println("City Name: ${data.city.names["en"]}")
    println("Continent Code: ${data.continent.code}")
    println("Country Name: ${data.country.names["en"]}")
    println("Latitude: ${data.location.latitude}")
    println("Longitude: ${data.location.longitude}")
    println("Time Zone: ${data.location.timeZone}")
    println("Weather Code: ${data.location.weatherCode}")
    data.subdivisions.forEach { println("Subdivision Name: ${it.names["en"]}") }
    println("Autonomous System Number: ${data.traits.autonomousSystemNumber}")
    println("Autonomous System Organization: ${data.traits.autonomousSystemOrganization}")
    println("Connection Type: ${data.traits.connectionType}")
    println("ISP: ${data.traits.isp}")
    println("User Type: ${data.traits.userType}")


Remember to replace 'IP_Address' and 'YOUR_API_KEY' with the actual IP address you want to verify and your actual API key. Also, ensure to have the necessary packages installed (net/http, uri, json).