Skip to content

Validating a mailing address

Iryna Trush edited this page Nov 16, 2023 · 3 revisions

Validate an address to ensure that mailings will arrive. Amplify does not store this information; this API integrates with the Lob US Verification API.

Code Description

The lob.js code is a route handler function for the /createLetter endpoint in a Node.js application that uses the Express.js framework. This endpoint is used to create a new letter using the Lob API and Stripe API.

The function first extracts the relevant properties from the request body, including the description, to, from, template_id, and sessionId.

It then initializes the Lob and Stripe APIs and retrieves the Stripe checkout session using the stripe.checkout.sessions.retrieve method. The function verifies that the payment has succeeded using the stripe.paymentIntents.retrieve method. If the payment has not succeeded, the function returns a 500 status code with an error message.

If the from parameter contains the string "test_", the function returns a fake delivery date for testing purposes.

Otherwise, the function creates a new letter using the Lob API by calling the lob.letters.create method with the description, to, from, template_id, and color properties. It then returns a 200 status code with the expected delivery date of the letter.

If an error occurs during the execution of the function, it catches the error, logs it to the console, issues a refund using the Stripe API, and returns a 500 status code with an error message.

Verify a mailing address

Evaluates a single address object to determine if it is a valid, deliverable, residential address within the United States.

POST

Syntax

api/lob/addressVerification

Sample query: https://murmuring-headland-63935.herokuapp.com/api/lob/addressVerification

Sample body:

{"line1":"123 Street Name","line2":null,"city":"SomeCity","state":"CA","zip":"12345-6789"}

Returns

  • Success: Status 200. The address is valid as written.
  • Success: Status 200. The address is valid, but there is a warning, and a revisedAddress object. Example: The address is deliverable, but suite number "300" does not exist.
  • Fail: Status 400. The address is invalid. Example: The zip code is only 4 digits.
  • Fail: Status 400. The address is not deliverable. Non-residential addresses, PO boxes, and addresses in Puerto Rico are not supported.

Tests

The provided code in lob.test.js includes a series of tests for different types of addresses. The tests are designed to verify the functionality of an API endpoint that validates addresses. Each test sends a POST request to the endpoint with a specific type of address and checks the response.

  • There are tests that check for a 200 status code for various types of valid residential addresses. These include a house, a highrise, a Department of State address, a military address, and a residence with an unnecessary unit. Each test asserts that the response status is 200 and the response body matches the expected object.

  • There are also tests that check for a 400 status code for unsupported or invalid addresses. These include a post office box, a residence in Puerto Rico, a commercial building, a commercial highrise, and an undeliverable address. Each test asserts that the response status is 400 and the response body matches the expected object.

  • Finally, there's a test for the 'POST /api/lob/createAddress' route. It checks if a 200 status is returned for an address meeting all requirements. The test defines an address object that meets all requirements, makes a POST request to the route with the address, and checks if the response status is 200.

Clone this wiki locally