Skip to main content

Error Handling

PostalDataPI uses standard HTTP status codes. All error responses return a JSON object with an error field.

Error Response Format

{
"error": "Human-readable error message"
}

Some errors include additional fields:

{
"error": "Insufficient balance",
"currentBalance": "$0.0000",
"message": "Please add funds to your account to continue using the API"
}
{
"error": "Rate limit exceeded. Please wait before making another request.",
"retryAfter": 30
}

HTTP Status Codes

StatusNameDescription
200OKRequest succeeded
400Bad RequestMissing or invalid parameters
401UnauthorizedInvalid or missing API key
402Payment RequiredAccount balance is zero
404Not FoundPostal code or city not found
429Too Many RequestsRate limit exceeded
500Internal Server ErrorUnexpected server error

Handling Errors in Code

The Python SDK raises typed exceptions that you can catch individually:

from postaldatapi import (
PostalDataPI,
AuthenticationError,
NotFoundError,
ValidationError,
RateLimitError,
InsufficientBalanceError,
ServerError,
)

client = PostalDataPI(api_key="YOUR_API_KEY")

try:
result = client.lookup("00000")
except NotFoundError:
print("Postal code does not exist")
except AuthenticationError:
print("Check your API key")
except ValidationError as e:
print(f"Bad request: {e.message}")
except InsufficientBalanceError:
print("Add funds at postaldatapi.com")
except RateLimitError:
print("Slow down -- rate limit exceeded")
except ServerError:
print("Server error -- try again later")

All exceptions inherit from PostalDataPIError, so you can also catch them all:

from postaldatapi import PostalDataPIError

try:
result = client.lookup("90210")
except PostalDataPIError as e:
print(f"API error {e.status_code}: {e.message}")

Retry Strategy

For 429 (rate limit) responses, use the retryAfter value in the response body to determine when to retry. For 500 errors, implement exponential backoff:

import time

def lookup_with_retry(client, postal_code, max_retries=3):
for attempt in range(max_retries):
try:
return client.lookup(postal_code)
except RateLimitError:
time.sleep(attempt * 2 + 1)
except ServerError:
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt)

Common Mistakes

SymptomCauseFix
400: Missing required field: apiKeyapiKey not in request bodyAdd "apiKey": "..." to JSON body
400: Missing 'state' or 'ST'US city search without stateAdd "ST": "CA" or "state": "California"
401: Invalid API keyTypo in key, or key was revokedCheck key in dashboard
404: ZIP code not foundPostal code doesn't exist in that countryVerify postal code and country code