| Crates.io | momento-functions-host |
| lib.rs | momento-functions-host |
| version | 0.4.1 |
| created_at | 2025-04-16 22:53:37.891475+00 |
| updated_at | 2025-09-19 22:17:17.879243+00 |
| description | Host interface support crate for Momento Functions |
| homepage | |
| repository | https://github.com/momentohq/functions |
| max_upload_size | |
| id | 1637028 |
| size | 101,537 |
Momento Functions are how you can extend Momento.
A work in progress, you can learn more about Functions by reaching out to support@momentohq.com
Functions run on Momento's service hosts, and offer a powerful scripting capability.
This repository holds crates for Momento Functions guest code.
To see some of what you can do with Functions, you can look at the examples.
rustup target add wasm32-wasip2cargo init --lib hello
Add a file .cargo/config.toml that sets the build target, for convenience.
[build]
target = "wasm32-wasip2"
Cargo.tomlAdd this to build the right kind of artifact:
[lib]
crate-type = ["cdylib"]
Import the Functions support library and WIT library.
[dependencies]
momento-functions = { version = "0" }
momento-functions-wit = { version = "0" }
The simplest function is a pong response web function. You can put this in lib.rs.
momento_functions::post!(ping);
fn ping(_payload: Vec<u8>) -> &'static str {
"pong"
}
Build: cargo build --release
Deploy
First, base64 encode the function, then upload. Note that the path here includes "manage". The output from
using curl -v should include an HTTP status code of 204.
MOMENTO_CACHE_NAME=your_cache
base64_data=$(cat target/wasm32-wasip2/release/hello.wasm | base64)
curl -v \
https://api.cache.$MOMENTO_CELL_HOSTNAME/functions/manage/$MOMENTO_CACHE_NAME/ping \
-XPUT \
-H "authorization: $MOMENTO_API_KEY" \
-H "Content-Type: application/json" \
--data "{\"inline_wasm\":\"$base64_data\"}"
Alternatively, you can use the Momento CLI, which will handle the encoding for you:
momento preview function put-function \
--cache-name "$MOMENTO_CACHE_NAME" \
--name ping \
--wasm-file target/wasm32-wasip2/release/hello.wasm
Invoke
Invoke the function by sending a request directly to the function name.
MOMENTO_CACHE_NAME=your_cache
curl \
https://api.cache.$MOMENTO_CELL_HOSTNAME/functions/$MOMENTO_CACHE_NAME/ping \
-H "authorization: $MOMENTO_API_KEY" \
-d 'ping'
From here, you should look at the examples. Momento Functions are a limited environment, but the supported feature set is growing.
Using wasm32-wasip2, you have access to std::time. Most other std wasip2 interfaces will panic at runtime.
std wasi interface |
status |
|---|---|
| time | SystemTime and Instant supported |
| environment | supported, populated from function configuration |
| error | supported, but empty; also unavailable due to lack of io interface support |
| exit | unsupported - it does panic though, which may work well enough for you |
| filesystem_preopens | unsupported |
| filesystem_types | unsupported |
| stderr | unsupported |
| stdin | unsupported |
| stdout | unsupported |
| streams | unsupported |
Other wasi interfaces are not defined and will result in a linking error when you upload your Function.
You are running under a wasmtime host. Unless otherwise specified, the host you're running on is undefined.
You are effectively running as a stateless web server.
As the ecosystem matures, new limits may be created and execution location semantics may change.
If you hit an error you don't think you should - e.g., you updated Rust locally and now your Functions don't link - please reach out to support@momentohq.com