Docs / Start here
Installation
add Restify to an application and serve a first model.
Requirements #
- A Laravel for Rust application on
laravel-rust0.2, with the database driver feature for your database:database-postgres,database-mysql(MySQL and MariaDB) ordatabase-sqlite. Restify is tested on PostgreSQL 18, MySQL 8.4, MariaDB 12.3 and SQLite. For MariaDB, startDATABASE_URLwithmariadb://, so the framework writes MariaDB's SQL. To make one, see Creating the application. - Rust 1.99 or later.
Framework versions #
Each version of Restify is built for one minor version of the framework, and requires its exact release in its own Cargo.toml:
Restify (laravel-restify) | Framework (laravel-rust) |
|---|---|
| 0.2.x | 0.2.x (0.2.0 exactly) |
| 0.1.x | 0.1.6 exactly |
Your application's laravel-rust line must name the same minor version as the Restify you add: "0.2" for Restify 0.2. Cargo picks the release Restify requires within that minor version. It never moves your application from one minor version to another: with laravel-rust = "0.2" in your application and Restify 0.1's "=0.1.6", Cargo builds two copies of the framework, and the build fails, because your models are the 0.2 framework's and Restify's traits are the 0.1 framework's. A new application from the starter is on the newest framework, so it takes the newest Restify. To see the versions in use, run cargo tree -i laravel-rust.
To move an application to a newer Restify, follow the "Upgrading" part of its section in CHANGELOG.md, in the Restify for Rust repository.
Creating the application #
Skip this if you have one. Create a new application with the Laravel CLI, as the framework's own installation.md shows (install the CLI first, from that guide):
laravel new my-api --rust
cd my-api
laravel --version must start with Laravel CLI. Laravel Herd's laravel command, and the older installer from Composer, are other programs, without --rust.
Without the Laravel CLI, copy the official starter application with Git. It is the same application, and it needs only Git and Rust:
git clone --depth 1 --branch 0.x https://github.com/laravel/rust.git my-api
cd my-api
rm -rf .git && git init
cargo run -- key:generate
key:generate creates .env from .env.example and writes a new APP_KEY. The starter's 0.x branch follows the newest framework release. For an older framework, clone the starter's tag for it instead, such as --branch v0.1.3 for laravel-rust 0.1.
The new application uses PostgreSQL. For another database, replace database-postgres in the laravel-rust line of Cargo.toml with database-mysql or database-sqlite, and set DB_CONNECTION and DATABASE_URL in .env.
The starter's .cargo/config.toml sends most of the build to ~/.cargo/build/laravel-rust, a cache that every Laravel application on the machine shares (the framework's development.md, "The shared build cache"), not to target/. Clean one application with cargo clean --workspace. A plain cargo clean deletes the shared cache too. If you build in a container that is thrown away after each run, set CARGO_BUILD_BUILD_DIR to a folder you keep, or delete the build-dir line to keep the whole build in target/.
Adding the crate #
Add it from crates.io:
cargo add laravel-restify
The package is named laravel-restify, and your code imports it as restify.
Choosing features #
The MCP server and the OpenAPI document are Cargo features, both on by default. Turn off the ones your application doesn't use, and their code and dependencies leave your binary:
| Feature | Adds | Default |
|---|---|---|
mcp | The MCP server and its tools, .mcp(..) and .without_mcp(), and the rmcp crate | On |
openapi | The OpenAPI document, .openapi(..), .openapi_version(..) and the restify:openapi command | On |
The JSON:API endpoints, the restify:install and restify:list commands, actions, getters, bulk endpoints and relationships are always there.
To keep only the endpoints, turn off the default features:
[dependencies]
laravel-restify = { version = "0.2", default-features = false }
To keep the endpoints and the MCP server, without the OpenAPI document:
laravel-restify = { version = "0.2", default-features = false, features = ["mcp"] }
With mcp off, Registry::mcp is None, RepositoryInfo::tools is empty, and restify:list says the MCP server is turned off. With openapi off, Registry::openapi is None and the restify:openapi command doesn't exist. A call to .without_mcp() or .openapi(..) with its feature off doesn't compile: turn the feature on, or remove the call. What each feature costs in build time and binary size is measured in bench/README.md in the Restify for Rust repository.
Registering the provider #
Register the Restify service provider in src/app.rs right away, even before it serves a repository. Restify's commands, such as restify:install, come with the provider: until it is registered, the application says the command is unknown.
Application::configure()
.with_providers(|providers| {
providers.add(restify::Restify::new());
})
If src/app.rs already calls .with_providers(..), add the providers.add(..) line inside it.
Installing the agent skill #
If AI coding agents work on your application, install the Restify skill once, after registering the provider:
laravel restify:install
The command writes two skills to .agents/skills/, and lists them in your AGENTS.md when you have one. It writes the same files to .claude/skills/, because Claude Code reads skills only from there, and only skills there become slash commands such as /restify-migrate. Other agents read .agents/skills/. building-apis-with-restify tells an agent the rules of a repository and where to find these guides for your installed version. restify-migrate takes an agent through moving a Restify for PHP application to this one (Migrating from Restify for PHP). To start that migration from the PHP application instead, php artisan restify:rust-install writes the same skill there. Running the command again updates the skill and changes nothing else. Run it again after you upgrade Restify.
Serving a first model #
Restify needs three things for a model: an input struct for creating it, an input struct for updating it, and a repository that lists the fields to show. This example serves a Post model with a title and a nullable body.
// src/models/post.rs
use laravel::prelude::*;
use laravel::validation::Sometimes;
use restify::{Action, Field, Repository, Schema};
#[laravel::input(fills = Post)]
#[validate(anyone)]
#[derive(Schema)]
pub struct NewPost {
#[validate(required, max_length(255))]
pub title: String,
pub body: Option<String>,
}
#[laravel::input(fills = Post)]
#[validate(anyone)]
#[derive(Schema)]
pub struct EditPost {
#[validate(required, max_length(255))]
pub title: Sometimes<String>,
pub body: Sometimes<Option<String>>,
}
pub struct PostRepository;
impl Repository for PostRepository {
type Model = Post;
type Store = NewPost;
type Update = EditPost;
/// What a post shows. A column without a field stays private.
fn fields() -> Vec<Field<Post>> {
vec![
Field::new(Post::title).searchable().sortable(),
Field::new(Post::body).searchable().hide_from_index(),
Field::new(Post::created_at),
]
}
async fn authorize(action: Action, _request: &Request, _post: Option<&Post>) -> laravel::Result<()> {
// Anyone may read. Nobody may write yet.
match action {
Action::AllowRestify | Action::Index | Action::Show => Ok(()),
_ => Err(laravel::abort(403)),
}
}
}
Register the repository with the Restify provider in src/app.rs, in place of the bare Restify::new():
use restify::Restify;
Application::configure()
.with_providers(|providers| {
providers.add(Restify::new().repository::<PostRepository>());
})
The application now serves GET /api/restify/posts, GET /api/restify/posts/{id}, and an MCP server at POST /mcp/restify. The MCP server has no tools for posts until PostRepository sets const MCP. See MCP. To see everything it serves:
laravel restify:list
A repository refuses every request until its authorize method allows it, and shows nothing of the model until fields lists it. This is deliberate: an API that nobody decided to open stays closed. To let users write, see Authorization. For what a field can do, see Fields.
Configuring the provider #
| Method | Does | Default |
|---|---|---|
.repository::<R>() | Serves a repository's endpoints and tools | None |
.authenticate(m) | Runs the middleware that authenticates users before every endpoint and MCP request, except the reads of a public repository | None |
.middleware(m) | Runs a middleware before every endpoint and MCP request | None |
.prefix("/v1") | Changes the path of the endpoints, inside the api group | /restify |
.mcp("/mcp/shop") | Changes the path of the MCP server (feature mcp) | /mcp/restify |
.without_mcp() | Serves no MCP server (feature mcp) | Served |
.openapi("/openapi.json") | Serves the OpenAPI document at a path inside the prefix (feature openapi) | Not served |
.openapi_version("2.0.0") | Changes the document's info.version (feature openapi) | 1.0.0 |
.auth::<R>() | Serves register, login and logout for the users of repository R; see Authentication. A new application needs the tokens table's migration | Not served |
.password_reset::<R>() | Serves forgot and reset password; see Authentication | Not served |
.email_verification::<R>() | Emails a link at registration, and serves the link; see Authentication | Not served |
.profile::<R>() | Serves the logged-in user's profile; see Authentication | Not served |
The framework opens 10 database connections at most (DB_MAX_CONNECTIONS). In the benchmark, with the database on the same machine, a larger pool served no more requests: the server ran out of processor first. Raise it when requests wait for the database instead, such as when the database is on another machine.
An application refuses to start when two repositories have the same name. Override Repository::uri_key on one of them.
Before the first deploy, read Before you go to production.