# Rust SDK

Add urouter-sdk to a Cargo project using Rust 1.86 or later. Call the async client from a Tokio runtime.

[Source on GitHub](https://github.com/uRouter/urouter-rust)

[SDK examples](https://github.com/uRouter/urouter-rust/tree/v0.1.0/examples)

## Install the SDK

```sh
cargo add urouter-sdk --git https://github.com/uRouter/urouter-rust.git --tag v0.1.0
cargo add tokio --features macros,rt-multi-thread
```

## Send a text request

Create an inference key in Developer → API Keys. Eligible calls use Plan usage first, then paid credit. Keep the key in UROUTER_API_KEY on your server. The clients connect to https://api.urouter.net/v1 by default.

Choose an available text model from Models & Pricing and set UROUTER_MODEL to its exact ID. Review the model’s required credit for a 256-token output limit before running the example.

[Models & Pricing](/developers/catalog/models)

Set UROUTER_OPERATION_ID to a new UUID for this request and save it with your work. Run the example once to send a paid Responses request and print the result.

src/main.rs

```rust
use std::env;
use urouter_sdk::{Client, RequestOptions, ResponseRequest};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::from_env()?;
    let mut request = ResponseRequest::new(
        env::var("UROUTER_MODEL")?, "Explain DNS in one sentence.",
    );
    request.max_output_tokens = Some(256);
    let options = RequestOptions::default()
        .with_idempotency_key(env::var("UROUTER_OPERATION_ID")?);
    let reply = client.create_response(&request, &options).await?;
    if let Some(id) = &reply.request_id {
        println!("Request ID: {id}");
    }
    println!("{}", reply.value.output_text());
    Ok(())
}
```

```sh
cargo run
```

## Recover a request

Keep the HTTP request ID (req_…) and the operation’s idempotency key. If a stream disconnects or the outcome is unclear, look up the original request. Read its execution and financial states separately before deciding whether to send another request.

```rust
let status = client.request_status(request_id).await?;
```

## Handle an agent’s tool call

Declare the functions your app supports. When the model returns a function_call, check its name and arguments, apply your app’s approval policy, then run the approved action locally. Send a function_call_output with the same call_id. Each continuation is a new inference request.

[Run tools in your client](/developer/api-docs/responses#responses_tools)

[Example with approve, edit, and cancel decisions](https://github.com/uRouter/urouter-rust/blob/v0.1.0/examples/tools.rs)

- [Models & pricing](/developer/api-docs/models)
- [Responses](/developer/api-docs/responses)
- [Errors & recovery](/developer/api-docs/errors)
- [Reseller API](/developer/resellers/api)
