# Axum

> Builds HTTP APIs in Rust with Axum: routing, extractors, shared state, middleware, and testing with tower. Use when working with project setup, serve and test, api or when the user mentions project setup, serve and test, api.

- Skill: `opknock/axum` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/axum`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/axum/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/axum

---


Builds HTTP APIs in Rust with Axum: routing, extractors, shared state, middleware, and testing with tower.

## Agentic Workflow: Read -> Reason -> Act (axum)

You are **Axum** (api/general) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.

### 1. Read — api context for `axum`
- Domain: Builds HTTP APIs in Rust with Axum: routing, extractors, shared state, middleware, and testing with tower.
- **project-setup**: Create an Axum project and add dependencies. — `cargo new axum-api`
- **serve-and-test**: Run the server and test routes with tower ServiceExt. — `cargo run`
- Check `knowledge` and `prerequisites: cargo`

### 2. Reason — think for `axum`
- For `project-setup`: Create an Axum project and add dependencies. — decide which checks to run
- For `serve-and-test`: Run the server and test routes with tower ServiceExt. — decide which checks to run
- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs

### 3. Act — execute with `axum` tools
- Tools: `Glob`, `Grep`, `Read`, `Bash` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for any write; record evidence (paths, checksums)
- Fingerprint: `axum:04a32048`

# Axum

## What this skill does

Builds HTTP APIs in Rust with Axum: routing, extractors, shared state, middleware (CORS/tracing), and route testing with tower.

## When to use

- A new Rust REST API with tokio
- Adding extractors, state, or middleware to an existing app
- Fast, compile-time-safe routing

## Real commands

```bash
cargo new axum-api
cargo add axum tokio --features tokio/full
cargo add serde --features derive
cargo add tower-http --features cors,trace

cargo run
curl -s http://localhost:3000/health

cargo test
cargo clippy -- -D warnings
cargo build --release
```

## Minimal server

```rust
use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/health", get(|| async { "ok" }));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
```

## State + extractors

```rust
#[derive(Clone)]
struct Db { pool: String }

let app = Router::new()
    .route("/users/:id", get(get_user))
    .with_state(Db { pool: "postgres://db".into() });

async fn get_user(State(db): State<Db>, Path(id): Path<u64>) -> String {
    format!("user {id} via {}", db.pool)
}
```

## Testing

- Test routers with tower::ServiceExt::oneshot
- Use `cargo test` and clippy in CI

## Best practices

- Share dependencies via state, not globals
- Use path parameters and typed extractors instead of manual parsing
- Layer CORS via tower-http, not hand-rolled middleware

## Capabilities

### project-setup
Create an Axum project and add dependencies.

**Parameters:**
- `features` (string): Cargo features for axum or tower-http

**Commands:**
- `cargo new axum-api`
- `cargo add axum tokio --features tokio/full`
- `cargo add serde --features derive`
- `cargo add tower-http --features cors`
- `cargo build`

**Examples:**
- cargo new axum-api && cargo add axum tokio --features tokio/full
- cargo add axum --features multipart,ws
- cargo add tower-http --features trace,cors

### serve-and-test
Run the server and test routes with tower ServiceExt.

**Parameters:**
- `port` (number): Bind port (default 3000)
- `addr` (string): Bind address

**Commands:**
- `cargo run`
- `curl -s http://localhost:3000/health`
- `cargo test`
- `cargo clippy -- -D warnings`
- `cargo build --release`

**Examples:**
- cargo run && curl -s http://localhost:3000/health
- cargo test -- --nocapture
- cargo clippy --all-targets -- -D warnings

## References
- [Axum Docs (docs.rs)](https://docs.rs/axum/latest/axum/)
- [Axum README](https://github.com/tokio-rs/axum)
- [tower-http](https://docs.rs/tower-http)

