Delphi DLL EasyPost: EasyPost Strict Address Verification

Back to Index

Create an address, but with a strict verify, and shows how to get the error response information.

Documentation: https://www.easypost.com/docs/api#addresses

CURL Command

curl -X POST https://api.easypost.com/v2/addresses \
  -u EASYPOST_API_KEY: \
  -d "verify[]=delivery" \
  -d "address[street1]=UNDELIVERABLE ST" \
  -d "address[city]=SAN FRANCISCO" \
  -d "address[state]=CA" \
  -d "address[zip]=94104" \
  -d "address[country]=US" \
  -d "address[company]=EasyPost" \
  -d "address[phone]=415-123-4567"

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
strResponseBody: PWideChar;
jsonResponse: HCkJsonObject;
errorCode: PWideChar;
errorMessage: PWideChar;
i: Integer;
count_i: Integer;
suggestion: Boolean;
code: PWideChar;
field: PWideChar;
message: PWideChar;

begin
rest := CkRest_Create();

//  URL: https://api.easypost.com/v2/addresses
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'api.easypost.com',port,bTls,bAutoReconnect);
if (success <> True) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(CkRest_getConnectFailReason(rest)));
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_SetAuthBasic(rest,'EASYPOST_API_KEY','');

CkRest_AddQueryParam(rest,'verify[]','delivery');
CkRest_AddQueryParam(rest,'address[street1]','UNDELIVERABLE ST');
CkRest_AddQueryParam(rest,'address[city]','SAN FRANCISCO');
CkRest_AddQueryParam(rest,'address[state]','CA');
CkRest_AddQueryParam(rest,'address[zip]','94104');
CkRest_AddQueryParam(rest,'address[country]','US');
CkRest_AddQueryParam(rest,'address[company]','EasyPost');
CkRest_AddQueryParam(rest,'address[phone]','415-123-4567');

strResponseBody := CkRest__fullRequestFormUrlEncoded(rest,'POST','/v2/addresses');
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_Load(jsonResponse,strResponseBody);

errorCode := CkJsonObject__stringOf(jsonResponse,'error.code');
errorMessage := CkJsonObject__stringOf(jsonResponse,'error.message');
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'error.errors');
while i < count_i do
  begin
CkJsonObject_putI(jsonResponse,i);
    suggestion := CkJsonObject_IsNullOf(jsonResponse,'error.errors[i].suggestion');
    code := CkJsonObject__stringOf(jsonResponse,'error.errors[i].code');
    field := CkJsonObject__stringOf(jsonResponse,'error.errors[i].field');
    message := CkJsonObject__stringOf(jsonResponse,'error.errors[i].message');
    i := i + 1;
  end;

CkRest_Dispose(rest);
CkJsonObject_Dispose(jsonResponse);

Sample JSON Response Body

{
  "error": {
    "code": "ADDRESS.VERIFY.FAILURE",
    "message": "Unable to verify address.",
    "errors": [
      {
        "suggestion": null,
        "code": "E.ADDRESS.NOT_FOUND",
        "field": "address",
        "message": "Address not found"
      },
      {
        "suggestion": null,
        "code": "E.HOUSE_NUMBER.MISSING",
        "field": "street1",
        "message": "House number is missing"
      }
    ]
  }
}