ZIP Code Distance API - Calculate Distance Between ZIP Codes
ZIP Code Distance API is a quick solution to calculate straight-line distance (as-the-crow-flies), not the drive distance, between ZIP codes and postal codes. You can use this API in two ways:
- Calculate distance from a base point to a list of target ZIP codes or postal codes. The base point can be a ZIP code, a postal code or a set of location coordinates (latitude & longitude). And, the list of target ZIP codes or postal codes from the same country as the base point.
- Calculate distance between all the possible pairs of ZIP codes in a list and filter all the pairs that lie within an upper threshold on distance between them.
You can also configure the distance measuring unit to return the calculated distance value per your need. Supported distance measuring units are `mi` (mile), `km` (kilometer), `m` (meter), `ft` (feet), `yd` (yard), `in` (inch). But, ZIP Code Distance API uses `km` (kilometer) as default distance measuring unit.
ZIP Code Distance API has vast usage in businesses like to:
- plan efficient delivery routes
- Service Area: define service areas for your field teams
- measure ETA for delivery
- calculate shipping charges
- manage logistics
- emergency response in disaster management systems
- define service area
- find nearest services
Features
ZIP Code or Postal Code as Base
You can use a ZIP code or a postal code at the base point, to calculate distance to your provided list of ZIP codes or postal code.
Location (Latitude & Longitude) as Base
You can also select a set of location coordinates (latitude, longitude) as the base to calculate distance to the provided list of ZIPs.
Bulk Comparison
ZIP Code Distance API can compare up to 100 ZIP codes or postal codes at a time in both modes. When pairing up 100 ZIP codes, it'll make up to 4950 pairs.
Configure Distance Measuring Unit
ZIP Code Distance API allows to convert distance between ZIP codes in a wide range of distance measuring units per your need. Allowed distance measuring units are `mi` (miles), `km` (kilometers), `m` (meters), `ft` (feets), `yd` (yards), `in` (inches). Though, `km` (kilometer) is the default unit.
ZIP Code Distance API
API Endpoint
$ pip install requestsimport requests
import json
url = "https://api.apifreaks.com/v1.0/zipcode/distance"
payload = json.dumps({ "code": "V6H 3L4", "compare": ["P3A 5W8", "B3B 1K1", "H3E 1B5"], "country": "CA", "unit": "mi" })
headers = {
'Content-Type': 'application/json',
'X-apiKey': 'API-KEY'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
A maximum of 100 ZIP codes or postal codes can be included in the request body for a bulk lookup request.
You must provide either a pair of lat and long or a combination of code and country as the required fields to specify the origin location. If both lat/long and code/country are provided in the request, the code and country combination will take precedence.
ZIP Code Distance Match API
API Endpoint
$ pip install requestsimport requests
import json
url = "https://api.apifreaks.com/v1.0/zipcode/distance/match"
payload = json.dumps({ "codes": ["71149", "56517", "54510", "51549", "10009"], "country": "US", "distance": 1000, "unit": "mi" })
headers = {
'Content-Type': 'application/json',
'X-apiKey': 'API-KEY'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
A maximum of 100 ZIP codes or postal codes can be included in the request body for a bulk lookup request.
How-To-Guides
Calculate Distance from A Location Coordinates to a List of ZIP Codes
You can use ZIP Codes Distance API to find distance to a list of ZIP codes or postal codes from any location point. All you need to provide is a list of ZIP codes or postal codes, a location coordinates and the country where they exist to calculate distance using ZIP Codes Distance API.
Pass all these parameters in the JSON body: lat (latitude), long (longitude), compare (array of ZIP/postal codes), and country (ISO 3166-1 alpha-2 code).
Let’s find the distance to E1 0AZ, HD1 1AB, OX1 1PU postcodes from location (lat: 51.5132336, long: -0.2273734) in the United Kingdom. Here is a curl request to do that:
# Response { "result_count": 3, "results": [ { "code": "E1 0AZ", "distance": 11.977 }, { "code": "HD1 1AB", "distance": 259.566 }, { "code": "OX1 1PU", "distance": 76.004 } ] }curl -s -X POST "https://api.apifreaks.com/v1.0/zipcode/distance?apiKey=API-KEY" \ -H "Content-Type: application/json" \ -d '{ "lat": 51.5132336, "long": -0.2273734, "compare": ["E1 0AZ", "HD1 1AB", "OX1 1PU"], "country": "GB" }'
All the distances in the above response are measured in kilometers (km), the default distance measuring unit. But, you can specify your choice of unit.
Convert JSON Response to XML
ZIP Codes Distance API can respond in JSON or XML formats for all requests. But, it responds in JSON format by default. Pass format=xml URL parameter along with your request to convert the API response in XML format. Here is a curl request to do that:
# Response <CodeDistance> <result_count>3</result_count> <results> <code_distance> <code>E1 0AZ</code> <distance>11.977</distance> </code_distance> <code_distance> <code>HD1 1AB</code> <distance>259.566</distance> </code_distance> <code_distance> <code>OX1 1PU</code> <distance>76.004</distance> </code_distance> </results> </CodeDistance>curl -s -X POST "https://api.apifreaks.com/v1.0/zipcode/distance?format=xml&apiKey=API-KEY" \ -H "Content-Type: application/json" \ -d '{ "lat": 51.5132336, "long": -0.2273734, "compare": ["E1 0AZ", "HD1 1AB", "OX1 1PU"], "country": "GB" }'
Find all Pairs of ZIP Codes within a Specific Distance
Given a list of ZIP codes, their country and an upper threshold on distance between them, you can find all pairs of ZIP codes in the list that are within the range of specified distance.
Suppose that you‘ve 5 US ZIP codes `71149`, `56517`, `54510`, `51549` and `10009`. Lets find out all ZIP codes pairs that are within 1000 miles from each other. Here is a curl request to do that:
# Response { "result_count": 2, "results": [{ "code_1": "56517", "code_2": "51549", "distance": 442.561 }, { "code_1": "71149", "code_2": "51549", "distance": 605.837 }] }curl -s -X POST "https://api.apifreaks.com/v1.0/zipcode/distance/match?apiKey=API-KEY" \ -H "Content-Type: application/json" \ -d '{ "codes": ["71149", "56517", "54510", "51549", "10009"], "country": "US", "distance": 1000, "unit": "mi" }'
The above response includes all the ZIP codes that up to 1000 miles apart.
Pricing
To use the ZIP Code Distance and Distance Match APIs, API credits are required. Charges apply only for successful queries, defined by a 2xx status code. If a request results in a 4xx or 5xx status code, no credits will be deducted, and any credits already charged will be refunded.
- ZIP Code Distance API: For each successful lookup, 5 credit will be charged.
- ZIP Code Distance Match API: For each successful lookup, 5 credit will be charged.