Key Value API
1
4fbf02fc-0329-4291-aac3-0143969167c4
Promise API
2
4fbf02fc-0329-4291-aac3-0143969167c4
Worker API
8
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Token API
7
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Template API
6
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Project policy API
5
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Project grant API
4
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Project API
3
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Login API
2
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Account API
1
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Breaking changes August 2023
1
726cfb0b-bffe-41ba-98b4-94069569ec3c
Overview
0
726cfb0b-bffe-41ba-98b4-94069569ec3c
Tier 4 languages
4
03501aa5-572b-4129-aa0a-9e9fe9f5d3fc
Python
4
54bda586-0bf3-430e-93e5-6d8c578372bd
Overview
0
ac8f610e-7e8f-465d-9e32-4b6ae0aba3b5
Overview
0
0f58ad57-8cb7-4a64-8aed-c7bea90c37f9
Overview
0
03501aa5-572b-4129-aa0a-9e9fe9f5d3fc
Overview
0
54bda586-0bf3-430e-93e5-6d8c578372bd
Overview
0
9b92ca64-009f-47e8-9457-b544f786d8af
Quickstart
2
1d167981-dd90-40ab-8023-3b2013ceb5c5
Overview
0
1aa11fd4-1dcf-4c87-8b46-5c15ca815d61
Overview
0
4fbf02fc-0329-4291-aac3-0143969167c4
Overview
0
d0be080a-56f4-481a-91d3-ba7d30c144f4
Common tooling
1
03501aa5-572b-4129-aa0a-9e9fe9f5d3fc
C
3
54bda586-0bf3-430e-93e5-6d8c578372bd
Go
2
54bda586-0bf3-430e-93e5-6d8c578372bd
Grain
3
9b92ca64-009f-47e8-9457-b544f786d8af
Swift
1
9b92ca64-009f-47e8-9457-b544f786d8af
C#
3
9b92ca64-009f-47e8-9457-b544f786d8af
Zig
2
9b92ca64-009f-47e8-9457-b544f786d8af
Rust
1
54bda586-0bf3-430e-93e5-6d8c578372bd
Overview
0
fc0b5887-cca5-4261-b6b7-73711f6f23b0
Overview
0
62133a19-c114-4a29-a74b-f986d118e157
Why Golem?
1
1d167981-dd90-40ab-8023-3b2013ceb5c5
Overview
0
1d167981-dd90-40ab-8023-3b2013ceb5c5
API Gateway
308adf1f-525e-460e-81c9-6ea00d1dbca4
d0be080a-56f4-481a-91d3-ba7d30c144f4
4
Release notes
726cfb0b-bffe-41ba-98b4-94069569ec3c
d0be080a-56f4-481a-91d3-ba7d30c144f4
9
Component interface
ac8f610e-7e8f-465d-9e32-4b6ae0aba3b5
d0be080a-56f4-481a-91d3-ba7d30c144f4
7
Getting Started
1d167981-dd90-40ab-8023-3b2013ceb5c5
d0be080a-56f4-481a-91d3-ba7d30c144f4
1
Building Components
03501aa5-572b-4129-aa0a-9e9fe9f5d3fc
d0be080a-56f4-481a-91d3-ba7d30c144f4
2
Tier 2 languages
54bda586-0bf3-430e-93e5-6d8c578372bd
03501aa5-572b-4129-aa0a-9e9fe9f5d3fc
2
Promises
62133a19-c114-4a29-a74b-f986d118e157
d0be080a-56f4-481a-91d3-ba7d30c144f4
3
Tier 3 languages
9b92ca64-009f-47e8-9457-b544f786d8af
03501aa5-572b-4129-aa0a-9e9fe9f5d3fc
3
Golem CLI
1aa11fd4-1dcf-4c87-8b46-5c15ca815d61
d0be080a-56f4-481a-91d3-ba7d30c144f4
4
REST API
fc0b5887-cca5-4261-b6b7-73711f6f23b0
d0be080a-56f4-481a-91d3-ba7d30c144f4
5
Runtime API
4fbf02fc-0329-4291-aac3-0143969167c4
d0be080a-56f4-481a-91d3-ba7d30c144f4
6
FAQ
0f58ad57-8cb7-4a64-8aed-c7bea90c37f9
d0be080a-56f4-481a-91d3-ba7d30c144f4
7

Rust

54bda586-0bf3-430e-93e5-6d8c578372bd

Make sure you have the latest Rust version installed and have cargo in the path. The recommended way to do so is using https://rustup.rs:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

rustup install stable && rustup default stable

rustup target add wasm32-wasi

The easiest way to build Golem templates with Rust is using the compatible version of https://github.com/bytecodealliance/cargo-component. A prerequisite of cargo-component is protobuf (at least 15), which can be installed as described on http://google.github.io/proto-lens/installing-protoc.html:

brew install protobuf

Then install the required version of cargo-component:

cargo install --git https://github.com/bytecodealliance/cargo-component cargo-component --rev e57d1d1405ed2d76f1f3d8647480dea700379ff8 --locked --force

cargo component --version

cargo-component-component 0.1.0 (e57d1d1 2023-08-31 wasi:134dddc)
💡
Read the following section if your IDE is not recognizing the generated bindings in Rust projects using cargo-component: https://github.com/bytecodealliance/cargo-component#using-rust-analyzer
💡
The cargo build tool does not know how to build WebAssembly components alone. The above installed cargo-component tool is an extension to it adding a new subset of commands starting with cargo component ...
Using these new commands such as cargo component build makes sure the compiler toolchain produces a WebAssembly component model target and that all the necessary bindings are generated.

The easiest way to get started once the tooling is installed is to use the golem new command as described in the Quickstart.

If you prefer to set up your project manually, you first need to create a new template:

cargo component new --reactor golem-test

Modify the interface definition for the template in wit/world.wit:

package my:template



interface api {

record product-item {

product-id: string,

name: string,

price: float32,

quantity: u32,

}



record order {

order-id: string,

items: list<product-item>,

total: float32,

timestamp: u64,

}



record order-confirmation {

order-id: string,

}



variant checkout-result {

error(string),

success(order-confirmation),

}



initialize-cart: func(user-id: string) -> ()



add-item: func(item: product-item) -> ()



remove-item: func(product-id: string) -> ()



update-item-quantity: func(product-id: string, quantity: u32) -> ()



checkout: func() -> checkout-result



get-cart-contents: func() -> list<product-item>

}



world shopping-cart {

export api

}

The functions exported in the world in this file will be exposed by Golem.

In the Rust source code you generate and import the exported symbols as:

cargo_component_bindings::generate!();



use bindings::exports::my::template::api::*;

and then implement the Guest trait from the interface definition:

struct Component;



impl Guest for Component {

fn initialize_cart(user_id: String) {

// ...

}

// ...

}

The struct you implement the trait for must be called Component!

State in Rust

When running a Golem component you don’t have to care about concurrent access of global state - each worker based on the template runs completely separated. The worker’s state is persisted by Golem so all you have to do for state handling is to store data in a global mutable variable.

In Rust we recommend the following pattern for this:

struct State {

user_id: String,

items: Vec<ProductItem>,

}



static mut STATE: State = State {

user_id: String::new(),

items: vec![],

};



fn with_state<T>(f: impl FnOnce(&mut State) -> T) -> T {

let result = unsafe { f(&mut STATE) };



return result;

}



// ...



fn add_item(item: ProductItem) {

with_state(|state| {

println!(

"Adding item {:?} to the cart of user {}",

item, state.user_id

);



state.items.push(item);

});

}

WASI Preview1 APIs

Rust already support WASI Preview1 so by using its standard libraries to work with IO, file systems, random numbers will automatically work with Golem.

WASI Preview2 APIs

To use any of the new WASI APIs, you need to explicitly put the WASI interface definitions in the wit/deps folder. Use the WASI interface definitions packaged with golem-cli to make sure they are compatible with the version implemented by Golem as these interfaces are still not finalized.

The dependencies has to be added to the Cargo.toml file for cargo-component, for example:

[package.metadata.component.target.dependencies]

"wasi:poll" = { path = "wit/deps/poll"}

"wasi:io" = { path = "wit/deps/io" }

"wasi:http" = { path = "wit/deps/http" }

This allows using these WIT packages in your main WIT file, making them available from Rust:

import wasi:poll/poll

import wasi:io/streams

import wasi:http/types

import wasi:http/outgoing-handler

HTTP

Golem implements the WASI-HTTP interface so any library built on that could be used from Golem components to communicate with external services.

At the time of writing the only library available is our fork of reqwest

To use it, just add the following dependency to your component’s Cargo.toml:

[dependencies]

reqwest = { git = "https://github.com/zivergetech/reqwest", branch = "update-aug-2023", features = ["json"] }

There is no need to manually add the WASI-HTTP interfaces to your wit/deps folder.

The fork provides the same API as the official reqwest blocking API, so please use the library’s documentation for more information: https://docs.rs/reqwest/latest/reqwest/

Compiling Rust

Creating the Golem template is just running:

cargo component build --release

The output is target/wasm32-wasi/release/component_name.wasm and it is ready to be uploaded to Golem Cloud. (the output file’s name depends on the project!)