I spend a lot of time writing Laravel in PHP, so my first question when Laravel Rust landed was: what does a normal route actually look like? You can answer that with a small project. We'll build a JSON API that takes a title for a link preview, checks it and gives us back a clean title and a character count. We're only building the title preview, so we won't fetch the link or save anything.
Laravel Rust launched in public beta on 1 December 2026. It's a separate Rust framework from the Laravel team. If you're used to PHP's API routes and form requests, you'll recognise the jobs those pieces do. What changes is how you specify them: the handler's argument describes the input, and its return type describes the output.
Install Rust with rustup if you don't already have it. The framework crate requires Rust 1.99 or newer. I tested this walkthrough with Rust 1.99.1 and Laravel Rust 0.1.0. Rust 1.100.1 is the current stable release as of writing; the versions I tested are listed so you can reproduce the specific setup if the beta changes.
rustc --version
cargo new link-preview
cd link-preview
Laravel has an official application starter for full projects. We're starting with Cargo instead because two JSON routes don't need a frontend, an app skeleton or a database. Replace Cargo.toml with:
[package]
name = "link-preview"
version = "0.1.0"
edition = "2024"
rust-version = "1.99"
[dependencies]
laravel-rust = { version = "=0.1.0", default-features = false }
serde = { version = "1", features = ["derive"] }
Cargo.toml is roughly the equivalent of composer.json. We pin the framework version for this tutorial and turn off the default Inertia and template features because there's no page to render. Serde converts between our Rust structs and JSON. Cargo will write a Cargo.lock on the first build. Commit that file if you put this project in Git, so another person building your copy gets the same dependency versions.
Replace src/main.rs with this. Keeping it in one file makes the request easy to follow.
use std::process::ExitCode;
use laravel::prelude::*;
use laravel::validation::{Valid, Validate};
use laravel::Application;
use serde::{Deserialize, Serialize};
#[derive(Serialize)]
struct Health {
status: &'static str,
}
#[derive(Deserialize, Validate)]
#[validate(anyone)]
struct PreviewInput {
#[validate(required, max_length = 80)]
title: String,
}
#[derive(Serialize)]
struct Preview {
title: String,
characters: usize,
}
async fn health() -> Json<Health> {
Json(Health { status: "ok" })
}
async fn preview(Valid(input): Valid<PreviewInput>) -> Json<Preview> {
let title = input.title.trim().to_owned();
let characters = title.chars().count();
Json(Preview { title, characters })
}
fn main() -> ExitCode {
Application::configure()
.with_routing(|routing, _| {
routing.api(|routes| {
routes.get("/health", health);
routes.post("/preview", preview);
});
})
.run()
}
Read it from the bottom up. main boots the app, and routing.api mounts the routes under /api by default. Register /preview in the group and call /api/preview from your client. It's easy to type /api in both places when you're new to the setup. If you do, you'll register /api/api/preview. The routing docs cover how to change the prefix.
The handler argument, Valid(input): Valid<PreviewInput>, asks Laravel to decode and validate the request against our struct before the handler runs. The pattern on the left unpacks the wrapper and gives us the checked value as input. Deserialize lets Serde read the fields, and Validate provides the rules. An empty string is still a valid Rust String, so required matters even though we've declared the field as a string. The #[validate(anyone)] attribute makes this input available without logging in, which is what we want for a local demo. See the validation docs for more rules.
On success we trim the outer whitespace and count Unicode scalar values with chars().count(). That's more useful here than counting UTF-8 bytes, though a combined emoji can still count as multiple values. Then we wrap the output in Json to send it back to the client.
Notice that the input and output are separate structs. The client sends a title, but only the server sets characters. Suppose you later add a display label or a URL to the response. You can change what the frontend receives without accidentally asking callers to send that new field. On the flip side, changing PreviewInput.title changes the contract clients send you. Keeping those two shapes separate makes the difference obvious when you come back to the file.
Start the app in one terminal:
APP_URL=http://127.0.0.1:8077 cargo run -- serve --host 127.0.0.1 --port 8077
The first build downloads and compiles dependencies, so give it a moment. Set APP_URL to the same address you pass to curl. Laravel checks the incoming host, so the wrong host can give you a 400 before your handler runs. While the server is running, open a second terminal and try the health route first:
curl -i http://127.0.0.1:8077/api/health
You should see HTTP 200 and {"status":"ok"}. That confirms you're hitting the right port and prefix before you add a request body. Now send a title with a couple of extra spaces:
curl -i http://127.0.0.1:8077/api/preview \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
--data '{"title":" Hello, Rust! "}'
Look for HTTP 200 and {"title":"Hello, Rust!","characters":12}. Then change the data to {"title":""} and run it again. You should get HTTP 422 and a validation error for title. With {"title":42}, you still get a 422, but the message changes. Laravel rejects the request because the input type expects a string. Either way, the handler doesn't run.
There are two headers in that request for a reason. Content-Type describes what you're sending; Accept tells the server what you want back. Curl's --data already makes this a POST, so you don't need -X POST as well. Leave -i in while you're learning: without the status line, a JSON error response can look like success as you skim the terminal.
One more useful test is the exact limit. Try a title made of 80 copies of the letter A, then add one more. The first should pass and the second should fail. Testing both sides catches an off-by-one mistake that an empty-string test wouldn't.
If you get a 404, run cargo run -- route:list from your project folder and compare the paths with your curl command. If the port is already busy, change it in APP_URL, the serve command and the curl URLs.
My next tweak would be to add an is_truncated flag and let the frontend show what happens at the edge of a preview card. For now, you've got a small working API with a typed request, a typed JSON response and an error path you can trigger on purpose. That's enough to get a feel for Laravel Rust before you bring in a database or a frontend.