Addresses
Parse
The PackageX address parser takes addresses in multiple different formats and returns the address in the PackageX address mode.
Parse String
If your address is in a string
, we'll parse the full string. Commonly this string is from the autocomplete API, where you'll optionally pass the line2 value separately. The line2 value can be passed in as a separate field or can be included in the string.
const data = {
address: "500 7th Ave, New York, NY",
address_line2: "Floor 10", //optional if collected a line2 value
};
fetch("https://api.packagex.io/v1/address/parse", {
method: "POST",
headers: {
"PX-API-KEY": process.env.PX_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
Parse Object
If you have collected data about your address in a more structured format, you can send the address object. While in the response, we'll give you the state
/ state_code
and country
/ country_code
properties parsed into the full name and abbreviation. You can provide the state and country values in any of the properties, the parser isn't picky about it.
const data = {
address: {
line1: "500 7th Ave",
line2: "Floor 10",
city: "New York",
state: "NY", //can use full string or abbreviation
postal_code: "10018", //Not required but improves accuracy
country: "US", //Not required but improves accuracy for non US addresses
},
};
const response = await fetch("https://api.packagex.io/v1/address/parse", {
method: "POST",
headers: {
"PX-API-KEY": process.env.PX_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const address = response.data;
Parse Location
There could be times when you're using a PackageX Location
object which already includes your address in it. You can pass the id
of the location into the address
field of the parser and it will return the address of your location. This can be done anytime we ask for an address property. A common example is when creating a shipment. You can pass the location ID into the address property to the sender and/or recipient objects.
const data = {
address: "loc_jZ1fYiQobeWthjbhme3vDX",
};
const response = await fetch("https://api.packagex.io/v1/address/parse", {
method: "POST",
headers: {
"PX-API-KEY": process.env.PX_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const address = response.data;
Parse Address ID
Like the location ID, you can also provide a PackageX address id
to the parser and it will respond with the address.
const data = {
address: "addr_2d4955db874c35355d0674a758220b2b761ea7bd1424cbbc91282c2b69f044ac",
};
const response = await fetch("https://api.packagex.io/v1/address/parse", {
method: "POST",
headers: {
"PX-API-KEY": process.env.PX_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const address = response.data;