last update
May 23, 2026
license
MITv1.0.0
format
SKILL.md
- Install
// Downloads SKILL.md into ~/.claude/skills/rust-axum-api/
$ curl -fsSL --create-dirs -o ~/.claude/skills/rust-axum-api/SKILL.md https://claudeskil.com/skills/rust-axum-api/SKILL.md - Invoke
// Inside Claude Code, type the skill name as a slash command
> /rust-axum-api scaffold a typed REST endpoint - Or just ask
// Claude loads the skill by itself when a request matches its description
> scaffold a typed REST endpoint
- name:
- Rust Axum API
- slug:
- rust-axum-api
- version:
- v1.0.0
- license:
- MIT
- author:
- @chiragthummar16
- categories:
- tags:
- #rust#axum#tokio#sqlx#performance
- description:
High-performance Rust APIs with Axum + Tokio + sqlx: typed routes, extractors, async DB, deploy small.
// What you can do with it
- A Router built with .route("/path", get(handler)) and merged sub-routers.
- Handlers using real extractors — Path, Query, Json, and State — returning Result, AppError> where AppError implements IntoResponse.
- A shared PgPool created via PgPoolOptions::new().connect(&url), stored in State and cloned cheaply per request.
- Compile-time-checked queries with the sqlx::query! / query_as! macros (backed by sqlx-cli and a .sqlx offline cache).
- tower-http layers: TraceLayer, CorsLayer, and timeout/compression as needed.
- A migrations/ folder runnable with sqlx migrate run, and a distroless/scratch Dockerfile.
README.md
Rust Axum API
Scaffold production-ready HTTP services in Rust using Axum, Tokio, and sqlx. This skill generates a working project with typed handlers, tower middleware wired up, an async Postgres pool, and a multi-stage Dockerfile that produces a tiny statically-linked image. It's aimed at developers who want the performance and type-safety of Rust for backend services without spending a day assembling boilerplate from blog posts.
When to use it
- Starting a new JSON API or microservice and you want sqlx + Axum set up correctly the first time.
- Adding routes to an existing Axum app and you want consistent extractor/handler signatures.
- You need a small container image for cost-sensitive deploys (Fly.io, Cloud Run, a cheap VPS).
- Replacing a Node/Python service where p99 latency or memory footprint matters.
What it does
The skill lays down a Cargo.toml with axum, tokio (with the macros and rt-multi-thread features), sqlx (runtime-tokio-rustls, postgres, macros), tower, tower-http, and serde. It generates:
- A
Routerbuilt with.route("/path", get(handler))and merged sub-routers. - Handlers using real extractors —
Path<T>,Query<T>,Json<T>, andState<AppState>— returningResult<Json<T>, AppError>whereAppErrorimplementsIntoResponse. - A shared
PgPoolcreated viaPgPoolOptions::new().connect(&url), stored inStateand cloned cheaply per request. - Compile-time-checked queries with the
sqlx::query!/query_as!macros (backed bysqlx-cliand a.sqlxoffline cache). tower-httplayers:TraceLayer,CorsLayer, and timeout/compression as needed.- A
migrations/folder runnable withsqlx migrate run, and a distroless/scratch Dockerfile.
Usage
curl -fsSL --create-dirs -o ~/.claude/skills/rust-axum-api/SKILL.md https://claudeskil.com/skills/rust-axum-api/SKILL.md
# then, in your project
cargo sqlx prepare # build the offline query cache
cargo run # serves on 0.0.0.0:3000
docker build -t my-api . # ~20MB final image
A generated handler looks like:
async fn get_user(
State(pool): State<PgPool>,
Path(id): Path<i64>,
) -> Result<Json<User>, AppError> {
let user = sqlx::query_as!(User, "SELECT id, name FROM users WHERE id = $1", id)
.fetch_one(&pool)
.await?;
Ok(Json(user))
}
Tips & gotchas
query!macros hit the database at compile time. SetDATABASE_URLor commit the.sqlxcache (cargo sqlx prepare) so CI/Docker builds don't need a live DB.- Extractor order matters: body-consuming extractors like
Jsonmust be the last argument in a handler. - For the tiny image, build with the
x86_64-unknown-linux-musltarget so the binary runs onscratch/distroless without glibc. - Add
TraceLayerlast in your layer stack so it wraps everything beneath it.
FAQ
Does it use SeaORM or Diesel? No — it uses sqlx with raw, compile-checked SQL. You write SQL, sqlx verifies it against your schema.
How is the image only ~20MB? A musl static build copies a single binary into a scratch base; there's no runtime, interpreter, or OS layer.
Can I add auth/middleware? Yes — write a tower layer or an extractor (e.g. a FromRequestParts impl for a Claims type) and attach it with .layer() or per-route.
$ cat reviews/