Skip to main content

Code Snippets and Example Programs

The following snippets and examples are meant to highlight some commonly used capabilities of the API. For a complete understanding of the API's features, please refer to the libraries' source code (available on github).

Example Scripts​

Example scripts using the Pipl API can be found at the Search API Examples github page

Code Snippets​

Below are a few snippets for the basic things that you can do with the API Code Libraries.

Creating a request​

Option 1 - This is a fast, direct method for simple requests with few parameters:

from piplapis.search import SearchAPIRequest
request = SearchAPIRequest(email=u'clark.kent@example.com', api_key='YOURKEY')

Option 2 - Using the data-model. This method is useful for more complex queries, such as when you need to send multiple values of the same field type:

from piplapis.data import Person, Name, Address, Job

fields = [Name(first=u'Clark', last=u'Kent'),
Address(country=u'US', state=u'KS', city=u'Smallville'),
Address(country=u'US', state=u'KS', city=u'Metropolis'),
Job(title=u'Field Reporter')]
request = SearchAPIRequest(person=Person(fields=fields), api_key='YOURKEY')

Sending a request​

Option 1 – Synchronously

from piplapis.search import SearchAPIError
try:
response = request.send()
except SearchAPIError as e:
print(e.http_status_code, e)

Option 2 – Async: Send the query with a non-blocking call

def my_callback(response=None, error=None):
print response or error

request.send_async(my_callback)
do_other_things()

Working with a response​

πŸ“˜ A response can return either a Person , a list of Possible Persons , or no data.

When working with the response, first check for a person object. If it's null, check for the possible_persons array. If there are neither, there is no data in the response (the persons_count property will be 0 in this case).

The below syntax is available for all fields to quickly access frequently used data (the most recent name, last address, most prominent username, etc.). For example, when using the fast, direct option #1 method:

>>> print(response.name)
u'Clark Joseph Kent'
>>> print(response.gender)
u'Male'
>>> print(response.address)
u'10-1 Hickory Lane, Smallville, Kansas'
>>> print(response.job)
u'Field Reporter at The Daily Planet (2000-2012)'

Access the person object that came with the response:

>>> response.person
<piplapis.data.containers.Person object at 0x02849E50>
>>> response.person.addresses
[Address(country=u'US', state=u'KS', city=u"Metropolis", street=u"Broadway", house=u"1000", apartment=u"355"), Address(country=u'US', state=u'KS', city=u"Smallville", street=u"Hickory Lane", house=u"10", apartment=u"1")]

Working with sources​

Access the response data's list of sources. This is only available if you set the β€œshow_sources” flag to β€œall” or β€œmatching”:

>>> len(response.sources)
2
>>> response.sources[0]
<piplapis.data.containers.Source object at 0x02849D22>
>>> response.sources[0].phones
[Phone(country_code=1, number=9785550145)]
>>> response.sources[0].origin_url
u'http://linkedin.com/clark.kent'

Group sources. Sources can be grouped by domain, category, match and more:

>>> type(response.group_sources_by_domain())
<type 'dict'>
>>> response.group_sources_by_domain().get(u'linkedin.com')
[<piplapis.data.containers.Source object at 0x02849D24>, <piplapis.data.containers.Source object at 0x02849D28>]

Useful utilities​

There are useful utilities implemented for the field objects. Here are some examples:

>>> address = response.address
>>> address.state
u'KS'
>>> address.state_full
u'Kansas'

>>> email = response.email
>>> email.username
u'clark.kent'

Global Preferences​

You also can set search preferences globally, and they will apply to all search requests. Examples shown in the snippet are the default preferences.

Setting global preferences is optionalβ€”you can choose query settings per request if you prefer.

SearchAPIRequest.set_default_settings(api_key=u'YOURKEY', minimum_probability=None,
show_sources=None, minimum_match=None, hide_sponsored=None, live_feeds=None, use_https=True)