What I Learned: Baby Steps to OpenRouter

C…
crusty.rustacean
Published on November 28, 2025

A short piece documenting the progress I made to understand how to use the OpenRouter API.

notes
programming rust openrouter reqwest http-client
0 likes

This is a quick bite, mostly a reference for myself in the future. This evening I took some baby steps to using OpenRouter instead of going to the providers directly…and spending more money than I need to.

This is what I came up with, using the reqwest crate:

// src/main.rs

// dependencies
use reqwest::Client;
use serde_json::json;

// constants
const OPENROUTER_API_KEY: &str =
    "<your-api-token-here>";
const OPENROUTER_URL: &str = "https://openrouter.ai/api/v1/chat/completions";

#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
    let http_client = Client::new();

    let body = json! {{
        "model": "anthropic/claude-sonnet-4.5",
        "messages": [
            {
                "role": "user",
                "content": "How does the Turbo Fish operator in Rust work? Please provide an example of how to use it to deserialize a response from an API."
            }
        ]
    }};

    let response = http_client
        .post(OPENROUTER_URL)
        .header("Authorization", format!("Bearer {}", OPENROUTER_API_KEY))
        .body(body.to_string())
        .send()
        .await?;

    let body = response.text().await?;

    println!("{}", body);

    Ok(())
}

Not super useful, but it’s a start. Will embellish this in the coming days…likely after Advent of Code though, want to give that my all for the first half of December.

C…
crusty.rustacean

Comments

Loading comments...