This documentation is deprecated. It covers rspc 0.3.0, which is no longer maintained.
Quickstart
Run the released rspc 0.3.0 API with Axum.
This guide is for an existing application using rspc 0.3.0. rspc is no longer maintained. Do not mix these dependencies with the experimental procedure API.
Create a project
cargo new rspc-example
cd rspc-example
cargo add rspc@=0.3.0 rspc-axum@=0.2.0 axum@0.7
cargo add tokio --features macros,rt-multi-thread,netIf you add Specta directly for custom payload derives, use the 2.0.0-rc.20 release required by rspc 0.3.0, not the version in the current Specta guide.
Define and expose a router
use rspc::{Config, Router};
fn router() -> Router<()> {
Router::new()
.config(Config::new().export_ts_bindings("./bindings.ts"))
.query("version", |t| {
t(|_: (), _: ()| env!("CARGO_PKG_VERSION"))
})
.build()
}
#[tokio::main]
async fn main() {
let app = axum::Router::new()
.nest("/rspc", rspc_axum::endpoint(router().arced(), || ()));
let listener = tokio::net::TcpListener::bind("127.0.0.1:4000")
.await
.expect("failed to bind server");
axum::serve(listener, app).await.expect("server failed");
}
#[cfg(test)]
mod tests {
#[test]
fn router_builds() {
super::router();
}
}Run cargo run to export bindings.ts and start the server. Binding export through Config happens only in debug builds. Run the router test in CI before building the frontend.
Create a frontend client
npm install @rspc/client@0.3.1Copy or export the bindings into your frontend source directory:
import { createClient, FetchTransport } from "@rspc/client";
import type { Procedures } from "./bindings";
export const client = createClient<Procedures>({
transport: new FetchTransport("/rspc"),
});
console.log(await client.query(["version"]));Proxy the same-origin /rspc path to the Rust server during development. For a cross-origin API, use its full URL and configure CORS in Axum.
Next steps
- Router: add procedures, merge routers, and export bindings.
- Middleware: authenticate requests and change context.
- Vanilla client: configure transports and credentials.
- Deployment: generate bindings and deploy the server.
Last updated on
