Rust icon

Rust Quick Start Guide

Get started with FacePing's face recognition API using Rust

Prerequisites

Installation

Create a new Rust project:

cargo new faceping-demo
cd faceping-demo

Update Cargo.toml with required dependencies:

[package]
name = "faceping-demo"
version = "0.1.0"
edition = "2021"

[dependencies]
reqwest = { version = "0.11", features = ["multipart", "json"] }
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
dotenv = "0.15"

Step 1: Create Face Group

Update src/main.rs:

use dotenv::dotenv;
use std::env;

async fn create_group(group_name: &str) -> Result<(), Box> {
    dotenv().ok();
    let api_key = env::var("FACEPING_API_KEY")?;
    let client = reqwest::Client::new();

    let response = client
        .post(format!("https://api.faceping.ai/groups/{}", group_name))
        .header("Authorization", format!("Bearer {}", api_key))
        .send()
        .await?;

    if response.status().is_success() {
        println!("Group created successfully");
    } else {
        println!("Error creating group: {}", response.status());
    }

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box> {
    create_group("my-group").await?;
    Ok(())
}

Step 2: Upload Face Image

Add to src/main.rs:

use reqwest::multipart;
use std::path::Path;
use tokio::fs::File;
use tokio::io::AsyncReadExt;

async fn upload_face(group_name: &str, image_path: &str) -> Result<(), Box> {
    dotenv().ok();
    let api_key = env::var("FACEPING_API_KEY")?;
    let client = reqwest::Client::new();

    // Read image file
    let mut file = File::open(image_path).await?;
    let mut buffer = Vec::new();
    file.read_to_end(&mut buffer).await?;

    // Create multipart form
    let form = multipart::Form::new()
        .part("image", multipart::Part::bytes(buffer)
            .file_name(Path::new(image_path).file_name()
                .unwrap()
                .to_string_lossy()
                .into_owned()));

    let response = client
        .post(format!("https://api.faceping.ai/groups/{}/faces", group_name))
        .header("Authorization", format!("Bearer {}", api_key))
        .multipart(form)
        .send()
        .await?;

    if response.status().is_success() {
        println!("Face uploaded successfully");
    } else {
        println!("Error uploading face: {}", response.status());
    }

    Ok(())
}

Step 3: Search for Faces

Add to src/main.rs:

#[derive(Debug, serde::Deserialize)]
struct Match {
    score: f64,
}

#[derive(Debug, serde::Deserialize)]
struct SearchResponse {
    matches: Vec,
}

async fn search_faces(group_name: &str, image_path: &str) -> Result<(), Box> {
    dotenv().ok();
    let api_key = env::var("FACEPING_API_KEY")?;
    let client = reqwest::Client::new();

    // Read image file
    let mut file = File::open(image_path).await?;
    let mut buffer = Vec::new();
    file.read_to_end(&mut buffer).await?;

    // Create multipart form
    let form = multipart::Form::new()
        .part("image", multipart::Part::bytes(buffer)
            .file_name(Path::new(image_path).file_name()
                .unwrap()
                .to_string_lossy()
                .into_owned()));

    let response = client
        .post(format!("https://api.faceping.ai/groups/{}/search", group_name))
        .header("Authorization", format!("Bearer {}", api_key))
        .multipart(form)
        .send()
        .await?;

    if response.status().is_success() {
        let search_result: SearchResponse = response.json().await?;
        println!("Found {} matches", search_result.matches.len());
        
        for match_result in search_result.matches {
            println!("Match Score: {}", match_result.score);
        }
    } else {
        println!("Error searching faces: {}", response.status());
    }

    Ok(())
}

Security Notes

  • Images are immediately discarded after vector conversion
  • Face vectors cannot be reverse-engineered into images
  • Always use HTTPS for all API calls
  • Store your API keys in environment variables (.env file)

Ready to get started?

Take a look at the API documentation
API docs