Example - IP Enrichment API

Code examples and walkthroughs using the IP Enrichment API

Example

When specifying query parameters within a URL, you should separate them with an ampersand (&). When specifying query parameters within a JSON object, you should use lists for the request parameters.

We've provided code samples in cURL, Python, Ruby, Go and JavaScript. If you aren't comfortable working in any of these languages, feel free to use this handy tool to convert code from cURL to the language of your choice.

📘

See a Bug? Want To See a Specific Example?

Feel free to use the Suggest Edits button in the top right-hand corner of this page to point out any bugs or to request any specific code examples for this API that you'd like to see here.

The following example enriches an IP:

import json

# See https://github.com/peopledatalabs/peopledatalabs-python
from peopledatalabs import PDLPY

# Create a client, specifying your API key
CLIENT = PDLPY(
    api_key="YOUR API KEY",
)

# Create a parameters JSON object
PARAMS = {
    "ip": "72.212.42.169"
}

# Pass the parameters object to the IP Enrichment API
json_response = CLIENT.ip(**PARAMS).json()

# Check for successful response
if json_response["status"] == 200:
  record = json_response['data']

  # Print selected fields
  print(
    record['ip']["location"]["metro"],
    record['company']["name"]
  )

  print(f"Successfully enriched ip with PDL data.")

  # Save enrichment data to JSON file
  with open("my_pdl_enrichment.jsonl", "w") as out:
    out.write(json.dumps(record) + "\n")
else:
  print("Enrichment unsuccessful. See error and try again.")
  print("error:", json_response)
curl -X GET -G \
  'https://api.peopledatalabs.com/v5/ip/enrich'\
  -H 'X-Api-Key: xxxx' \
 	--data-urlencode 'ip=72.212.42.169'
// See https://github.com/peopledatalabs/peopledatalabs-js
import PDLJS from 'peopledatalabs';

import fs from 'fs';

// Create a client, specifying your API key
const PDLJSClient = new PDLJS({ apiKey: "YOUR API KEY" });

// Create a parameters JSON object
const params = {
  ip: "72.212.42.169"
}

// Pass the parameters object to the IP Enrichment API
PDLJSClient.ip(params).then((data) => {
    var record = data.data
    
    // Print selected fields
    console.log(
        record["ip"]["location"]["metro"],
      	record["company"]["name"]
        )
        
    console.log("Successfully enriched ip with PDL data.")
    
    // Save enrichment data to JSON file
    fs.writeFile("my_pdl_enrichment.jsonl", Buffer.from(JSON.stringify(record)), (err) => {
        if (err) throw err;
    });
}).catch((error) => {
    console.log("Enrichment unsuccessful. See error and try again.")
    console.log(error);
});
require 'json'

# See https://github.com/peopledatalabs/peopledatalabs-ruby
require 'peopledatalabs'

# Set your API key
Peopledatalabs.api_key = 'YOUR API KEY'

# Create a parameters JSON object
PARAMS = {
    "profile": "72.212.42.169"
}

# Pass the parameters object to the IP Enrichment API
json_response = Peopledatalabs::Enrichment.ip(params: PARAMS)

# Check for successful response
if json_response['status'] == 200
    record = json_response['data']
    
    # Print selected fields
    puts \
        "#{record['ip']['location']['metro']} \
         #{record['company']['name']}"
    
    puts "Successfully enriched ip with PDL data."
    
    # Save enrichment data to JSON file
    File.open("my_pdl_enrichment.jsonl", "w") do |out|
        out.write(JSON.dump(record) + "\n")
    end
else
    puts "Enrichment unsuccessful. See error and try again."
    puts "error: #{json_response}"
end
package main

import (
    "fmt"
    "os"
    "encoding/json"
    "context"
)

// See https://github.com/peopledatalabs/peopledatalabs-go
import (
    pdl "github.com/peopledatalabs/peopledatalabs-go"
    pdlmodel "github.com/peopledatalabs/peopledatalabs-go/model"
)

func main() {
    // Set your API key
    apiKey := "YOUR API KEY"
    // Set API key as environmental variable
    // apiKey := os.Getenv("API_KEY")

    // Create a client, specifying your API key
    client := pdl.New(apiKey)
    
    // Create a parameters JSON object
    params := pdlmodel.IPParams {
        IPBaseParams: pdlmodel.IPBaseParams {
            IP: "72.212.42.169",
        }
    }

    // Pass the parameters object to the IP Enrichment API
    response, err := client.IP(context.Background(), params)
    
    // Check for successful response
    if err == nil {
        // Convert the API response to JSON
        jsonResponse, jsonErr := json.Marshal(response.Data)
        if jsonErr == nil {
            var record map[string]interface{}
            json.Unmarshal(jsonResponse, &record)
            // Print selected fields
            fmt.Println(
                       record["ip"]["location"]["metro"], 
                       record["company"]["name"])
        
            fmt.Println("Successfully enriched ip with PDL data.")
        
            // Save enrichment data to JSON file
            out, outErr := os.Create("my_pdl_enrichment.jsonl")
            defer out.Close()
            if outErr == nil {
                out.WriteString(string(jsonResponse) + "\n")
            }
            out.Sync()
        }
   	} else {
        fmt.Println("Enrichment unsuccessful. See error and try again.")
        fmt.Println("error:", err)
    }
}
import requests, json

# Set your API key
API_KEY = "YOUR API KEY"

# Set the IP Enrichment API URL
PDL_URL = "https://api.peopledatalabs.com/v5/ip/enrich"

# Create a parameters JSON object
PARAMS = {
    "api_key": API_KEY,
    "ip": "72.212.42.169"
}

# Pass the parameters object to the IP Enrichment API
json_response = requests.get(PDL_URL, params=PARAMS).json()

# Check for successful response
if json_response["status"] == 200:
  record = json_response['data']

  # Print selected fields
  print(
    record['ip']['location']['metro'],
    record['company']['name']
  )

  print(f"Successfully enriched ip with PDL data.")

  # Save enrichment data to JSON file
  with open("my_pdl_enrichment.jsonl", "w") as out:
    out.write(json.dumps(record) + "\n")
else:
  print("Enrichment unsuccessful. See error and try again.")
  print("error:", json_response)