Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

SendGrid: Look up a bounced email address

See more SendGrid Examples

Look up a recipient on the bounce suppression list without changing it. SendGrid returns an array. An empty array means there is no bounce record for this lookup; it does not prove a particular message reached the inbox.

Retrieve a bounce

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oRows
LOCAL oRow
LOCAL i

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
//  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
oHttp:AuthToken := "SENDGRID_API_KEY"
oResp := CreateObject("Chilkat.HttpResponse")

//  SetUrlVar encodes the raw address as a URL component, including the plus sign.
oHttp:SetUrlVar("address", "avery+orders@example.com")

nSuccess := oHttp:HttpNoBody("GET", "https://api.sendgrid.com/v3/suppression/bounces/{$address}", oResp)
IF (nSuccess != 1)
    IF (oResp:StatusCode == 0)
        ? oHttp:LastErrorText
    ELSE
        ? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
    ENDIF

    oHttp:destroy()
    oResp:destroy()
    RETURN
ENDIF

IF (oResp:StatusCode != 200)
    ? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
    oHttp:destroy()
    oResp:destroy()
    RETURN
ENDIF

oRows := CreateObject("Chilkat.JsonArray")
nSuccess := oRows:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Unexpected response: expected JSON."
    oHttp:destroy()
    oResp:destroy()
    oRows:destroy()
    RETURN
ENDIF

IF (oRows:Size == 0)
    ? "No bounce record for this address."
    oHttp:destroy()
    oResp:destroy()
    oRows:destroy()
    RETURN
ENDIF

oRow := CreateObject("Chilkat.JsonObject")
i := 0
DO WHILE (i < oRows:Size)
    oRows:ObjectAt2(i, oRow)
    ? oRow:StringOf("email") + ": " + oRow:StringOf("status") + " " + oRow:StringOf("reason")
    i := i + 1
ENDDO

oHttp:destroy()
oResp:destroy()
oRows:destroy()
oRow:destroy()