{"id":25307465,"url":"https://github.com/spinframework/spin-rust-sdk","last_synced_at":"2025-10-28T11:31:22.707Z","repository":{"id":223583495,"uuid":"592829189","full_name":"spinframework/spin-rust-sdk","owner":"spinframework","description":"Spin SDK for Rust","archived":false,"fork":false,"pushed_at":"2025-02-10T14:49:28.000Z","size":611,"stargazers_count":17,"open_issues_count":11,"forks_count":13,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-02-13T11:14:38.793Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://developer.fermyon.com/spin/v2/rust-components","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spinframework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-24T16:17:27.000Z","updated_at":"2025-02-13T00:51:36.000Z","dependencies_parsed_at":"2025-01-15T17:38:54.888Z","dependency_job_id":"a89261e4-0cba-4239-b1b3-2ceb3c938681","html_url":"https://github.com/spinframework/spin-rust-sdk","commit_stats":null,"previous_names":["fermyon/spin-rust-sdk","spinframework/spin-rust-sdk"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spinframework%2Fspin-rust-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spinframework%2Fspin-rust-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spinframework%2Fspin-rust-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spinframework%2Fspin-rust-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spinframework","download_url":"https://codeload.github.com/spinframework/spin-rust-sdk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238642365,"owners_count":19506199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-13T11:14:54.238Z","updated_at":"2025-10-28T11:31:22.693Z","avatar_url":"https://github.com/spinframework.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The Spin Rust SDK\n\nThe Spin Rust SDK makes it easy to build Spin components in Rust.\n\n## Spin documentation\n\nThis `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](https://spinframework.dev). This resource includes [a page on installing Spin](https://spinframework.dev/install#installing-spin), [a quickstart guide](https://spinframework.dev/quickstart), and [a language support overview page](https://spinframework.dev/language-support-overview). 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.\n\n### Writing Spin HTTP Components in Rust\n\nThis library simplifies writing Spin HTTP components. Below is an example of\nsuch a component:\n\n```rust\n// lib.rs\nuse spin_sdk::http::{IntoResponse, Request, Response};\nuse spin_sdk::http_component;\n\n/// A simple Spin HTTP component.\n#[http_component]\nfn handle_hello_world(req: Request) -\u003e anyhow::Result\u003cimpl IntoResponse\u003e {\n    println!(\"Handling request to {:?}\", req.header(\"spin-full-url\"));\n    Ok(Response::builder()\n        .status(200)\n        .header(\"content-type\", \"text/plain\")\n        .body(\"Hello, Fermyon\")\n        .build())\n}\n```\n\nThe important things to note about the function above are:\n\n- the `spin_sdk::http_component` macro marks the function as the entry point for the Spin component,\n- in the function signature (`fn handle_hello_world(req: Request) -\u003e anyhow::Result\u003cimpl IntoResponse\u003e`), `req` can be any number of types, including the built-in `Request` type or the `http::Request` type from the popular `http` crate\n- in the function signature, the response type can be anything that implements `IntoResponse` meaning the return type can any number of things including `anyhow::Result\u003cimpl IntoResponse\u003e` (as shown above), `impl IntoResponse`, `Response`, `anyhow::Result\u003cResponse\u003e`, or even the `http::Response` type from the `http` crate. \n  - Note: Using the `http` crate will require you to add it to your Cargo.toml manifest (i.e., `cargo add http`).\n\n### Making Outbound HTTP Requests\n\nLet's see an example where the component makes an outbound HTTP request to a server, modifies the result, and then returns it:\n\n```rust\nuse spin_sdk::{\n    http::{IntoResponse, Request, Method, Response},\n    http_component,\n};\n\n#[http_component]\nasync fn handle_hello_world(_req: Request) -\u003e Result\u003cimpl IntoResponse\u003e {\n    // Create the outbound request object\n    let req = Request::builder()\n        .method(Method::Get)\n        .uri(\"https://random-data-api.fermyon.app/animals/json\")\n        .build();\n\n    // Send the request and await the response\n    let res: Response = spin_sdk::http::send(req).await?;\n\n    println!(\"{:?}\", res);  // log the response\n    Ok(res)\n}\n```\n\nFor 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):\n\n```toml\nspin_manifest_version = 2\n\n[application]\nname = \"hello_world\"\nversion = \"0.1.0\"\nauthors = [\"Your Name \u003cyour-name@example.com\u003e\"]\ndescription = \"An example application\"\n\n[[trigger.http]]\nroute = \"/...\"\ncomponent = \"hello-world\"\n\n[component.hello-world]\nsource = \"target/wasm32-wasip1/release/hello_world.wasm\"\nallowed_outbound_hosts = [\"https://random-data-api.fermyon.app\"]\n[component.hello-world.build]\ncommand = \"cargo build --target wasm32-wasip1 --release\"\nwatch = [\"src/**/*.rs\", \"Cargo.toml\"]\n```\n\n### Building and Running the Spin Application\n\nSpin 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`:\n\n```bash\n$ spin build --up\nBuilding component hello-world with `cargo build --target wasm32-wasip1 --release`\n    Finished release [optimized] target(s) in 0.12s\nFinished building all Spin components\nLogging component stdio to \".spin/logs/\"\n\nServing http://127.0.0.1:3000\nAvailable Routes:\n  hello-world: http://127.0.0.1:3000 (wildcard)\n```\n\nOnce our application is running, we can make a request (by visiting `http://localhost:3000/` in a web browser) or using `curl` as shown below:\n\n```bash\n$ curl -i localhost:3000\nHTTP/1.1 200 OK\ncontent-length: 77\ncontent-type: application/json\n\n{\"timestamp\":1702599575198,\"fact\":\"Sharks lay the biggest eggs in the world\"}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspinframework%2Fspin-rust-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspinframework%2Fspin-rust-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspinframework%2Fspin-rust-sdk/lists"}