Using the USPS Address Cleaner
Live Demo
Enter an address. You can copy from Google Maps
What is Address Cleaning?
Address Cleaning is a process to standardize addresses to make them more deliverable. It helps to improve the accuracy and readability of addresses. Typos can be fixed.
The benefits could include improved mail delivery, better targeting for marketing.
So,
328 west main street
Pittsburgh
OH 70202
Becomes
328 W Main St
Pittsburgh
OH 70202
How Do I Clean Up Or Scrub Address Data?
The USPS offers a service which software developers can use. This can be useful in e-commerce websites where accurate delivery is needed. Developers or the business will need to open a business account and register their application.
Note that the USPS doesn't allow this service to be used to clean addresses in bulk
The application needs to be registered in the USPS developer portal after which an api key and api secret will be granted.
Here is how a developer may do it:
Make a POST request to the USPS oauth2 api endpoint to get the token
Then make a GET request to the /v3/address endpoint using the above token
usps.js
export default async function handler(req, res) {const url = 'https://api.usps.com/oauth2/v3/token'
const headers = {
'Content-Type': 'application/json'
}
const data = {
grant_type = "client_credentials"
client_id: process.env.NEXT_PUBLIC_USPS_KEY,
client_secret: process.env.NEXT_PUBLIC_USPS_SECRET,
scope: "addresses',
state: "'
}
// Get the token from USPS
const uspsTkn = await fetch(url, {
method: "POST",
headers = {
'Content-Type': 'application/json'
},
body = JSON.stringify(data)
});
const resp = await uspsTkn.json()
const { access_token } = resp
const params = req.body
const queryString = Object.keys(params)
.map(key => {encodeURIComponent(key)}={encodeURIComponent(params[key])})
.join('&');
const addrBaseUrl = 'https://api.usps.com/addresses/v3/address'
const addressUrl = `${addrBaseUrl}?${queryString}`
// Make a GET request with the Bearer token
const respUspsAdd = await fetch(addressUrl, {
method: "GET",
headers = {
'Content-Type': 'application/json'
'Authorization': `Bearer ${access_token}`
},
});
const respUspsAddJson = await respUspsAdd.json()
res.status(200).json({respUspsAddJson});
}
}