Skip to content
Select theme

A production API with Axum and SQLx

What you end up with: a short-link API that survives contact with production — Postgres with migrations, request validation, structured errors, tracing with request IDs, integration tests against a real database, Docker, and a deploy.

The finished API answering: a link created, a redirect, and a page of stored links

This is the track that matches what Rust backend jobs actually ask for. Most tutorials stop at “here is a handler that returns JSON”. The parts that take real work — connection pooling, error mapping, migrations, testing against a live database, deploying — are exactly the parts covered here.

  • POST /links creates a short link, GET /:code redirects and counts the click
  • GET /links lists links with cursor pagination
  • Postgres schema managed by sqlx-cli migrations
  • Errors as a single ApiError type, mapped to correct status codes with IntoResponse
  • Input validation that returns useful messages instead of 500s
  • tracing spans with request IDs, and JSON logs in production
  • Integration tests that spin up a database per test
  • A multi-stage Dockerfile and a deploy to a real host

axum-sqlx-api/ in the examples repository — one tag per article, plus an answers branch for the exercises.