Progress will discontinue Telerik Platform on May 10th, 2018. Learn more

Defining Cloud Functions

You can define a Cloud Function either from the Telerik Platform portal or by using the Backend Services Metadata API.

In this article:

Structure

You start defining a Cloud Function by giving it a name. The name and its format is important because it becomes a part of the RESTful endpoint that you use to execute the Cloud Function. It must be unique within the app. Choose a name that is short, descriptive, and URL-safe.

When defining the Cloud Function body, always start with the following code. Executing it results in an empty JSON object and status code 200.

Everlive.CloudFunction.onRequest(function (request, response, done) {
    done();
});

The request object contains information about the Cloud Function request, including the HTTP method that was used, the query string, and the principal who executed it on the client, along with any posted data and headers.

Use the response object to customize the response, including status code, headers, modified operation result, and additional data.

The body of the Cloud Function is set using the body property of the response object.

response.body = 'My custom result';

To set the HTTP status code, use the statusCode property:

response.statusCode = 200;

The response headers can be set using the headers property:

response.headers = {
    "HTTP-header-name": "header-value"
}

Example

The following Cloud Function manipulates the response body and headers to return an HTML document instead of the default JSON.

Everlive.CloudFunction.onRequest(function(request, response, done) { 
    response.body = "<html><head></head><body><h2>Sample body</h2></body></html>";
    response.headers = {        
        "Content-type": "text/html"    
    };

    done();
});

Passing Parameters

You can pass parameters to the Cloud Function as query string parameters in the URL.

https://api.everlive.com/v1/your-app-id/functions/MyCloudFunction?a=1&b=2&c=3

Inside the Cloud Function, you have access to the query string through the request.queryString object.

Everlive.CloudFunction.onRequest(function (request, response, done) {

    if (request.queryString) {
        response.body = request.queryString;
        done();
    } else {
        done();
    }
});

The result of the above function is as follows:

{
    "a": "1",
    "b": "2",
    "c": "3"
}

From the body of a Cloud Function, you can access your content types, send push notifications and SMSs, make external requests, as well as take other actions supported by the Cloud Code API.

For example, you could pass an array of values that identify content type items that you want to delete:

https://api.everlive.com/v1/your-app-id/functions/MyCloudFunction?Delete=["MessageText1", "MessageText2", "MessageText3"]

The following code defines a filter expression (query.where().isin()) that is used to delete items from the Messages content type. The expression matches all items that have one of the specified values in their Message field.

Everlive.CloudFunction.onRequest(function (request, response, done) {

        if (request.queryString && request.queryString.Delete) {
            var filterCondition = JSON.parse(request.queryString.Delete);

            var query = new Everlive.Sdk.Query();
            query.where().isin("Message", filterCondition);

            var data = Everlive.Sdk.$.data('Messages');

            data.destroy(query, function (data) {
                console.log("Deleted items: " + data.result);
                done();
            }, function (err) {
                console.log("Error occurred: " + err.message);
            });
        } else {
            done();
        }
    });

Permissions

Each Cloud Function has permissions that control who can execute it. Read more about permissions in the security section.

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.