API Client Macro
A procedural macro for generating type-safe API clients in Rust.
Install
The crate remains published as http-provider-macro, and the recommended macro is api_client!.
You'll also need these dependencies:
Usage
use api_client;
use ;
api_client!;
async
Endpoint Fields
Required:
method: HTTP method (GET,POST,PUT,DELETE,PATCH)
Optional:
path: URL path (e.g.,"/users/{id}")res: Response type (defaults to())req: Request body typepath_params: Type for path parametersquery_params: Type for query parametersheaders: Header type (e.g.,reqwest::header::HeaderMap)fn_name: Custom function nameretries: Retry count for this endpoint (overrides global)
Auth
Add automatic authentication to every request. Three strategies are supported:
// Bearer token — injects `Authorization: Bearer <token>`
api_client!;
let client = new;
// Basic auth — injects `Authorization: Basic <base64>`
api_client!;
let client = new;
// API key — injects a custom header
api_client!;
let client = new;
Omitting auth keeps the original new(url, timeout) constructor (backward compatible). Auth works with all other features including retries.
Retry with Backoff
Set a global retry count that applies to all endpoints. Retries use exponential backoff (100ms base, 2x multiplier, 5s cap) and trigger on 5xx errors and request timeouts. 4xx errors are never retried.
api_client!;
Per-endpoint retries overrides the global value. Omitting retries entirely means no retries (backward compatible).
Generated Code
The macro generates:
- A struct with
new(url, timeout)constructor - An async method for each endpoint
- A trait (
{Name}Trait) for mocking in tests - An error enum (
{Name}Error) with variants for URL, request, HTTP, and deserialization errors
Examples
See the examples/ directory:
basic.rs- Simple GET requestsparams.rs- Path and query parametersadvanced.rs- All featuresmocking.rs- Testing with generated traitsmultiple_path_params.rs- Nested resources
License
MIT OR Apache-2.0