General concepts

General concepts of serverless functions

Don't serialize/use as response i128. We're investigating a bug that crashes the program's execution when i128s are being serialized. Consider converting them to i64s in the responses. Converting to string won't work either.

EnvClient::empty()

When getting a handle on the host environment client, you should never use the standard EnvClient::new() inside the on_close() functions. Using that handle will result in the program's execution stopping immediately if called as a serverless function.

You should rather use the EnvClient::empty() function which is compatible with serverless functions.

Reading the request body

The request body that you provided in the web request is forwarded to the VM and can be accessed guest-side with env.read_request_body() :

#[derive(Serialize, Deserialize)]
pub struct Request {
    account: String
}

#[no_mangle]
pub extern "C" fn test_function() {
    let env = EnvClient::empty();
    let request: Request = env.read_request_body();
}

Returning the response

Returning the response is done through env.conclude(response) where response is a serializable type.


Calling functions

To call functions you can run a web request against the /zephyr/execute endpoint:

curl -X POST https://api.mercurydata.app/zephyr/execute -H "Authorization: Bearer $MERCURY_JWT" -H 'Content-Type: application/json' -d '{"mode":{"Function": {"fname": "test_function", "arguments": "{\"account\": \"GC5PGBLWLRPBIJ7AC2KD3MI34BXYKCJIRVXXFBR2CP2NBLVN2LLY2HUJ\" "}}}'

Where you specify the request body within the arguments field.

A Note on Auth

Currently, calling functions is an authorized action, as a result, code deployed by a certain user can only be called with a JWT token that authenticates the user. This will change:

  • very soon by adding publicly callable functions.

  • mid-term with a very simple auth framework if needed.

Last updated