Author Topic: Address Validation using API  (Read 3841 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Address Validation using API
« on: July 19, 2020, 09:59:15 pm »
In case anyone needs to validate addresses for any reason, here is a way that you can do it! Go to smartystreets.com and make a free account! You are limited to 250 API requests per month and you can validate any address! I've created a simple program to show how it can be done.:
Code: QB64: [Select]
  1. TYPE USaddress
  2.     Street AS STRING
  3.     City AS STRING
  4.     State AS STRING
  5.  
  6. DIM Address AS USaddress
  7.  
  8. Address.Street = "1600 Pennsylvania Avenue NW"
  9. Address.City = "Washington"
  10. Address.State = "DC"
  11.  
  12. PRINT ValidateAddress(Address)
  13.  
  14. FUNCTION ValidateAddress$ (Address AS USaddress)
  15.     DIM URL AS STRING
  16.     DIM URLFile AS STRING
  17.     URL = "https://us-street.api.smartystreets.com/street-address?auth-id=YOURAUTHIDHERE&auth-token=YOURAUTHTOKENHERE&"
  18.     URL = URL + "street=" + FormatAsHTTP(Address.Street) + "&"
  19.     URL = URL + "city=" + FormatAsHTTP(Address.City) + "&"
  20.     URL = URL + "state=" + Address.State + "&"
  21.     URL = URL + "candidates=1"
  22.     URLFile = "addressvalidation.json"
  23.     a% = FileDownload(URL, URLFile)
  24.     DIM U AS INTEGER
  25.     U = FREEFILE
  26.     OPEN URLFile FOR BINARY AS #U
  27.     IF LOF(U) <> 0 THEN
  28.         LINE INPUT #U, result$
  29.     ELSE
  30.         CLOSE #U
  31.         KILL URLFile
  32.         ValidateAddress = "Could not validate"
  33.         EXIT FUNCTION
  34.     END IF
  35.     CLOSE #U
  36.     KILL URLFile
  37.     validation$ = MID$(result$, INSTR(result$, "dpv_match_code") + 17, 1)
  38.     IF validation$ = "Y" THEN
  39.         ValidateAddress = "Address exists and is correct"
  40.     ELSE
  41.         ValidateAddress = "Address either doesn't exist or is invalid"
  42.     END IF
  43.  
  44. FUNCTION FormatAsHTTP$ (Request AS STRING)
  45.     start% = 1
  46.     DO
  47.         position% = INSTR(start%, Request, " ")
  48.         IF position% THEN
  49.             MID$(Request, position%, 1) = "+"
  50.             start% = position% + 1
  51.         END IF
  52.     LOOP UNTIL position% = 0
  53.     FormatAsHTTP = LCASE$(Request)
  54.  
  55.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  56.  
  57. FUNCTION FileDownload (URL AS STRING, File AS STRING)
  58.     FileDownload = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
For obvious reasons I'm not posting my API keys here in the forum. I don't want everyone burning up my allotment. Check it out for yourself and see if you like it! It could be great for an InForm project with an address form where you might need to make sure that someone is entering a valid address. Definitely give this a try!
Shuwatch!

Offline loudar

  • Newbie
  • Posts: 73
  • improve it bit by bit.
    • View Profile
Re: Address Validation using API
« Reply #1 on: July 20, 2020, 08:22:58 am »
Ohhhh this is really cool! Might try that out :D
Check out what I do besides coding: http://loudar.myportfolio.com/

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Address Validation using API
« Reply #2 on: July 20, 2020, 03:37:54 pm »
And if you desired more candidates for your validation you could increase the number in the candidates part of the query string but then you would need to make sure that you parse the return file and handle multiple address returns.
Shuwatch!