Skip to main content

Cloud Functions

Less allows you to easily create and deploy serverless functions with infinite scale.

Creating Cloud Functions

Simply create a folder named functions in the less directory and add your functions to it:

mkdir -p less/functions

Here's an example of a sum function that adds 2 numbers:

mkdir less/functions/sum
touch less/functions/sum/index.js
less/functions/sum/index.js
exports.process = ({ a, b }) => {
return a + b;
}

Calling Cloud Functions

Less offers 2 ways to call cloud functions:

  1. Using the functions module in the Less SDK.
  2. Using the Less Functions REST API.

Using the SDK

Import functions from @chuva.io/less to call the cloud function. The function payloads are JSON objects.

const { functions } = require('@chuva.io/less');

const sum_result = await functions.sum({ a: 3, b: 4 });
console.log('Result: ', sum_result);
// Result: 7

Let's create a GET /sum route that will return the sum of 2 numbers using our sum cloud function.

mkdir less/apis/demo/sum
touch less/apis/demo/sum/get.js
less/apis/demo/sum/get.js
const { functions } = require('@chuva.io/less');

exports.process = async (request, response) => {
// Get the values to add from the query parameters.
const { a, b } = request.query;

// Call the cloud function.
const sum_result = await functions.sum({ a, b });
response.body = `The sum is: ${sum_result}`
return response;
};
Less REST API Documentation

Read the Less REST API documentation to learn more.

You can now deploy and call your GET /sum route to test your sum cloud function:

  1. Deploy your changes.
npx @chuva.io/less-cli deploy my-less-project
  1. Execute your GET /sum request.
curl "[FUNCTIONS_URL]/sum?a=1&b=2"
# 3

Using the REST API

When using the Cloud Functions feature, Less automatically creates a Functions REST API for you. You can use the API to call cloud functions from anywhere, making it easier to integrate Less with your existing systems.

  1. Deploy your function.
npx @chuva.io/less-cli deploy my-less-project
  1. Retrieve your Functions API URL from your deployment output:
[less-cli] Deployment complete ✅
[less-cli] Resources
[less-cli] - API URLs
[less-cli] - Functions: https://[PROJECT_NAME]-functions.api.eu-0.a83b464c9.less.chuva.cv
[less-cli] 🇨🇻
  1. Send a message to your sum function using the POST /functions/{function_name} route.
curl -X POST -d '{"a": 1, "b": 2}' [FUNCTIONS_BASE_URL]/functions/sum
# 3

Tips & Use-Cases

Tip 0: Allow interoperability between different programming languages
  • E.g. Create a Cloud Function in Python and call it in Javascript.
  • E.g. Get Python math precision in Javascript code.
Tip 1: Share SDKs and libraries between different languages
  • E.g. Wrap your ORMs in Cloud Functions to query your database from anywhere.
  • E.g. Export Faker.js functions and use them in Python.
  • E.g. Wrap your Shared Modules functions in Cloud Functions to make your code accessibile from anywhere.
Tip 2: Call your Cloud Functions outside of Less using the Functions REST API