Rust Examples
Integrate nofakemails email validation into your Rust applications using reqwest.
Dependencies
Add the following to your
Cargo.toml:Cargo.toml
[dependencies]
reqwest = { version = "0.12", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }Single Email Validation
Validate a single email address with a GET request to the /v1/email/{email} endpoint.
main.rs
use reqwest::header::{HeaderMap, HeaderValue};
use serde_json::Value;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("NOFAKEMAILS_API_KEY")
.expect("NOFAKEMAILS_API_KEY must be set");
let email = "user@example.com";
let url = format!("https://api.nofakemails.com/v1/email/{}", email);
let mut headers = HeaderMap::new();
headers.insert("X-API-Key", HeaderValue::from_str(&api_key)?);
let client = reqwest::Client::new();
let response = client
.get(&url)
.headers(headers)
.send()
.await?;
if response.status().is_success() {
let result: Value = response.json().await?;
println!("{}", serde_json::to_string_pretty(&result)?);
} else {
eprintln!("Error: {}", response.status());
}
Ok(())
}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.rs
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::env;
#[derive(Serialize)]
struct BatchRequest {
emails: Vec<String>,
}
#[derive(Deserialize, Debug)]
struct BatchResponse {
success: bool,
partial: bool,
total: u32,
valid_count: u32,
invalid_count: u32,
error_count: u32,
usage_points: u32,
processing_time_ms: u64,
results: Vec<BatchResult>,
}
#[derive(Deserialize, Debug)]
struct BatchResult {
email: String,
status: String,
result: Option<Value>,
error: Option<BatchError>,
}
#[derive(Deserialize, Debug)]
struct BatchError {
code: String,
message: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = env::var("NOFAKEMAILS_API_KEY")
.expect("NOFAKEMAILS_API_KEY must be set");
let emails = vec![
"user1@example.com".to_string(),
"user2@gmail.com".to_string(),
"test@tempmail.com".to_string(),
];
let mut headers = HeaderMap::new();
headers.insert("X-API-Key", HeaderValue::from_str(&api_key)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
let client = reqwest::Client::new();
let response = client
.post("https://api.nofakemails.com/v1/emails")
.headers(headers)
.json(&BatchRequest { emails })
.send()
.await?;
if !response.status().is_success() {
eprintln!("Error: {}", response.status());
return Ok(());
}
let batch: BatchResponse = response.json().await?;
// Check for partial results (timeout)
if batch.partial {
eprintln!("Warning: some emails may not have been processed");
}
println!("Total: {}, Valid: {}", batch.total, batch.valid_count);
for item in &batch.results {
if item.status == "error" {
if let Some(err) = &item.error {
eprintln!("{}: ERROR - {}", item.email, err.message);
}
continue;
}
if let Some(result) = &item.result {
println!("{}: valid={}, disposable={}",
item.email, result["valid"], result["disposable"]);
}
}
Ok(())
}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.