Home / Python integration guide
Python integration guide
Call the LatLongAPI from Python with a JSON POST request, server-side license configuration, and HTTP error handling.
Configure your application
Use Python 3. This example uses the standard library, with no additional packages. Save it as example.py.
In a Bash or compatible shell, set LATLONG_API_BASE_URL to your assigned HTTPS origin and LATLONG_API_LICENSE to your license. For testing, use the test origin below with a test license. Replace YOUR_TEST_LICENSE with the license issued for that environment. Keep the license outside source control and browser code.
export LATLONG_API_BASE_URL="https://v1-dev.latlongapi.com"
export LATLONG_API_LICENSE="YOUR_TEST_LICENSE"Request an address
import json
import os
import urllib.request
base = os.environ['LATLONG_API_BASE_URL'].rstrip('/')
payload = json.dumps({
'lat': 51.5007, 'long': -0.1246,
'api': os.environ['LATLONG_API_LICENSE']
}).encode('utf-8')
request = urllib.request.Request(
base + '/api/v1/reverse', data=payload,
headers={'Content-Type': 'application/json'}, method='POST'
)
# HTTPError is raised for non-success statuses.
with urllib.request.urlopen(request, timeout=10) as response:
data = json.load(response)
print(data['result']['short_address'])Run the example
python3 example.pyUse the result
The example prints result.short_address. Read result.long_address for a longer description. Check nullable city and country fields before displaying them.
Handle failures in your service
Set a timeout, catch request errors, and record the status and request identifier without logging the license. Retry temporary failures with bounded backoff. A 401 requires a valid license; a 422 requires correcting the input.