Skip to content

SDK Guide

The JellyFaaS SDK simplifies working with the JellyFaaS API. It provides a Client class which handles authentication and functions to simplify creating HTTP requests to the API. It also provides additional validation and checks of your requests, to prevent making invalid function calls, which would otherwise consume API credits.

API Credits

The cloud functions used in the examples below will not use your credits. Click here to get an secret key.

Here is a walk through using the Hello World function.

1. Creating a Config object

The first step is to create your config object and parse your JellyFaaS secret key into it. The config handles keys and authorization.

from jellyfaas import Config

config = Config(api_key="JELLYFAAS_SECRET", debug=True)
const {Config} = require('jellyfaas')

let config = await new Config("<JELLYFAAS_SECRET>")

2. Creating the Client

After that, you can create the client, and parse the newly created 'config' object into it:

from jellyfaas import Client

# initialize config object here 

client = Client(config)
const {Config, Client} = require('jellyfaas')

let client = await new Client(config)          

3. Looking up a function

To look up a function, you must use the function's short name to find it. Provided all the previous steps are complete, add the following:

client.lookup_function("helloworld")
await client.lookupFunction("helloworld")

4. Setting the request query parameters & body

Once you've called 'lookup' on your client object, you can now set your request. Depending on the function, you may need to set query parameters, a body, or both. See the function schema for details.

4.1 Setting query parameters

query_params={
    "firstname": "foo",
    "lastname": "bar"
}
client.set_function_query_params(queryParams)
query_params={
    "firstname": "foo",
    "lastname": "bar"
}
client.setFunctionQueryParams(queryParams)

4.2 Setting the body

a. Sending JSON data

If the function accepts a JSON format body, use the following syntax.

body = {
    "message": "JellyFaaS is cool",
    "rating": 5.0
}
client.set_function_body(body)
body = {
    "message": "JellyFaaS is cool",
    "rating": 5.0
}
client.setFunctionJsonBody(body);

b. Sending files

If the function accepts a file input body, use the following syntax.

with open('./relative/path/to/file.png', 'rb') as file:
    client.set_function_body(file)
await client.setFunctionFileBody('./relative/path/to/file.png')

6. Invoking the function

You are ready to call your function! Simply call invoke() and your function will be called.

import os
from  jellyfaas import Config, Client

query_params = {
    "rating": "PG",
    "score": "5.8"
}

config = Config(api_key="<JELLYFAAS_SECRET>")
client, result = Client(config).lookup_function("suggestmovie").set_function_query_params(query_params).invoke()

print(result.json())
const {Config, Client} = require('jellyfaas')

query_params = {
    "rating": "PG",
    "score": "5.8"
}

async function run() {
    let config = await new Config("<JELLYFAAS_SECRET>") // Construct Config
    let client = await new Client(config)                 // Construct Client
    await client.lookupFunction("suggestmovie")           // Lookup function
    client.setFunctionQueryParams(query_params)           // Set query params
    let resp = await client.invoke(null, false)           // invoke (overriding validation)

    console.log(resp)        
}

run()

Error Handling

The JellyFaaS SDK provides custom exceptions for different error scenarios. Below is a description of each error type and where to expect it to be thrown for each SDK:

  1. jellyfaas.AuthenticationFailedException

    Thrown when your credentials cannot be verified during config object creation.

  2. jellyfaas.FunctionLookupException

    Occurs during the function look up stage. This could be due to the function not existing.

  3. jellyfaas.SetRequestException

    This can be down to the query parameters being incorrect and therefore not lining up with the function's query parameter schema. Or, this can be due to the input body not matching the functions input schema.

  4. jellyfaas.InvocationException

Next Steps