Skip to main content

Address validation

Address format varies between each country. The API validates every address it receives against its country's format, using Google's address data, and returns an error for each field that does not fit.

info

Validation is only performed on the format of address data. To check if the address exists or get the address auto-complete, you can integrate services like Shippo or Loqate.

Setting up the address using the API​

As an example of address validation, we will set the billing address in the checkout. The operation can be made with checkoutBillingAddressUpdate:

mutation {
checkoutBillingAddressUpdate(
checkoutId: "Q2hlY2tvdXQ6NThmNGNhMTctOWM2Zi00MzdiLTgyMDQtYmViNDdiYmVlMzY0"
billingAddress: {
country: PL
firstName: "John"
lastName: "Smith"
streetAddress1: "ul. Tęczowa 7"
postalCode: "53-030"
city: "Wroclaw"
}
) {
checkout {
billingAddress {
firstName
lastName
streetAddress1
city
postalCode
}
}
checkoutErrors {
field
message
code
}
}
}
Show more ↓

Successful response:

{
"data": {
"checkoutBillingAddressUpdate": {
"checkout": {
"billingAddress": {
"firstName": "John",
"lastName": "Smith",
"streetAddress1": "ul. Tęczowa 7",
"city": "WROCLAW",
"postalCode": "53-030"
}
},
"checkoutErrors": []
}
}
}

Errors​

Let's create an example of address validation errors. The address below is missing a few fields and has the wrong postal code:

mutation {
checkoutBillingAddressUpdate(
checkoutId: "Q2hlY2tvdXQ6NThmNGNhMTctOWM2Zi00MzdiLTgyMDQtYmViNDdiYmVlMzY0"
billingAddress: {
country: PL
firstName: "John"
lastName: "Smith"
postalCode: "Wrong Code"
}
) {
checkoutErrors {
field
message
code
}
}
}

Response:

{
"data": {
"checkoutBillingAddressUpdate": {
"checkoutErrors": [
{
"field": "city",
"message": "This field is required.",
"code": "REQUIRED"
},
{
"field": "postalCode",
"message": "This value is not valid for the address.",
"code": "INVALID"
},
{
"field": "streetAddress1",
"message": "This field is required.",
"code": "REQUIRED"
},
{
"field": "streetAddress2",
"message": "This field is required.",
"code": "REQUIRED"
}
]
}
}
}
Show more ↓

Each field is validated separately, and each issue has a corresponding error code:

  • postalCode with the wrong format returns the INVALID error
  • a field the country requires that is missing returns the REQUIRED error

Was this page helpful?