Build a Rust REST API with Axum and SQLite

Example tested on with Miblo 0.9.1 · NaSuRall

Create a REST API with Rust, Axum and SQLite: generate a User model and CRUD routes, then test a POST request with Miblo.

▶ Watch the demo
On this page

Before you start

This tutorial creates a local API with a users table, five CRUD endpoints and a SQLite database. Miblo CLI generates editable Axum and SQLx source code.

You need Rust 1.88 or later, Cargo, rustfmt and your platform’s native build tools. Prepare your environment → SQLite needs no database server or Docker. An Internet connection is needed for the first template and Rust dependency downloads.

shell
rustup component add rustfmt
cargo install miblo --version 0.9.1 --locked
miblo --version

Scaffolding is quick; installation and the first Rust build can take several minutes. Use a new directory and an available port 3000.

1. Create the project

shell
miblo
  1. Press n and enter team-api.
  2. Choose a directory that does not exist yet, then port 3000.
  3. Select SQLite — local file, automatic setup.
  4. Choose GitHub Releases, then Create project.

Miblo creates the project and miblo.sqlite. The database status reads Migrations up to date.

2. Add User

  1. Press a and enter User.
  2. Add name, type Short text (255 characters), with no options.
  3. Select Add another field: email, also short text. Toggle Unique with Space.
  4. Select Finish and generate, accept /users, then Generate.

Miblo creates the users table with an id primary key, Rust code and five CRUD routes. With the API stopped, the new SQLite model migration is applied automatically.

3. Send a POST

  1. Press r and wait for API running.
  2. Use Tab to reach Routes, select POST /users, then press x.
  3. The fields are prefilled. Press Enter to edit a value.
  4. Select Send request and press Enter. The response is HTTP 201.
  5. Test GET /users to retrieve the created row.

s stops the API. The first Rust build takes longer than subsequent starts.

The same steps in the shell

shell
miblo new team-api --database sqlite --yes
cd team-api
miblo add User name:string email:string:unique --yes
miblo db init
miblo run

Open miblo in a second terminal inside team-api to test routes. An API started elsewhere is identified as external.

Generated routes and responses

MethodRouteResult
POST/usersCreates a user: HTTP 201 and its JSON.
GET/usersPaginated list: HTTP 200 and a JSON array.
GET/users/{id}Reads a user: HTTP 200, or 404 if missing.
PATCH/users/{id}Updates the supplied fields: HTTP 200.
DELETE/users/{id}Deletes a user: HTTP 204, no response body.

In a second terminal, create Alice with this command. Shell examples use macOS, Linux or Git Bash syntax; in PowerShell you can use Miblo’s request form.

shell
curl -i http://127.0.0.1:3000/users \
  -H 'Content-Type: application/json' \
  -d '{"name":"Alice","email":"alice@example.com"}'
json
{"id":1,"name":"Alice","email":"alice@example.com"}
shell
curl http://127.0.0.1:3000/users
curl http://127.0.0.1:3000/users/1

On a fresh database the first user gets ID 1, with the HTTP 201 JSON shown above. Replace 1 with the returned ID if your database already contains rows. Repeating the POST with the same email returns 409 because the field is unique. Keep the API running in the first terminal; Ctrl+C stops it.

Where to edit the generated code

simplified project tree
team-api/
├── Cargo.toml
├── .env
├── miblo.yaml
├── miblo.sqlite
├── migrations/
└── src/
    ├── main.rs
    ├── database.rs
    └── resources/user/
        ├── mod.rs
        ├── model.rs
        ├── requests.rs
        ├── handlers.rs
        ├── service.rs
        └── repository.rs

handlers.rs handles HTTP, service.rs holds your business rules and repository.rs contains SQL queries. requests.rs describes request data. Explore the full architecture →

To start with MySQL, use --database mysql in a new project and configure an existing server: MySQL setup. This does not convert an existing SQLite database.

Continue building

Continue with the User and Group example: a foreign key and a custom route.

Press m to add a field, g to create a route, d to inspect the schema, or e to open the code.

Search for a feature or command.

↑ ↓ browse Enter to open