The Spin Rust SDK
The Spin Rust SDK makes it easy to build Spin components in Rust.
Spin documentation
This README file provides a few examples, such as writing Spin HTTP components in Rust and making outbound HTTP requests. For comprehensive information, visit the official Spin Documentation website. This resource includes a page on installing Spin, a quickstart guide, and a language support overview page. The latter lists all of Spin's features—including key-value storage, SQLite, MySQL, Redis, Serverless AI, etc.—and their implementation in specific languages such as Rust, TS/JS, Python, and TinyGo.
Writing Spin HTTP Components in Rust
This library simplifies writing Spin HTTP components. Below is an example of such a component:
// lib.rs
use ;
use http_service;
/// A simple Spin HTTP component.
async
The important things to note about the function above are:
- the
spin_sdk::http_servicemacro marks the function as the entry point for the Spin component, - in the function signature,
reqcan be the built-inRequesttype (a re-export ofhttp::Request) or any type that implementsFromRequest, - the response type can be anything that implements
IntoResponse, including&str,String,http::StatusCode,http::Response<T>, a tuple of(StatusCode, body), orResult<impl IntoResponse>.
Making Outbound HTTP Requests
Let's see an example where the component makes an outbound HTTP request to a server, modifies the result, and then returns it:
use ;
use http_service;
async
For the component above to be allowed to make the outbound HTTP request, the destination host must be declared, using the allowed_outbound_hosts configuration, in the Spin application's manifest (the spin.toml file):
= 2
[]
= "hello_world"
= "0.1.0"
= ["Your Name <your-name@example.com>"]
= "An example application"
[[]]
= "/..."
= "hello-world"
[]
= "target/wasm32-wasip2/release/hello_world.wasm"
= ["https://random-data-api.fermyon.app"]
[]
= "cargo build --target wasm32-wasip2 --release"
= ["src/**/*.rs", "Cargo.toml"]
Building and Running the Spin Application
Spin build can be used to build all components defined in the Spin manifest file at the same time, and also has a flag that starts the application after finishing the compilation, spin build --up:
)
)
Once our application is running, we can make a request (by visiting http://localhost:3000/ in a web browser) or using curl as shown below:
}