Real-time email verification using only the Go standard library —
net/http and encoding/json.
Send a GET request with the email and your API token, and check the HTTP status before reading the result.
The helper below returns one of three decisions: accept, reject or defer.
Running out of credits (402), a rate limit (429), an outage or an Unknown result
never becomes a rejection, and a temporary Unknown is asked again after RetryAfterSeconds.
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"time"
)
const (
apiURL = "https://api.validemail.net/"
apiKey = "YOUR_API_KEY" // find it in your ValidEmail dashboard; keep it on your server
)
type AdditionalInfo struct {
Key string `json:"Key"`
Value string `json:"Value"`
}
type VerificationResult struct {
IsValid bool `json:"IsValid"`
Score int `json:"Score"`
Email string `json:"Email"`
State string `json:"State"`
Reason string `json:"Reason"`
Domain string `json:"Domain"`
Free bool `json:"Free"`
Role bool `json:"Role"`
Disposable bool `json:"Disposable"`
AcceptAll bool `json:"AcceptAll"`
Tag bool `json:"Tag"`
MXRecord string `json:"MXRecord"`
RetryAfterSeconds *int `json:"RetryAfterSeconds"`
EmailAdditionalInfo []AdditionalInfo `json:"EmailAdditionalInfo"`
}
var client = &http.Client{Timeout: 90 * time.Second}
// verifyEmail returns a decision ("accept", "reject" or "defer") and a reason.
// "defer" means: let the user continue, keep the address flagged, check it again later.
// Running out of credits, a rate limit or an outage never turns into "reject".
func verifyEmail(email string, maxAttempts int) (decision, reason string) {
for attempt := 1; attempt <= maxAttempts; attempt++ {
query := url.Values{"email": {email}, "token": {apiKey}}
resp, err := client.Get(apiURL + "?" + query.Encode())
if err != nil {
return "defer", "network: " + err.Error()
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return "defer", "network: " + err.Error()
}
// 1. The HTTP status first: only a 200 carries a verdict about the address.
switch {
case resp.StatusCode == http.StatusPaymentRequired:
fmt.Fprintln(os.Stderr, "ValidEmail is out of credits.", string(body)) // alert whoever pays the bill
return "defer", "no_credits"
case resp.StatusCode == http.StatusTooManyRequests:
wait, err := strconv.Atoi(resp.Header.Get("Retry-After"))
if err != nil || wait < 1 {
wait = 1
}
time.Sleep(time.Duration(wait) * time.Second)
continue
case resp.StatusCode != http.StatusOK:
return "defer", fmt.Sprintf("http_%d", resp.StatusCode)
}
// 2. Then the verdict.
var result VerificationResult
if err := json.Unmarshal(body, &result); err != nil {
return "defer", "unreadable response"
}
switch {
case result.State == "Deliverable":
return "accept", result.Reason
case result.State == "Not Deliverable":
return "reject", result.Reason
case result.RetryAfterSeconds != nil:
// Still being resolved (greylisting, a slow server). Asking again for the same address is free.
time.Sleep(time.Duration(*result.RetryAfterSeconds) * time.Second)
continue
}
return "defer", result.Reason // Unknown, and no answer is coming
}
return "defer", "still pending"
}
func main() {
decision, reason := verifyEmail("someone@example.com", 3)
fmt.Printf("%s: %s\n", decision, reason)
}
State = "Unknown")
Some mail servers greylist first-time senders or respond slowly. Instead of failing, the API returns an
interim 200 OK result with State = "Unknown", a Reason of
PENDING or GREYLISTED, and a RetryAfterSeconds hint while the
verification finishes in the background.
RetryAfterSeconds seconds, then repeat the same request to get the final verdict.Unknown as "not yet decided" — never as a delivery failure.The balance endpoint is free to call and never consumes a credit — perfect for a pre-flight check before verifying a large list.
resp, err := client.Get("https://api.validemail.net/balance?token=" + apiKey)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var balance struct {
Balance int `json:"balance"`
}
if err := json.NewDecoder(resp.Body).Decode(&balance); err != nil {
panic(err)
}
fmt.Println("Credits remaining:", balance.Balance)
| HTTP Status | Meaning | What to do |
|---|---|---|
| 200 OK | The request was processed. Check State — a result with State = "Unknown" and a RetryAfterSeconds value is an interim answer, not a final verdict. |
Use the result. If State is Unknown, retry the same request after RetryAfterSeconds. |
| 400 Bad Request | Missing email/token parameter, or a transient processing failure. |
Check the response text and retry. |
| 402 Payment Required | No credits remaining. The JSON body says what is missing and what fixes it: {"error":"no_credits","shortfall":1,"pack":{"credits":5000,"price":9.50,"currency":"USD"},"buyUrl":"https://validemail.net/BuyCredits?credits=5000","message":"…"}. |
Stop sending requests, open buyUrl to top up (the pack is preselected), then retry. Need an invoice or a larger volume? Email info@validemail.net. |
| 401 Unauthorized | The API key is invalid or unknown. | Verify the token value against the API key shown in your dashboard. |
| 403 Forbidden | The account attached to this API key is inactive. | Contact info@validemail.net to reactivate the account. |
| 429 Too Many Requests | You exceeded your per-second request limit. The response body includes the configured limit. | Slow down and retry with backoff, or contact us to raise your rate limit. |
Explore every response field and status code in the full API reference.
Hi. I'm ValidEmail's AI assistant. Ask me about verifying a list, the API, prices or your account.