Create your first decnetralized function

In this tutorial, you will learn how to write a simple function to calculate sum of two numbers in Rust language and run on Planetr decentralized cloud using Planetr gateway CLI interface.

This tutorial is aimed at someone who has basic knowledge of Function-as-a-service (AWS Lambda, Google Functions etc..) and wants to get a basic and quick understanding of Planetr decentralized functions. We will not be going into depth about the intricacies of developing on Planetr, but will hopefully satisfy your curiosity so that you will continue your journey!

This tutorial should take you about 10 minutes to complete.

We only expect that:

  • You are generally familiar with software development and command line interfaces.
  • You are generally familiar with Rust programming language.
  • You are open to learning about the bleeding edge of decentralized development.

Let's Begin

On Planetr's decentralised cloud, run a function to compute sum of two numbers.

Install and run Planetr Gateway on your computer

To install Planetr gateway on your computer, please read the installation section. This can be achieved on a laptop, a desktop computer, or a cloud instance.

Make sure Planetr gateway is installed and running on your computer.

$ planetr -v

Create a web assembly

Follow this link to create a web assembly binary (.wasm file) with the rust code to add two numbers. After completing the steps, you should be having a wasm file named wasm-sum.wasm.

Deploy the 'sum' function

Deploy the sum function on Planetr network using func-create command. You can provide any name for the function using -n parameter.

$ planetr func-create wasm-sum.wasm -n sumfunc
INSTANCE ID            NAME      ENDPOINT                                       ERROR  
c2nmdr72hrahq4eraj8g   sumfunc   http://localhost:7001/f/c2nmdr72hrahq4eraj8g          

Please note the endpoint URL.

Run the 'sum' function using CLI

You can run the sum function using planetr func-run command.

$ planetr func-run c2nmdr72hrahq4eraj8g -a "{\"x\":10, \"y\":20}" -p
    "code": 200,
    "log": "begin\nend\n",
    "payload": {
        "sum": 30

Where c2nmdr72hrahq4eraj8g is the Instance Id of the sum function created earler. It will be different for you.

{x:10, y:20} is the input payload JSON which defines x and y integers as input.

{"sum":30} is the result payload JSON which gives sum=30.

-p flag is to pretty print the JSON result and is optional.

Congratulations! You ran your first function on the decentralized cloud.

Provide the input through JSON file

You can also provide the input payload JSON in a file with -f parameter.

Create a file input.json with the input as below:


{x:10, y:20}

Now run the function with input json file.

$ planetr func-run c2nmdr72hrahq4eraj8g -f input.json -p
    "code": 200,
    "log": "begin\nend\n",
    "payload": {
        "sum": 30

Run the 'sum' function using API

Planetr exposes the function as a REST API for you to integrate it with your application which may be written in any language of your choice (example: Java, PHP, Rust, Go, .Net, Python etc...)

The endpoint for the function is seen when you create the function which for this tutorial is http://localhost:7001/f/c2nmdr72hrahq4eraj8g

When you are invoking the function via API, use:

URL: http://localhost:7001/f/c2nmdr72hrahq4eraj8g
Content-type: application/json
Method: POST
Body: Input as JSON string

For example, the CURL request will be:

curl \
  --header "Content-type: application/json" \
  --request POST \
  --data '{"x":10, "y":20}' \

Thank you for trying the first function tutorial.