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

Key Value API

4fbf02fc-0329-4291-aac3-0143969167c4

The key value API allows you to record arbitrary values in a key value store.

Often you can just use in memory data structures to maintain your application’s state because these data structures are durable with Golem. However, sometimes you will still want to use a more traditional key value store, for example because you expect the total size to be large.

This is where Golem’s key value API comes in! It is based on https://github.com/WebAssembly/wasi-keyvalue so it is fully open source and anyone can provide an implementation of it. When you run your program in Golem Cloud we provide an implementation that supports durable execution.

Golem’s implementation of the key value API currently uses Redis, though all of the APIs abstract over different underlying key value providers. In the future we will allow users of Golem Cloud to select the key value provider they wish to use as well as other configuration options.

To use the key value API, first make sure to add the contents of the wit/deps folders from https://github.com/golemcloud/golem-wit to your own wit/deps folder. You can then interact with the key value interface from your own code in whatever language you are working in.

Let’s see what this looks like with an example. For this example we will create a simple service that lets us set arbitrary key value pairs in specified buckets and get those values, implemented using the key value API.

Our WIT file will look like this:

package golem:examples



interface api {

get: func(bucket: string, key: string) -> option<list<u8>>



set: func(bucket: string, key: string, value: list<u8>) -> ()

}



world key-value-example {

import wasi:keyvalue/readwrite

export api

}

Notice that we imported the wasi:keyvalue/readwrite functionality. WASI Key Value includes a variety of interfaces for simple read / write operations, batch operations, atomic operations, and even caching.

💡
Golem supports the readwrite and batch interfaces right now. Support for additional interfaces will be provided in the future.
💡
The WASI Key Value interface is still in the proposal stage and is likely to change in the future. Some of the interfaces have already been modified based on feedback from the Golem team.

Now that we have our interface we are ready to implement it. Let’s take a look at how we would do that:

cargo_component_bindings::generate!();



use crate::bindings::exports::golem::examples::api::*;

use crate::bindings::wasi::keyvalue::readwrite::*;

use crate::bindings::wasi::keyvalue::types::*;

use crate::bindings::wasi::keyvalue::wasi_cloud_error::*;



struct Component;



impl Guest for Component {

fn get(bucket: String, key: String) -> Option<Vec<u8>> {

let bucket = open_bucket(&bucket).unwrap();

match get(bucket, &key) {

Ok(incoming_value) => {

let value = incoming_value_consume_sync(incoming_value).unwrap();

Some(value)

}

Err(error) => {

let trace = trace(error);

if trace == "Key not found" {

None

} else {

panic!("Unexpected error: {}", trace);

}

}

}

}



fn set(bucket: String, key: String, value: Vec<u8>) {

let bucket = open_bucket(&bucket).unwrap();

let outgoing_value = new_outgoing_value();

outgoing_value_write_body_sync(outgoing_value, &value).unwrap();

set(bucket, &key, outgoing_value).unwrap()

}

}

To set a value we first open a bucket, then create an outgoing value that will contain the value we want to write. We then write our value to the outgoing value and finally set it in the key value store.

Similarly to get a value we open a bucket, then get the value at the corresponding key. WASI Key Value currently returns an error if the key does not exist, so we introspect on the value and either return None if the key does not exist or retail with the error if another error occurred.

And there we have it! We have taken advantage of key value functionality in our own program with a few lines of code and without having to stand up any infrastructure of our own.