Introduction

This is the documentation for the Python client for Salesfly® API. You can find the source code for the client on GitHub. We have API clients in several other programming languages hosted on GitHub.

Requirements

The Python client for Salesfly API requires Python version 3.4 or later. If you’re using a Mac or Linux machine, you probably already have Python installed. You can check this by opening up a terminal and running the following command:

python --version

You should see something like:

Python 3.7.1

Windows users can follow this tutorial for installing Python on Windows, or follow the instructions from Python's documentation.

Installation

The easiest way to install the library is using pip, a package manager for Python that makes it easier to install the libraries you need. Simply run this in the terminal:

pip install salesfly

If you get a pip: command not found error, you can also use easy_install by running this in your terminal:

easy_install salesfly

If you'd prefer a manual installation, you can download the source code (ZIP) for salesfly-python and then install the library by running:

python setup.py install

in the folder containing the salesfly-python library.

Using

Now that we have Python and salesfly-python installed, we can start using the API. In the example below we will look up the geolocation of the IP address 8.8.8.8.

import os
import salesfly

APIKEY = os.environ.get("SALESFLY_APIKEY")

client = salesfly.Client(api_key=APIKEY)
location = client.geoip.get("8.8.8.8")
print("Country code: {0}".format(location["country_code"]))

Error handling

Handle errors with try...except:

import salesfly
from salesfly.errors import ResponseError

# If you enter an invalid IP address, you get an error
try:
    client = salesfly.Client(api_key="YOUR-API-KEY")
    location = client.geoip.get("x")
except ValueError as e:
    # Missing API key?
except ResponseError as e:
	if e.code == "err-invalid-ip":
		# Handle error...
	else:
		# Other error...

You will find all the error codes in the REST API documentation.