Skip to main content

http_service

Attribute Macro http_service 

Source
#[http_service]
Expand description

Re-export entrypoint macros Marks an async fn as an HTTP component entrypoint for Spin.

The #[http_service] attribute designates an asynchronous function as the handler for incoming HTTP requests in a Spin component using the WASI Preview 3 (wasip3) HTTP ABI.

When applied, this macro generates the necessary boilerplate to export the function to the Spin runtime as a valid HTTP handler. The function must be declared async and take a single argument implementing [FromRequest], typically [Request], and must return a type that implements [IntoResponse].

§Requirements

  • The annotated function must be async.
  • The function’s parameter type must implement [FromRequest].
  • The return type must implement [IntoResponse].

If the function is not asynchronous, the macro emits a compile-time error.

§Example

use spin_sdk::http::{,Request, IntoResponse};
use spin_sdk::http_service;

#[http_service]
async fn my_handler(request: Request) -> impl IntoResponse {
  // Your logic goes here
}

§Generated Code

The macro expands into a module containing a Spin struct that implements the WASI http.handler/Guest interface, wiring the annotated function as the handler’s entrypoint. This allows the function to be invoked automatically by the Spin runtime when HTTP requests are received.