Go Examples
Go examples for integrating the nofakemails email validation API using the standard library net/http package.
No External Dependencies
These examples use only the Go standard library. Set your API key:
export NOFAKEMAILS_API_KEY=your_api_keySingle Email Validation
Validate a single email address with a GET request to the /v1/email/{email} endpoint.
main.go
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
)
func main() {
email := "user@example.com"
apiKey := os.Getenv("NOFAKEMAILS_API_KEY")
encodedEmail := url.PathEscape(email)
apiURL := fmt.Sprintf("https://api.nofakemails.com/v1/email/%s", encodedEmail)
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return
}
req.Header.Set("X-API-Key", apiKey)
resp, err := (&http.Client{}).Do(req)
if err != nil {
fmt.Printf("Error making request: %v\n", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response: %v\n", err)
return
}
if resp.StatusCode != http.StatusOK {
fmt.Printf("API error (status %d): %s\n", resp.StatusCode, string(body))
return
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
fmt.Printf("Error parsing JSON: %v\n", err)
return
}
fmt.Printf("Email: %s\n", result["email"])
fmt.Printf("Valid: %v\n", result["valid"])
fmt.Printf("Disposable: %v\n", result["disposable"])
}URL Encoding
Always use
url.PathEscape() to encode the email address. This ensures special characters like + are properly handled.Response:
{
"email": "user@example.com",
"valid": true,
"normalized": "user@example.com",
"local_part": "user",
"alias": false,
"ascii": true,
"role": null,
"gibberish": false,
"gibberish_score": 0.12,
"domain": "example.com",
"mx": true,
"disposable": false,
"public": false,
"relay": false,
"typo": false,
"typo_suggestion": null
}See the Response Format page for a full description of every field.
Batch Validation
Validate up to 64 email addresses in a single POST request to the /v1/emails endpoint.
batch.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
type BatchRequest struct {
Emails []string `json:"emails"`
}
type BatchResponse struct {
Success bool `json:"success"`
Partial bool `json:"partial"`
Total int `json:"total"`
ValidCount int `json:"valid_count"`
InvalidCount int `json:"invalid_count"`
ErrorCount int `json:"error_count"`
UsagePoints int `json:"usage_points"`
ProcessingTimeMs int `json:"processing_time_ms"`
Results []BatchResult `json:"results"`
}
type BatchResult struct {
Email string `json:"email"`
Status string `json:"status"`
Result map[string]interface{} `json:"result"`
Error *BatchError `json:"error"`
}
type BatchError struct {
Code string `json:"code"`
Message string `json:"message"`
}
func main() {
apiKey := os.Getenv("NOFAKEMAILS_API_KEY")
emails := []string{
"user1@example.com",
"user2@gmail.com",
"test@tempmail.com",
}
reqBody := BatchRequest{Emails: emails}
jsonBody, err := json.Marshal(reqBody)
if err != nil {
fmt.Printf("Error marshaling request: %v\n", err)
return
}
req, err := http.NewRequest("POST", "https://api.nofakemails.com/v1/emails", bytes.NewBuffer(jsonBody))
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return
}
req.Header.Set("X-API-Key", apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := (&http.Client{}).Do(req)
if err != nil {
fmt.Printf("Error making request: %v\n", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response: %v\n", err)
return
}
if resp.StatusCode != http.StatusOK {
fmt.Printf("API error (status %d): %s\n", resp.StatusCode, string(body))
return
}
var batch BatchResponse
if err := json.Unmarshal(body, &batch); err != nil {
fmt.Printf("Error parsing response: %v\n", err)
return
}
// Check for partial results (timeout)
if batch.Partial {
fmt.Println("Warning: some emails may not have been processed")
}
fmt.Printf("Total: %d, Valid: %d\n", batch.Total, batch.ValidCount)
for _, item := range batch.Results {
if item.Status == "error" {
fmt.Printf("%s: ERROR - %s\n", item.Email, item.Error.Message)
continue
}
fmt.Printf("%s: valid=%v, disposable=%v\n",
item.Email, item.Result["valid"], item.Result["disposable"])
}
}Response:
{
"success": true,
"partial": false,
"total": 3,
"valid_count": 3,
"invalid_count": 0,
"error_count": 0,
"usage_points": 3,
"processing_time_ms": 198,
"results": [
{
"email": "user1@example.com",
"status": "success",
"result": {
"email": "user1@example.com",
"valid": true,
"normalized": "user1@example.com",
"local_part": "user1",
"alias": false,
"ascii": true,
"role": null,
"gibberish": false,
"gibberish_score": 0.11,
"domain": "example.com",
"mx": true,
"disposable": false,
"public": false,
"relay": false,
"typo": false,
"typo_suggestion": null
},
"error": null
},
{
"email": "user2@gmail.com",
"status": "success",
"result": {
"email": "user2@gmail.com",
"valid": true,
"normalized": "user2@gmail.com",
"local_part": "user2",
"alias": false,
"ascii": true,
"role": null,
"gibberish": false,
"gibberish_score": 0.10,
"domain": "gmail.com",
"mx": true,
"disposable": false,
"public": true,
"relay": false,
"typo": false,
"typo_suggestion": null
},
"error": null
},
{
"email": "test@tempmail.com",
"status": "success",
"result": {
"email": "test@tempmail.com",
"valid": true,
"normalized": "test@tempmail.com",
"local_part": "test",
"alias": false,
"ascii": true,
"role": null,
"gibberish": false,
"gibberish_score": 0.10,
"domain": "tempmail.com",
"mx": true,
"disposable": true,
"public": false,
"relay": false,
"typo": false,
"typo_suggestion": null
},
"error": null
}
]
}Each item in results contains the full validation result. See the Batch Validation response format for details on the envelope fields and per-email result structure.
Batch Limits
Maximum 64 emails per batch request. Duplicates are automatically deduplicated. See the Error Codes page if you encounter issues.