Integrate Autheona using Rust

Server-side Rust integration guide for Autheona Trust Verification API with copy-paste ready code using standard library.

Prerequisites

Before using this code, you need:

  1. An Autheona account: Sign up at https://app.autheona.com/sign-up
  2. A project (Sandbox or Production): See project creation guide
  3. An access token with API permissions: See access token documentation

Store your access token securely using environment variables. Never hardcode tokens in your source code.

Code

This function sends an HTTP POST request to the Autheona API with the email address to validate. It accepts an optional custom_policy parameter for testing validation rules. The function returns the API response as JSON containing the validation result and email intelligence data.

use std::collections::HashMap;
use std::env;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::str;

pub fn validate_email(
    email: &str,
    api_key: &str,
    custom_policy: Option<&str>,
) -> Result<String, Box<dyn std::error::Error>> {
    let email = email.trim().to_lowercase();
    
    let mut payload = format!(r#"{{"email_address":"{}"}}"#, email);
    if let Some(policy) = custom_policy {
        payload = format!(r#"{{"email_address":"{}","custom_policy":{}}}"#, email, policy);
    }
    
    let host = "api.autheona.com";
    let request = format!(
        "POST /v1/intelligence HTTP/1.1\r\n\
         Host: {}\r\n\
         Content-Type: application/json\r\n\
         x-api-key: {}\r\n\
         Content-Length: {}\r\n\
         Connection: close\r\n\r\n\
         {}",
        host,
        api_key,
        payload.len(),
        payload
    );
    
    let mut stream = TcpStream::connect(format!("{}:443", host))?;
    
    #[cfg(feature = "native-tls")]
    {
        use native_tls::TlsConnector;
        let connector = TlsConnector::new()?;
        let stream = connector.connect(host, stream)?;
        let mut stream = stream;
        stream.write_all(request.as_bytes())?;
        
        let mut response = Vec::new();
        stream.read_to_end(&mut response)?;
        
        let response_str = String::from_utf8_lossy(&response);
        let body_start = response_str.find("\r\n\r\n").unwrap_or(0) + 4;
        let body = &response_str[body_start..];
        
        if response_str.contains("HTTP/1.1 200") {
            Ok(body.to_string())
        } else {
            Err(format!("HTTP error: {}", response_str.lines().next().unwrap_or("")).into())
        }
    }
    
    #[cfg(not(feature = "native-tls"))]
    {
        stream.write_all(request.as_bytes())?;
        
        let mut response = Vec::new();
        stream.read_to_end(&mut response)?;
        
        let response_str = String::from_utf8_lossy(&response);
        let body_start = response_str.find("\r\n\r\n").unwrap_or(0) + 4;
        let body = &response_str[body_start..];
        
        Ok(body.to_string())
    }
}

Usage

Import the function, provide your API key via environment variable, and call validate_email with an email address. The function returns a JSON string with action field ('allow', 'block', or 'ignore') and detailed email analysis. For testing specific rules, pass a custom policy as the third argument.

use std::env;

fn main() {
    let api_key = env::var("AUTHEONA_API_KEY").expect("API key not set");
    
    // Basic validation
    match validate_email("user@example.com", &api_key, None) {
        Ok(result) => println!("{}", result),
        Err(e) => eprintln!("Error: {}", e),
    }
    
    // With custom policy for testing
    let custom_policy = r#"{
        "parent_rules": [
            {"action": "block", "field": "domain_disposable", "operator": "==", "target": {"bool": true}}
        ],
        "stop_on_block": true,
        "version": "1.0"
    }"#;
    
    match validate_email("user@tempmail.com", &api_key, Some(custom_policy)) {
        Ok(result) => println!("{}", result),
        Err(e) => eprintln!("Error: {}", e),
    }
}

Note

For HTTPS in production, add native-tls or rustls to your Cargo.toml dependencies.

Custom Policy Parameter

Warning

The custom_policy parameter is for testing only. In production, configure policies using the Policy Editor instead of sending them in requests.

Add custom_policy to your request body to test custom validation rules:

let custom_policy = r#"{
  "parent_rules": [
    {"action": "block", "field": "domain_disposable", "operator": "==", "target": {"bool": true}},
    {"action": "block", "field": "email_deliverable", "operator": "==", "target": {"bool": false}},
    {"action": "ignore", "field": "domain_free", "operator": "==", "target": {"bool": true}}
  ],
  "stop_on_block": true,
  "version": "1.0"
}"#;

Available fields: domain_disposable, domain_free, email_deliverable, domain_has_website, email_has_fraud_pattern, and more from the API response.

Environment Variables

Set your API key as an environment variable to keep it out of your code:

export AUTHEONA_API_KEY="your-api-key"

Note

This is a minimal integration example. For production use cases, error handling, retry logic, rate limiting, and advanced response processing, refer to the API Reference Documentation.

Support

For issues with the API, refer to: