Grain in WebAssembly

Grain is a functional programming language that is designed specifically to compile to WebAssembly. We have enjoyed using it because of its intuitive syntax and great tooling.

Available Implementations

There is one official implementation of Grain


Grain can be used in the browser. It also supports WASI, so it can be used for the Fermyon Platform (Wagi and Spin) or with commandline runners like Wasmtime.

Pros and Cons

Things we like:

  • Easy to learn
  • Good toolchain
  • Good documentation

We’re neutral about:

  • Compiled object size.
  • Execution speed.

Things we’re not big fans of:

  • Not a lot of third party libraries yet


All of our examples follow a documented pattern using common tools.

To use Grain, you will need to install the Grain toolkit.

Start with a file:

print("content-type: text/plain\n")
print("Hello, World!")

Compile the program with the grain compiler:

$ grain

The above will produce a file. As usual, you can run wasmtime to see the output. The first time you compile a grain application, it will take a long time. After that, compiling is much faster.

To run the WebAssembly app with Spin, create a spin.toml file:

spin_version = "1"
authors = ["Fermyon Engineering <>"]
description = "Grain example."
name = "spin-grain"
trigger = { type = "http", base = "/" }
version = "1.0.0"

id = "grain-hello"
source = ""
route = "/"
# Spin components written in Grain use the Wagi HTTP executor
executor = { type = "wagi" }

From there, you can use spin up to start a server, and see the results on http://localhost:3000.

Learn More

Here are some great resources: