Validate a Postal Code, Get Location Information About It in Python

Cloudmersive
2 min readJan 28, 2022

It’s important to get your locational information right! Using the validate/address/postal-code iteration of the Cloudmersive Validate API, you can empower your application to ensure any given postal code is valid, and get more information about it in return about the City & State it corresponds with (supports all major countries). In this article we’ll walk through a few simple steps to use this API in Python.

The first step in the process is to install the Python SDK. You can copy & paste the below code from the Cloudmersive API console:

pip install cloudmersive-validate-api-clientfrom __future__ import print_function
import time
import cloudmersive_validate_api_client
from cloudmersive_validate_api_client.rest import ApiException
from pprint import pprint

Once that step is complete, you’ll need to copy in the below snippet to authenticate your API key for access. If you don’t already have an API key, you can get one by creating a free account on the Cloudmersive website (good for 800 API calls per month).

# Configure API key authorization: Apikey
configuration = cloudmersive_validate_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'

Finally, you can finish off the function with the following code, and you’re all set to go:

# create an instance of the API class
api_instance = cloudmersive_validate_api_client.AddressApi(cloudmersive_validate_api_client.ApiClient(configuration))
input = cloudmersive_validate_api_client.ValidatePostalCodeRequest() # ValidatePostalCodeRequest | Input parse request
try:
# Validate a postal code, get location information about it
api_response = api_instance.address_validate_postal_code(input)
pprint(api_response)
except ApiException as e:
print("Exception when calling AddressApi->address_validate_postal_code: %s\n" % e)

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.