SerpBase
Back to Blog

Google Search API in Rust: Results as JSON With reqwest and serde

A complete Rust program that POSTs a Google search and deserializes organic results with serde. Tested 5 December 2026 with reqwest 0.12.

December 5, 2026
By SerpBase Team3 min read

Short answer: in Rust, POST a JSON body to https://api.serpbase.dev/google/search with reqwest, send your key in the X-API-Key header, and deserialize the answer into a serde struct. No Google Cloud project or HTML parsing; the program below is about 50 lines.

Dependencies

[dependencies]
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"

rustls-tls avoids a dependency on the system OpenSSL. Use the async client instead of blocking if your program already runs on Tokio; the request is the same.

The program

use serde::Deserialize;
use serde_json::json;
use std::time::Duration;

#[derive(Debug, Deserialize)]
struct SearchResponse {
    status: i64,
    error: Option<String>,
    credits_charged: Option<u32>,
    #[serde(default)]
    organic: Vec<Organic>,
}

#[derive(Debug, Deserialize)]
struct Organic {
    rank: u32,
    title: String,
    link: String,
    snippet: Option<String>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let key = std::env::var("SERPBASE_API_KEY")?;
    let client = reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(60))
        .build()?;

    let resp: SearchResponse = client
        .post("https://api.serpbase.dev/google/search")
        .header("X-API-Key", key)
        .json(&json!({ "q": "rust web framework", "gl": "us", "hl": "en" }))
        .send()?
        .json()?;

    if resp.status != 0 {
        return Err(format!("status {}: {}", resp.status, resp.error.unwrap_or_default()).into());
    }

    println!("credits charged: {}", resp.credits_charged.unwrap_or(0));
    for r in &resp.organic {
        println!("{:>2}. {}", r.rank, r.title);
        println!("    {}", r.link);
        if let Some(s) = &r.snippet {
            println!("    {}", s.chars().take(100).collect::<String>());
        }
    }
    Ok(())
}

Fields you do not declare are ignored by serde, so the struct can stay small and grow when you need more (date, people_also_ask, ai_overview, …). Mark anything that is not always present as Option<…> or #[serde(default)].

What it printed

We sent this request on 5 December 2026 and ran the program against the recorded answer (Rust 1.100, reqwest 0.12). The search took 5.8 seconds and returned 9 organic results, plus an AI Overview and 4 People Also Ask questions:

credits charged: 1
 1. Top 11 Web Frameworks for Rust
    https://m.youtube.com/watch?v=1nsnVPdalE4
    Top 11 Web Frameworks for Rust. Rust GUI Explained: egui vs Iced vs Tauri vs GPUI
 2. Top 10 Rust Frameworks for Building Web Applications
    https://rust.guide/article/Top_10_Rust_Frameworks_for_Building_Web_Applications.html
    Rocket Rocket is a web framework for Rust that is designed to be fast, secure, and easy to use. It i
 3. Rust Frameworks
    https://dev.to/yellow_coder/rust-frameworks-3p3
    Rust Frameworks ; Actix-web, Excellent, Moderate ; Rocket, Very Good, Easy ; Axum, Excellent, Modera

Things that trip people up

  • Check status, not only the HTTP code. Search failures come back as HTTP 200 with a non-zero status and an error message; the program above turns them into an Err. The codes are listed in SERP API error codes. Failed requests are not charged.
  • Some links are Google redirect links. In this run 6 of the 9 link values were https://www.google.com/goto?url=… rather than the site's own address. Pass them to the free Google link resolver if you need the real URL.
  • Set a client timeout. 60 seconds is safe. If you need a shorter limit, also send the timeout field (1–30 seconds) so the request ends cleanly and uncharged.
  • Keep the key out of the source. The program reads it from SERPBASE_API_KEY.

Related

Cost and docs

A Google search is 1 credit, $0.25–$0.50 per 1,000 depending on the pack. New accounts get 500 free credits.